> I want to take the first variable (column) of a data frame and regress
> it against all other variables.
>
> bla <- function (dat) {
> reg <- lm(whateverthefirstofthevariablenamesis ~., data=dat)
> return(reg)
> }
>
>Thanks to all who answered my question:
Prof. Brian Ripley: ------------------- bla <- function (dat) eval(substitute(lm(foo ~., data=dat), list(foo=as.name(names(dat)[1]))))
which has the advantage of embedding a clean value of $call.
[EMAIL PROTECTED]: -------------------- lm(formula = dat)
[EMAIL PROTECTED]: ----------------- for(j in 1:ncol(dat)) { fff <- as.formula(paste(names(dat)[j],"~", paste(names(dat)[-j],collapse="+"))) nm <- paste("rslt",j,sep=".") assign(nm,lm(fff,data=dat)) }
[EMAIL PROTECTED] ----------------------- data(longley) lm( longley[,7] ~. , data = longley[,-7] )
You cannot call data() inside a function:
data(dat)
reg <- lm(dat[,1] ~ dat[,-1])
Error in model.frame(formula, rownames, variables, varnames, extras, extranames, :
invalid variable type
In addition: Warning message:
Data set 'dat' not found in: data(dat)
Regards
Christian
--
Dr.sc.math.Christian W. Hoffmann, http://www.wsl.ch/staff/christian.hoffmann
Mathematics + Statistical Computing e-mail: [EMAIL PROTECTED]
Swiss Federal Research Institute WSL Tel: ++41-44-73922- -77 (office)
CH-8903 Birmensdorf, Switzerland -11(exchange), -15 (fax)
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
