Using strings in a for loop in R - error when including string as variable in ANOVA within for loop

243 Views Asked by At

I have the following ANOVA in R which works great:

fit <- aov(dependent1 ~ X + Z + X*Z, data=dataset)
drop1(fit,~.,test="F")

"dependent1", "X", and "Z" are the column names.

I want to make a for loop where I loop over a certain amount of dependent variables, and I tried this:

dependent_variables <- c("dependent1", "dependent2", "dependent3")

for (i in dependent_variables) {
  fit <- aov(i ~ X + Z + X*Z, data=dataset)
  drop1(fit,~.,test="F")
}

If I run this, I get an error message:

Error in model.frame.default(formula = i ~ X + Z + X *  : 
  variable lengths differ (found for 'X')

Any idea what goes wrong here?

2

There are 2 best solutions below

0
On BEST ANSWER

Example data (which may or may not fulfil the criteria for an ANOVA)

X <- rnorm(100)
Z <- rnorm(100)
dependent1 <- rnorm(100)
dependent2 <- rnorm(100)
dependent3 <- rnorm(100)

dataset <- cbind(data.frame(X, Z, dependent1, dependent2, dependent3))

The following script would work, you need to put in the row column numbers of your dependent variables:

for (i in 3:5) {
fit <- aov(dataset[ , i] ~ X + Z + X*Z, data=dataset)
drop <- drop1(fit,~.,test="F")
print(fit)
print(drop)
}
2
On

Why not loop through data instead of looping through names? Perhaps this is a bit clunkier than what you're trying to do.

Create data

dependent1 = runif(100);
dependent2 = runif(100);
dependent3 = runif(100);
dataset = data.frame(X=1:100, Z=rnorm(1,1,100))

Run single ANOVA

fit = aov(dependent1 ~ X + Z + X*Z, data=dataset)
drop1(fit,~.,test="F")

cbind the dependents together and loop over them, storing results in list objects

d = cbind(dependent1, dependent2, dependent3)
fit = list(); drop = list()

for (i in 1:ncol(d)) {
  fit[[i]] = aov(d[,i] ~ X + Z + X*Z, data=dataset)
  drop[[i]] = drop1(fit[[i]],~.,test="F")
}

** Edited: called fit instead of fit[[i]]. Sorry about that.