# [R] First Variable in lm

Liaw, Andy andy_liaw at merck.com
Wed Mar 24 17:04:52 CET 2004

```You might be trying too hard:

> dat <- data.frame(y=rnorm(10), x1=rnorm(10), x2=rnorm(10))
> fit <- lm(dat)
> summary(fit)

Call:
lm(formula = dat)

Residuals:
Min       1Q   Median       3Q      Max
-1.11643 -0.42746 -0.01442  0.55902  1.04890

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)   0.7804     0.2700   2.891   0.0233 *
x1            0.7469     0.2925   2.553   0.0379 *
x2           -0.1099     0.2155  -0.510   0.6257
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1

Residual standard error: 0.8234 on 7 degrees of freedom
Multiple R-Squared: 0.5016,     Adjusted R-squared: 0.3592
F-statistic: 3.522 on 2 and 7 DF,  p-value: 0.08742

HTH,
Andy

> From: Christian Hoffmann
>
> Hi all,
>
> I just cannot think of how to do it:
> 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)
> }
>
> What kind of function do I have to take instead of the
> whateverthefirstofthevariablenamesis,
>
> eval(), substitute(), get(),  ...
>
> to correctly compute this regression?
>
> With   lm(get(names(dat)[1] ~., data=dat) there are no
> errors, but the
> first variable also shows up among the regressors.
>
> Thanks for help.
> Christian Hoffmann
>
> --
> Dr.sc.math.Christian W. Hoffmann,
> http://www.wsl.ch/staff/christian.hoffmann
> Mathematics + Statistical Computing   e-mail:
> christian.hoffmann at wsl.ch
> Swiss Federal Research Institute WSL  Tel: ++41-44-73922-
> -77  (office)
> CH-8903 Birmensdorf, Switzerland             -11(exchange), -15  (fax)
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help