I am finding it very difficult to understand how to use lapply on imputed datasets in R.
Here is a code for an example dataset (with 6 Variables: "Ozone", "Solar.R", "Wind", "Temp", "Month", "Day"):
data <- airquality
data[4:10,3] <- rep(NA,7)
data[1:5,4] <- NA
tempData <- mice(data,m=5,maxit=50,meth='pmm',seed=500)
After that, let's run a linear regression on the imputed datasets.
> reg1
call :
with.mids(data = tempData, expr = lm(Ozone ~ Wind))
call1 :
mice(data = data, m = 5, method = "pmm", maxit = 50, seed = 500)
nmis :
Ozone Solar.R Wind Temp Month Day
37 7 7 5 0 0
analyses :
[[1]]
Call:
lm(formula = Ozone ~ Wind)
Coefficients:
(Intercept) Wind
92.401 -5.067
[...]
So far, so good.
Now I was wondering if I can use lapply to conduct regression analyses for multiple dependent variables and store them in a list type object. Below, you can see my failed attempt.
> variables_subset<-c("Ozone","Solar.R", "Temp")
> models<-lapply(tempData[,variables_subset],
+ function(x) (with(tempData, lm(x ~ Wind))))
Error in tempData[, variables_subset] : incorrect number of dimensions
Is there a way to make this code work?