I could use some help wrapping a loop around a function.
I have 26 different models for different plant species that all have identical explanatory variables and structure. Ultimately I want to extract the model coefficients to a table.
First, I created a function to extract the coefficients from one model and put them in a row of an empty dataframe called mod.out. I can run this function for a single model by typing in the model name and a unique row number.
coefs<- function(model, row.num){
mod.out[row.num,1]<-strtrim(deparse(substitute(model)), 4)
mod.out[row.num, 2:4]<-summary(model)$coefficients[1, c(1,2,4)]
mod.out[row.num,5:7]<-summary(model)$coefficients[2, c(1,2,4)]
mod.out[row.num,8:10]<-summary(model)$coefficients[3, c(1,2,4)]
mod.out[row.num,11:13]<-summary(model)$coefficients[4, c(1,2,4)]
mod.out[row.num,14]<-summary(model)$optinfo$val[1]
return(mod.out)
}
What I would like to do now is write a loop to go through this function for every model to put each set of coefficients in a new row in the mod.out dataframe. The models are glmers. I created a list of all the model names:
mod.name<-c(abam.mort, abco.mort, abgr.mort, abla.mort, acma.mort, arme.mort, cade.mort, chch.mort, chla.mort, juoc.mort, laoc.mort, lide.mort, pial.mort, piat.mort, pico.mort, pien.mort, pije.mort, pila.mort, pimo.mort, pipo.mort, psme.mort, quch.mort, thpl.mort, tshe.mort, tsme.mort, umca.mort)
I thought I would be able to write a loop function pretty easily to go through it, but I cannot get it to work. I've tried lots of different flavors of the get() and paste() command, but I'm doing something wrong. I think that the problem is in how I am specifying the model name when the function is inside the loops, but I can't figure it out. Any help would be greatly appreciated. Right now I have:
for(i in 1:nrow(mod.out)){
coefs(mod.name[i], i)}
I know that there are packages that do things similar to this, but I am working hard to learn functions and loops, so I would really like to do it this way if possible. Thanks!