[R] linear regression: Is there a way to get the results of lm as variables
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Wed Oct 21 11:28:36 CEST 2009
On Wed, 21 Oct 2009, CE.KA wrote:
>
> Hi R users
>
> I used R to get the results of a linear regression
>
> reg<-lm(y~x)
>
> here are the results:
>
> # Call:
> # lm(formula = donnees$txi7098 ~ donnees$txs7098)
> #
> # Residuals:
> # Min 1Q Median 3Q Max
> # -0.037971 -0.013373 -0.004947 0.010618 0.053235
> #
> # Coefficients:
> # Estimate Std. Error t value Pr(>|t|)
> # (Intercept) 0.08547 0.03028 2.822 0.011738 *
> # donnees$txs7098 0.62306 0.12847 4.850 0.000150 ***
> # ---
> # Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1
> #
> # Residual standard error: 0.02199 on 17 degrees of freedom
> # Multiple R-squared: 0.5805, Adjusted R-squared: 0.5558
> # F-statistic: 23.52 on 1 and 17 DF, p-value: 0.0001502
>
> I know how to get the coefficients as variable
> for exemple:
> reg$coefficients[1]=0.08547 (Estimate )
> reg$coefficients[2]=0.62306 (Estimate )
The recommended way to extract many quantities from fitted model objects
(including "lm" and "glm" objects but also many others) are extractor
functions. Instead of
reg$coefficients
it is preferred to use
coef(reg)
Similarly, there are extractors vcov(), logLik(), residuals(), fitted(),
deviance() etc. summary() computes many statistics as well, in particular,
summary()$coefficients has the table of coefficients with standard errors,
t statistics etc.
> My question is:
> Is there a way to get the other results of lm as variables?
> -Std. Errors?
Either via
sqrt(diag(vcov(reg)))
or
summary(reg)$coefficients[,2]
> -t value?
> -Pr(>|t|) ?
Also via summary(reg)$coefficients[,3] and [,4] respectively.
> -Residual standard error?
summary(reg)$sigma
> -Multiple R-squared?
summary(reg)$r.squared
> -F-statistic?
summary(reg)$fstatistic
hth,
Z
More information about the R-help
mailing list