I have coeftest class object that I coded for some purposes that lm class cannot achieve, but I need to export the object in the way as xtable(lm) does. Any idea would be appreciated!
I think this could help you out:
library(AER) # I think coeftest() belongs to this package.
library(Hmisc)
latex(coeftest(model), digits=5)
Where model is the linear model estimated by lm(). latex() is a function from Hmisc package which is more general than xtable.
If this is not what you are looking for, then provide some data and the expected result so that we can figure it out.
library(stargazer)
stargazer(model, se=coeftest(model), report=vcs)
model is the linear model. stargazer is, at least in my experience, more general than latex() from Hmisc.
Alternatively, if you want to use xtable, could write down a small function:
library(lmtest)
library(xtable)
xtable.coeftest <- function (x, caption = NULL, label = NULL, align = NULL, digits = NULL,
display = NULL, ...)
{
class(x) <- "matrix"
li<-list(coef=x)
return(xtable:::xtable.summary.lm(li, caption = caption, label = label,
align = align, digits = digits, display = display))
}
example(coeftest)
xtable(coeftest(fm))
It is admittedly not the most elegant (there are two issues, one that it is difficult to convert a coeftest object, second that xtable.summary.lm uses x$coef instead of coef(x)) but it works!
Related
I'm tying to display the results of analysis-of-variance applied to univariate regressions into a table created with gtsummary::tbl_uvregression() using another function than car::Anova(), that cannot handle every type of models. In my case I would like to use Anova.clm() from RVAideMemoire package, which is directly based on car::Anova() function and specifically built for clm(m) objects.
Unfortunately, any time I try to use anova_fun argument of add_global_p(), I get an error telling me that mod (the model) should be specified into the function with no default value. I read the documentation of add_global_p(), and in fact it is specified that the "anova_fun" function is used "in place of car::Anova()" and that it must accept a model as one of its arguments. But as far as I know, Anova.clm() uses it.
Moreover, the thing is even when I try to use car::Anova() itself through the anova_fun argument, I still get the same error. I also tried to go through the source codes of the different functions I need here, but couldn't find the solution and begin to get lost...
Here a reprex :
## Load packages
library(survival)
library(gtsummary)
## Create the table using tbl_uvregression
tab <- trial %>%
select(response,age,trt) %>%
tbl_uvregression(method = glm,
method.args = list(family = binomial),
y = response,
exponentiate = T)
## Add global p-values from analysis-of-variance through 'anova_fun' argument
tab %>%
add_global_p(anova_fun = car::Anova)
## And the same for tbl_regression
trial %>%
glm(response ~ age + trt, data = ., family = binomial("logit")) %>%
tbl_regression(exponentiate = T) %>%
add_global_p(anova_fun = car::Anova)
Maybe I just misunderstand the use of anova_fun argument into add_global_p()... In that case could somebody help me to deal with it ? Or at least is there an other (simple) way to add aov global p-values to a tbl_uvregression object applied to clmm models ?
I am trying to write a function that spits out a KM survival curve. I am going to use this in a ShineyApp which is why I want to write a function so I can easily pass in arguments from a dropdown menu (which will input as a string into the strata argument). Here is a simplified version of what I need:
survival_function <- function(data_x, strata_x="1"){
survFormula <- Surv(data_x$time, data_x$status)
my_survfit <- survfit(data=data_x, as.formula(paste("survFormula~", {{strata_x}})))
ggsurvplot(my_survfit, data = data_x, pval=T)
}
survival_function(inputdata, "strata_var")
I get an error:
Error in paste("survFormula1~", { : object 'strata_x' not found
I'm at a loss because
as.formula(paste("~", {{arg}}))
has worked in other functions I've written to produce plots using ggplot to easily change variables to facet by, but this doesn't even seem to recognize strata_x as an argument.
Your function needs a couple of tweaks to get it working with ggsurvplot. It would be best to create the Surv object as a new column in the data frame and use this column in your formula. You also need to make sure you have an actual symbolic formula as the $call$formula member of the survfit object, otherwise ggsurvplot will fail to work due to non-standard evaluation deep within its internals.
library(survival)
library(survminer)
survival_function <- function(data_x, strata_x) {
data_x$s <- Surv(data_x$time, data_x$status)
survFormula <- as.formula(paste("s ~", strata_x))
my_survfit <- survfit(survFormula, data = data_x)
my_survfit$call$formula <- survFormula
ggsurvplot(my_survfit, data = data_x)
}
We can test this on the included lung data set:
survival_function(lung, "sex")
Created on 2022-08-03 by the reprex package (v2.0.1)
I am currently working with R due to a course at university, so I am still quite inexperienced.
We use R for exploratory data analysis. In a data analysis we are supposed to apply different regression models to the data and generate the same plots for each. Additionally, we are supposed to play a bit with the parameters for learning purposes. To avoid unattractive 10-20 times copy-pasting I wrote a function that shows the regression function and the parameters for it as an ellipsis (...). In this function I call the passed function with the ellipsis as parameter.
library("astsa")
data_glob <- globtemp
plot.data.and.reg <- function(data, reg.func, ...){
model <- reg.func(...)
par(mfrow = c(1, 2))
plot(data)
abline(model, col = "orange", lwd = 3)
qqnorm(data)
}
This works for the simple lm function, but unfortunately not for the ksmooth function.
When I pass this function I get the error message: "numeric y must be supplied. For density estimation use density()".
plot.data.and.reg(
data_g,
lm,
list(
formula = as.formula("data_glob ~ time(data_glob)"),
data = data_glob
)
)
plot.data.and.reg(
data_glob,
ksmooth,
list(
x = as.numeric(time(data_glob)),
y = as.numeric(data_glob),
kernel = "box",
bandwidth = 0.25
)
)
Thereupon I looked at the source code of ksmooth. It shows that this error message occurs because the check "missing(y)" fails. Apparently a problem occurs because I passed the parameters as an ellipsis and it doesn't seem to "unpack".
For simplicity, I wrote a dummy function to test if I can add this "unpack" myself.
test.wrapper <- function(func, ...){
func(...)
}
test <- function(x, y){
match.call()
if(missing(y))
print("Leider hatte ich Recht")
print(x)
print(y)
}
test.wrapper(test, list(x = 10, y = 20))
Unfortunately I have not found a solution yet.
From Python I know it so that as with kwargs a dictionary can be unpacked with the ** operator. Is there an equivalent in R? Or how to make sure in R that the parameters from the ellipsis are used correctly?
Since it worked with the lm function without errors I also looked again in their source code . Unfortunately, with my little experience in R, I can't see exactly where the essential difference is.
Overall, I would attribute the error to the fact that the ksmooth function is not yet designed for use with an ellipsis, but I am not sure. How would I need to adjust the ksmooth code to make it work with ...?
(For my Uni task, I will resort to the copy-paste (anti) pattern if in doubt. After searching for so long, I would still be interested in the solution and it may be useful in the future).
Thanks a lot for your help!
The closest equivalent of the */** splat in Python is the do.call function.
However, you don’t need this here. The actual issue is that you’re passing the extra arguments as a list rather than individually. Once you flatten the list, it works1:
plot.data.and.reg(
data_glob,
ksmooth,
x = as.numeric(time(data_glob)),
y = as.numeric(data_glob),
kernel = "box",
bandwidth = 0.25
)
I’m actually surprised that it works with a list for lm; that’s not intentional, it’s essentially an accident caused by how lm is currently implemented.
1 I say it “works” because there’s no error and it plots something, but with your example data there’s no visible regression line (abline is inappropriate for the output of ksmooth), and the smoothing parameters do nothing — the result is identical to the unsmoothed input.
To get this to work, use lines instead of abline. And as for the smoothing, for your example data a bandwidth of 10 works fine.
Been trying to use the rpart.plot package to plot a ctree from the partykit library. The reason for this being that the default plot method is terrible when the tree is deep. In my case, my max_depth = 5.
I really enjoy rpart.plot's output as it allows for deep trees to visually display better. How the output looks for a simple example:
rpart
library(partykit)
library(rpart)
library(rpart.plot)
df_test <- cu.summary[complete.cases(cu.summary),]
multi.class.model <- rpart(Reliability~., data = df_test)
rpart.plot(multi.class.model)
I would like to get this output from the partykit model using ctree
ctree
multi.class.model <- ctree(Reliability~., data = df_test)
rpart.plot(multi.class.model)
>Error: the object passed to prp is not an rpart object
Is there some way one could coerce the ctree object to rpart so this would run?
To the best of my knowledge all the other packages for visualizing rpart trees are really rpart-specific and not based on the agnostic party class for representing trees/recursive partitions. Also, we haven't tried to implement an as.rpart() method for party objects because the rpart class is really not well-suited for this.
But you can try to tweak the partykit visualizations which are customizable through panel functions for almost all aspects of the tree. One thing that might be helpful is to compute a simpleparty object which has all sorts of simple summary information in the $info of each node. This can then be used in the node_terminal() panel function for printing information in the tree display. Consider the following simple example for predicting one of three school types in the German Socio-Economic Panel. To achieve the desired depth I switch significance testing essentiall off:
library("partykit")
data("GSOEP9402", package = "AER")
ct <- ctree(school ~ ., data = GSOEP9402, maxdepth = 5, alpha = 0.5)
The default plot(ct) on a sufficiently big device gives you:
When turning the tree into a simpleparty you get a textual summary by default:
st <- as.simpleparty(ct)
plot(st)
This has still overlapping labels so we could set up a small convenience function that extracts the interesting bits from the $info of each node and puts them into a longer character vector with less wide entries:
myfun <- function(i) c(
as.character(i$prediction),
paste("n =", i$n),
format(round(i$distribution/i$n, digits = 3), nsmall = 3)
)
plot(st, tp_args = list(FUN = myfun), ep_args = list(justmin = 20))
In addition to the arguments of the terminal panel function (tp_args) I have tweaked the arguments of the edge panel function (ep_args) to avoid some of the overplotting in the edges.
Of course, you could also change the entire panel function and roll your own...
I'm trying to write a for loop in R (my first!) in order to produce and save diagnostic plots of several mixed effects models fitted using the function lmer in the package lme4. This is what I've done so far exemplified with the sleepstudy data:
require(lme4)
mod1<-lmer(Reaction ~ Days + (1|Subject),sleepstudy)
mod2<-lmer(Reaction ~ 1 + (1|Subject),sleepstudy)
List<-c(mod1,mod2)
names<-c("mod1","mod2")
i=1
for (i in 1:length(List)) {
jpeg(file = paste("modelval_", names[i], ".jpg", sep=""))
par(mfrow=c(2,2))
plot(resid(List[i]) ~ fitted(List[i]),main="residual plot")
abline(h=0)
qqnorm(resid(List[i]), main="Q-Q plot of residuals")
qqnorm(ranef(List[i])$Subject$"(Intercept)", main="Q-Q plot of random effect" )
dev.off()
}
I get the following error message when typing into R consol:
Error in function (formula, data = NULL, subset = NULL, na.action = na.fail, :
invalid type (NULL) for variable 'resid(list[i])'
I've got a feeling the problem is related to the list of models I've created and not the for loop itself and I think it might be related to the model objects being of class S4. Is it possible to make such a list?
I've also tried to make the list like below, with no improvements (still get the same error message)
List<-list(mod1,mod2)
First using c can risk losing the class structure of the objects you've created. To make a list containing your models, use list(mod1, mod2).
Second, List[i] is a list of length 1 containing the i'th element of List. Use List[[i]] to extract the element itself (your model).