Passing character arguments to escalc function, metafor R package - metafor

I have to fit several meta regression models and I was trying to write a for loop in which distinct variables names were passed to escalc function.
The task looks like this:
library(metafor)
dat <- get(data(dat.bcg))
some_vars<-c("tpos","tneg","cpos","cneg")
dat <- escalc(measure="RR", ai=some_vars[1], bi=some_vars[2], ci=some_vars[3], di=some_vars[4], data=dat)
However this fails with an error.
How can I pass variable names to escalc?

A bit ugly, but this should work:
dat <- escalc(measure="RR", ai=eval(parse(text=some_vars[1])),
bi=eval(parse(text=some_vars[2])),
ci=eval(parse(text=some_vars[3])),
di=eval(parse(text=some_vars[4])), data=dat)

Related

Writing a function to produce a Kaplan-Meier curve

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)

Using Vector of Character Variables within certain Part of the lm() Function of R

I am performing a regression analysis within R that looks the following:
lm_carclass_mod <- lm(log(count_faves+1)~log(views+1)+dateadded+group_url+license+log(precontext.nextphoto.views+1)+log(precontext.prevphoto.views+1)+log(oid.Bridge+1)+log(oid.Face+1)+log(oid.Quail+1)+log(oid.Sky+1)+log(oid.Car+1)+log(oid.Auditorium+1)+log(oid.Font+1)+log(oid.Lane+1)+log(oid.Bmw+1)+log(oid.Racing+1)+log(oid.Wheel+1),data=flickrcar_wo_country)
confint(lm_carclass_mod,level=0.95)
summary(lm_carclass_mod)
The dependent variable as well as some of the independent variables are quite variable throughout my analysis, which is why I would like to keep inserting them manually.
However, I am looking for a way to replace all of the "oid. ..." variables with one single function.
So far I have come up with the following:
g <- paste("log(",variables,"+1)", collapse="+")
Unfortuntaley this does not work inside the lm() function. Neither does a formula like this:
g <- as.formula(
paste("log(",variables,"+1)", collapse="+")
)
The vector variables has the following elements in it:
variables <- ("oid.Bridge", "oid.Face", "oid.Quail", "oid.Off-roading", "oid.Sky", "oid.Car", "oid.Auditorium", "oid.Font", "oid.Lane", "oid.Bmw", "oid.Racing", "oid.Wheel")
In the end my regression model should look something like this:
lm_carclass_mod <- lm(log(count_faves+1)~log(views+1)+dateadded+group_url+license+log(precontext.nextphoto.views+1)+log(precontext.prevphoto.views+1)+g,data=flickrcar_wo_country)
confint(lm_carclass_mod,level=0.95)
summary(lm_carclass_mod)
Thanks for your helpm in advance!
You would need to convert both of the parts into a string and then make the formula:
#the manual bit
manual <- "log(count_faves+1)~log(views+1)+dateadded+group_url+license+log(precontext.nextphoto.views+1)+log(precontext.prevphoto.views+1)"
#the variables:
oid_variables <- c("oid.Bridge", "oid.Face", "oid.Quail", "oid.Off-roading", "oid.Sky", "oid.Car", "oid.Auditorium", "oid.Font", "oid.Lane", "oid.Bmw", "oid.Racing", "oid.Wheel")
#paste them together
g <- paste("log(", oid_variables, "+1)", collapse="+")
#make the formula
myformula <- as.formula(paste(manual, '+', g))
Then you add the formula into lm:
lm_carclass_mod <- lm(myformula, data=flickrcar_wo_country

Error in class(x) while creating panel data using plm function

I'm trying to create a Panel data using the plm function for pooling a model from a balanced Panel data that I imported from Excel.
When I run the code I get the following error:
Error in class(x) <- setdiff(class(x), "pseries") : invalid to set
the class to matrix unless the dimension attribute is of length 2 (was
0)
library(plm)
library(readxl)
library(tidyr)
library(rJava)
library(xlsx)
library(xlsxjars)
all_met<- read_excel("data.xlsx", sheet = "all_met")
attach(all_met)
Y_all_met <- cbind(methane)
X_all_met <- cbind(gdp, ecogr, trade)
pdata_all_met <- plm.data(all_met, index=c("id","time"))
pooling_all_met <- plm(Y_all_met ~ X_all_met, data=pdata_all_met, model= "pooling")
After running the code I was supposed to get summary statistics of a pooled ols regression of my data. Can someone tell me how I can fix this issue? Thanks in advance.
1st:
Avoid plm.data and use pdata.frame instead:
pdata_all_met <- pdata.frame(all_met, index=c("id","time"))
If plm.data does not give you a deprecation warning, use a newer version of the package.
2nd (and addressing the question):
Specify the column names in the formula, not the variables from the global environment if you use the data argument of plm, i.e., try this:
plm(methane ~ gdp + ecogr + trade, data=pdata_all_met, model= "pooling")
check in the structure of your data if variables used in the regression are declared as factor, you can do that by typing: str(all_met).
if yes, then you should declare it as double, or as numeric, (try not to use as.numeric() function, it could change values in your data).
personally i've changed that by the next specification in the import code:
data <- read_csv("C:/Users/Uness/Desktop/Mydata.csv",
col_types = cols(variable1 = col_double(),
variable2 = col_double()))
View(data)
where variable1 and variable2 are the names of the variables I use, make sure you change that if you copy the code ;)

