Reputation: 2051
I have a following dataframe:
varnames<-c( "id", "a", "b", "c", "a.0", "b.0", "c.0", "d", "e", "f")
a <-as.data.frame (matrix (round (rnorm (40, mean = 5, sd = 3), 1), 4, 10))
colnames (a)<-varnames
> a
id a b c a.0 b.0 c.0 d e f
1 2.1 -0.5 7.0 6.0 1.7 9.2 2.6 3.6 4.6 7.0
2 4.0 7.1 4.1 5.4 6.8 4.3 7.7 0.1 5.6 1.4
3 8.7 6.9 3.8 7.5 5.1 3.3 7.9 4.1 10.3 5.7
4 5.6 5.3 3.9 2.8 8.2 4.9 6.9 6.6 3.7 1.9
I am performing three separate GEE analyses with formula always containing "d", "e", and "f" as predictor variables (d+e+f). Each formula has either "a" or "b" or "c" as dependent variable:
library (gee)
model<-lapply ((a)[2:4], function (x) {
gee(x~d+e+f, data=a, id = id)
})
This code works OK.
My next aim is to add "a.0" or "b.0" or "c.0" as predictor variables to formulas having "a", "b" and "c" as dependent variables, correspondingly:
gee(a~a.0+d+e+f, data=a, id = id)
gee(b~b.0+d+e+f, data=a, id = id)
gee(c~c.0+d+e+f, data=a, id = id)
That is, I would like adding to each formula an additional predictor variable corresponding to dependent variable, but having ".0" suffix.
I naively tried the following without much success:
model<-lapply ((a)[2:4], function (x) {
gee(x~ paste(names (x)[2:4], "0", sep = ".")+d+e+f, data=a, id = id)
})
It probably does not work as code " paste(names (x)[2:4], "0", sep = ".") " within function does not return proper variable name, although it does so when used as a separate code.
I would be most grateful to your suggestions on how to automatically insert proper variable names in my formulas.
Upvotes: 0
Views: 170
Reputation: 44299
Instead of working on the columns of a
, you could operate on the names of the variables you want to process:
set.seed(144)
varnames<-c( "id", "a", "b", "c", "a.0", "b.0", "c.0", "d", "e", "f")
a <- as.data.frame (matrix (round (rnorm (40, mean = 5, sd = 3), 1), 4, 10))
colnames(a) <- varnames
model <- lapply(varnames[2:4], function(x) {
gee(paste0(x, "~", x, ".0+d+e+f"), data=a, id=id)
})
Upvotes: 1