r - Using a for loop to extract coefficients from multiple models -
i have multiple cox models (with 1 variable static in models) , trying extract coefficient variable.
in models coefficient indexed follows: example in model1 model1[[8]][1] ; model2 model2[[8]][1] etc. attempted create loop r shown below not working.
could me why getting error when running following code
for (i in 1:5) { coef[i] <- exp(summary(model[i])[[8]][1]) }
i following error "object 'model' not found".
many in advance
a
here example of meant in comment
data(iris) model1 <- lm(data = iris, sepal.length ~ sepal.width + species) model2 <- lm(data = iris, sepal.length ~ sepal.width)
you can don't have type models.
model.list<-mget(grep("model[0-9]+$", ls(),value=t))
ls() lists object have , grep() taking objects have names "model" followed number.
coefs<-lapply(model.list,function(x)coef(x)[2]) unlist(coefs) sepal.width sepal.width 0.8035609 -0.2233611
Comments
Post a Comment