Exporting output of custom multiple regressions from R to Latex

I am trying to export the results of multiple regressions in a single table. Ideally, it should be formatted similar to stargazer() output.
The problem is that I have not found reliably working R functions for the kind of regressions I need (Fama-MacBeth regressions), so I use my custom regression functions, which produce all necessary output (estimates of coefficients, standard errors, t-stat, R^2).
Does stargazer() or other similar function have the parameters, which allow me to export results of multiple regressions to Latex in a nice form when output of my regression is just a dataframe?
EDIT: I was just wondering whether it is possible to create publication-style tables, looking like this:
Here's a simple example that might help you forward (example is too long for a comment, so making this an answer):
library(stargazer)
library(broom)
## generate dummy data
set.seed(123)
x <- runif(1000)
z <- x^0.5
y <- x + z + rnorm(1000, sd=.05)
model1 <- lm(y ~ x)
model2 <- lm(y ~ z)
## transform model summaries into dataframes
tidy(model1) -> model1_tidy
tidy(model2) -> model2_tidy
merge(model1_tidy, model2_tidy, by='term', all.x=T, all.y=T) -> output
stargazer(output, type='latex', summary=FALSE)
You will need to figure out the column headers by yourself but I believe you get the idea.

How to use lmer inside a function

I'm trying to write a function that collects some calls I use often in scripts
I use the sleepstudy data of the lme4 package in my examples
Here's (a simplified version of) the function I started with:
trimModel1 <- function(frm, df) {
require(LMERConvenienceFunctions)
require(lme4)
lm<-lmer(frm,data=df)
lm.trimmed = romr.fnc(lm, df)
df = lm.trimmed$data
# update initial model on trimmed data
lm<-lmer(frm,data=df)
# lm#call$formula<-frm
mcp.fnc(lm)
lm
}
When I call this function like below:
(fm1<-trimModel1(Reaction ~ Days + (Days|Subject),sleepstudy))
The first three lines of the output look like this:
Linear mixed model fit by REML
Formula: frm
Data: df
If I had called the commands of the trimModel1 function in the console the first three lines of the summary of the model look like this:
Linear mixed model fit by REML
Formula: Reaction ~ Days + (Days | Subject)
Data: sleepstudy
The difference is a problem because several packages that use the lme4 package make use of the formula and data fields. For instance the effects package uses these fields and a command like below will not work when I use the trimModel1 function above:
library(effects)
plot(allEffects(fm1))
I looked around on stackoverflow and R discussion groups for a solution and saw that you could change the formula field of the model. If you uncomment the lm#call$formula<-frm line in the trimModel1 function the formula field in the summary is displayed correctly. Unfortunately when I run a function from the effects package now I still get the error:
Error in terms.formula(formula, data = data) :
'data' argument is of the wrong type
This is because the data field is still incorrect.
Another possible solution I found is this function:
trimModel2 <- function(frm, df) {
require(LMERConvenienceFunctions)
require(lme4)
lm<-do.call("lmer",list(frm,data=df))
lm.trimmed = romr.fnc(lm, df)
df = lm.trimmed$data
# update initial model on trimmed data
lm<-do.call("lmer",list(frm,data=df))
mcp.fnc(lm)
lm
}
When I now type the following commands in the console I get no errors:
(fm2<-trimModel2(Reaction ~ Days + (Days|Subject),sleepstudy))
plot(allEffects(fm2))
The allEffects function works but now the problem is that the the summary of the fm2 model displays the raw sleepstudy data. That is not a big problem with the sleepstudy data but with very large datasets sometimes Rstudio crashed when displaying a model.
Does anyone know how to make one (or both) of these functions work correctly?
I think I have to change the fm1#call$data field but I don't know how.
Do it like this:
trimModel1 <- function(frm, df) {
require(LMERConvenienceFunctions)
require(lme4)
dfname <- as.name(deparse(substitute(df)))
lm<-lmer(frm,data=df)
lm.trimmed = romr.fnc(lm, df)
df = lm.trimmed$data
# update initial model on trimmed data
lm<-lmer(frm,data=df)
lm#call$formula <- frm
lm#call$data <- dfname
mcp.fnc(lm)
lm
}
It's the "deparse-substitute trick" to get an object name from the object itself.

Resources