Variable in formula statement

Hi, I am trying to make a variable in a formula statement without success. I have ten undependent variables X1 through X10, and I am trying to use them one at a time in the formula statement. The error I get is
Error in model.frame.default(formula = formula, data = df, drop.unused.levels = TRUE) :
variable lengths differ (found for 'Z'). Please advise. Thank you.

t <- vector()
for (i in 1:10){
Z <- paste("X",i,sep="")
formula <- Y ~ Z
model <- lm(formula, data=df)
model_summary <- summary(model)
t[i] <- model_summary$coefficients[2,3] # test statistic for beta1
}
t

Use as.formula() to have a text string interpreted as a formula.

Thank you, nirgrahamuk

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.