You have a basic idea: you must create a list of data frames and then use lapply to apply the function to each element of the list. Unfortunately, there are a few oddities in your code.
It makes no sense to randomly generate a seed, and then set it. You need to use set.seed to play random numbers. Cut lines
seed <- round(runif(10)*1000000)
and, perhaps,
set.seed(x)
rep(1:3, c(rep(3, 3))) matches rep(1:3, each = 3) .
Do not call var or matrix variables, because they will mask the names of these functions. since it is confusing.
3:ncol(x) is dangerous. If x has less than 3 columns, this does not do what you think.
... and now the problem you really wanted to solve.
The problem is the line out <- lm(gdt[x]$yvar ~ gdt[x][, ind[ind]]) .
lapply passes data frames to anovp rather than pointing, so x is a data frame in gdt[x] . What causes the error.
One more thing. While you are rewriting this line, note that lm takes a data argument, so you don't need to do things like gdt$some_column ; you can just reference some_column directly.
EDIT: further tips.
You always use the formula yvar ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 . Since every time every time create it before calling lapply .
independent_vars <- paste(colnames(gdt[[1]])[-1:-2], collapse = " + ") model_formula <- formula(paste("yvar", independent_vars, sep = " ~ "))
I probably wouldn't worry about the anovp function. Just do
models <- lapply(gdt, function(data) lm(model_formula, data))
Then enable further call lapply to play with odds if necessary. The next line replicates your anovp code, but will not work, because model$coefficients is a vector (so the sizes are wrong). Tune to get the bit you really want.
coeffs <- lapply(models, function(model) do.call(rbind, model$coefficients[,4][2]))