R – lmer function for loop

I am really new to this. I’d appreciate some help! The dataframe I have imported is a big table of all my results and I’d really like it to run through each column using the same lmer function.

for (i in d[c(6:45)]){   para = colnames(d[c(6:45)])[i] #para is parameter   d.lmer <- lmer(get(para) ~ Group*Sex*CS.NCS + (1|Dam), data = d)       d.anova <- anova(d.lmer)      library(emmeans)   d.compare <- emmeans(d.lmer, ~ Sex*Group*CS.NCS) # Sex, Group, and CS.NCS are columns too.      d.contrast <- contrast(d.compare, "consec", simple = "each", combine = TRUE, adjust = "mvt")      d.pairs <- pairs(d.compare)    } 

However, I keep getting

Error in get(para) : object 'NA' not found 

para is indeed just a long strong of NA and I have no idea where it’s coming from!

Ideally, I’d like it to write to an excel spreadsheet with this in the for loop, but I also can’t figure out how to get the file= to use the d.lmer function as its name. But that’s the next issue to deal with.

write.xlsx(d.compare, file = "X.xlsx",            sheetName = "Compare", append = FALSE) write.xlsx(d.contrast, file = "X.xlsx",             sheetName="Contrast", append=TRUE) write.xlsx(d.pairs, file = "X.xlsx",             sheetName="Pairs", append=TRUE) write.xlsx(d.anova, file = "X.xlsx",             sheetName="anova", append=TRUE) 

Sorry if this is a stupid question – my understanding of R largely comes from Google.

Thank you!

Edit: I should add, this works fine for this:

d.lmer <- lmer(Blood ~ Group*Sex*CS.NCS + (1|Dam), data = d) #Blood is an example parameter/ column header        d.anova <- anova(d.lmer)        library(emmeans) d.compare <- emmeans(d.lmer, ~ Sex*Group*CS.NCS) # Sex, Group, and CS.NCS are columns too.        d.contrast <- contrast(d.compare, "consec", simple = "each", combine = TRUE, adjust = "mvt")        d.pairs <- pairs(d.compare) 

I just don’t want to manually type in every column header.

Add Comment
1 Answer(s)

Without sample data, it’s tricky to confirm that this answer will be helpful. Yet it seems like your problem has do with how the loop is specified. Currently, your loop is looping over a data frame.

x <- d[c(6:45)] class(x) 

But it appears you wish to add each column (index 6 to 45) to your regression formula.

To set para to a single column name, you could probably adjust the beginning of your code to something like this, where the para can be included in a formula:

# load libraries library(lme4) library(emmeans)  # specify that d.pairs is a list d.pairs <- list()    for (i in colnames(d[c(6:45)])){ # here you loop over the column names      para = i #para is parameter # here para is set to the ith column          f <- as.formula(paste(para, paste("Group*Sex*CS.NCS + (1|Dam)", collapse=" * "), sep=" ~ ")) # here you make a formula      d.lmer <- lmer(f, data = d) # here you use the formula      d.anova <- anova(d.lmer)            d.compare <- emmeans(d.lmer, ~ Sex*Group*CS.NCS) # Sex, Group, and CS.NCS are columns too.            d.contrast <- contrast(d.compare, "consec", simple = "each", combine = TRUE, adjust = "mvt")            d.pairs[[i]] <- pairs(d.compare) # assign pairs output to ith of d.pairs   } 
Answered on August 30, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.