CE.KA
2009-Oct-21 09:07 UTC
[R] linear regression: Is there a way to get the results of lm as variables
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 ) My question is: Is there a way to get the other results of lm as variables? -Std. Errors? -t value? -Pr(>|t|) ? -Residual standard error? -Multiple R-squared? -F-statistic? Best regards -- View this message in context: http://www.nabble.com/linear-regression%3A-Is-there-a-way-to-get-the-results-of-lm-as-variables-tp25988916p25988916.html Sent from the R help mailing list archive at Nabble.com.
Achim Zeileis
2009-Oct-21 09:28 UTC
[R] linear regression: Is there a way to get the results of lm as variables
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
Jorge Ivan Velez
2009-Oct-21 14:18 UTC
[R] linear regression: Is there a way to get the results of lm as variables
Hi CE.KA, Take a look at the following: # Data set.seed(123) x <- rnorm(100) y <- 2 + 1.5*x + rnorm(100) # Regression model reg <- lm(y ~ x) # The summary summary(reg) # Objects present in the summary() names(summary(reg)) # Extracting the coefficients summary(reg)$coeff HTH, Jorge On Wed, Oct 21, 2009 at 5:07 AM, 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 ) > > My question is: > Is there a way to get the other results of lm as variables? > -Std. Errors? > -t value? > -Pr(>|t|) ? > -Residual standard error? > -Multiple R-squared? > -F-statistic? > > Best regards > > > -- > View this message in context: > http://www.nabble.com/linear-regression%3A-Is-there-a-way-to-get-the-results-of-lm-as-variables-tp25988916p25988916.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]