This is a function from an earlier question of mine: How to let R predict user input I would like to make it easier to supply several names to the xname argument but I still can't fugure out just how to do that.
lmfun<-function(df,yname,xname){
y<-deparse(substitute(yname))
x<-deparse(substitute(xname))
f<-as.formula(paste0(y,"~",x))
lm.fit<-do.call("lm",list(data=quote(df),f))
coef(lm.fit)
}
Here's what I've tried
vals<-names(mtcars)[-1]
lmfun(mtcars,mpg,disp)#This works
How can I best make this work? I've tried several other ways but showing only this:
for(name in 1:seq_along(vals)){
name<-eval(substitute(name))
lmfun(mtcars,mpg,name)
}
This fails:
Error in deparse(substitute(xname)) : 'arg' should be one of “mpg”, “cyl”, “disp”, “hp”, “drat”, “wt”, “qsec”, “vs”, “am”, “gear”, “carb”
Also tried:
for(name in 1:length(vals)){
vals<-noquote(vals)
lmfun(mtcars,mpg,vals[name])
}
I would also appreciate if I could be pointed at a way to incorporate multilinear regression. That is xname+xname1+xname2
Thanks!