I have a dataframe with a random number of quantitative variables. I need write a function that calculate lm for predicting the values of the dependent variable. As predictors, I want to use only those variables that have p.value > 0.05. The function should return linear regression coefficients constructed only for selected predictors as a vector. If there are no such predictors in the data, then the function should returns the warning "There are no normal variables in the data". I write the function, but it does not work.
smart_lm <- function(x) {
sl <- apply(x[2:dim(x)[2]], 2, function(x) shapiro.test(x)$p.value)
my_reg <- lm(as.formula(paste("x[[1]]~",paste(x[2:dim(x)[2]], collapse = "+"))))
return(ifelse(sl[sl > 0.05], my_reg, "There are no normal variables in the data"))
}