Selecting different columns of a data frame to run ANOVA in a for loop in R? -


i trying run anova binomially distributed data in data frame named megadata first 4 columns categorical variable unit, year , species. below r code isn't working when try loop across different columns of data frame run model.

mod <- list() (i in megadata[,5:36]) {   for(j in length(megadata[,5:36])){     mod[[j]] <- glm(i/number ~ unit*beginyear*species_raw,     family = binomial(link = logit), weight=number,      data = megadata)     print(anova(mod[[j]]), test="chisq")     print(summary(mod[[j]]))   } } 

if trying train 1 model each column in 5:36, obtaining list of fitted models , printing out summary of each model, try:

mod <- list() (j in 5:36) {   mod[[j]] <- glm(paste0(names(megadata)[j], "/number~unit*beginyear*species_raw"),                   family = binomial(link = logit), weight=number, data = megadata)   print(anova(mod[[j]]), test="chisq")   print(summary(mod[[j]])) } 

data:

set.seed(144) megadata <- data.frame(number=sample(1001:2000, 1000, replace=true), unit=sample(1:10, 1000, replace=true), beginyear=sample(2000:2010, 1000, replace=true), species_raw=sample(letters[1:3], 1000, replace=true)) (i in 1:32) {megadata[[paste0("dat", i)]] <- sample(1:1000, 1000, replace=true)} 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -