Re: [R] getting the p-value from lm as a list object
In your example, you could have noted that summary had an element named coefficients which was a 2 x 4 object, with names "(Intercept)" and "x". Although you could have asked for the values with coefficients[2,4], the use of names makes the intent more clear. snipped from the str(summary(fm)) output: $ coefficients : num [1:2, 1:4] 1.067 0.588 1.520 0.245 0.702 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:2] "(Intercept)" "x" .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)" So ... > summary(fm)$coefficients Estimate Std. Error t value Pr(>|t|) (Intercept) 1.067 1.5196358 0.7019226 0.50263396 x 0.5878788 0.2449115 2.4003725 0.04315164 > summary(fm)$coefficients["x","Pr(>|t|)"] [1] 0.04315164 -- David Winsemius, MD Heritage Labs On Oct 31, 2008, at 11:59 AM, eric lee wrote: Hi, I'm trying to get the p-value from the 'lm' regression function as a list object. For example, I can get r^2 from the following code by entering summary(fm)$r.squared. Is there a way to get the p-value? If not, is there a function where I can enter the f-value and degrees of freedom to get the p-value? Thanks. x <- c(1,2,3,4,5,6,7,8,9,10) y <- c(1,2,3,4,4,5,6,8,1,9) fm <- lm(y ~ x) str(summary(fm)) [[alternative HTML version deleted]] __ 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. __ 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.
Re: [R] getting the p-value from lm as a list object
on 10/31/2008 10:59 AM eric lee wrote: > Hi, > > I'm trying to get the p-value from the 'lm' regression function as a list > object. For example, I can get r^2 from the following code by entering > summary(fm)$r.squared. Is there a way to get the p-value? If not, is there > a function where I can enter the f-value and degrees of freedom to get the > p-value? Thanks. > > x <- c(1,2,3,4,5,6,7,8,9,10) > y <- c(1,2,3,4,4,5,6,8,1,9) > > fm <- lm(y ~ x) > str(summary(fm)) The default output is created in stats:::print.summary.lm(). The basic incantation is: pf(x$fstatistic[1], x$fstatistic[2], x$fstatistic[3], lower.tail = FALSE) where: pf() is the F distribution function x = the summary.lm model object x$fstatistic[1] = model F statistic x$fstatistic[2] = model numerator DF x$fstatistic[3] = model denominator DF Thus, using lm.D9 from example(lm): > summary(lm.D9) Call: lm(formula = weight ~ group) Residuals: Min 1Q Median 3Q Max -1.0710 -0.4938 0.0685 0.2462 1.3690 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 5.0320 0.2202 22.850 9.55e-15 *** groupTrt -0.3710 0.3114 -1.1910.249 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.6964 on 18 degrees of freedom Multiple R-squared: 0.07308,Adjusted R-squared: 0.02158 F-statistic: 1.419 on 1 and 18 DF, p-value: 0.249 > summary(lm.D9)$fstatistic value numdf dendf 1.419101 1.00 18.00 > pf(1.419, 1, 18, lower = FALSE) [1] 0.2490394 See ?pf HTH, Marc Schwartz __ 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.
Re: [R] getting the p-value from lm as a list object
eric lee wrote: Hi, I'm trying to get the p-value from the 'lm' regression function as a list object. For example, I can get r^2 from the following code by entering summary(fm)$r.squared. Is there a way to get the p-value? If not, is there a function where I can enter the f-value and degrees of freedom to get the p-value? Thanks. x <- c(1,2,3,4,5,6,7,8,9,10) y <- c(1,2,3,4,4,5,6,8,1,9) fm <- lm(y ~ x) str(summary(fm)) What about the following (taken from stats:::print.summary.lm): x <- c(1,2,3,4,5,6,7,8,9,10) y <- c(1,2,3,4,4,5,6,8,1,9) fm <- lm(y ~ x) summary(fm) # for comparison only sfm <- summary(fm) pf(sfm$fstatistic[1], sfm$fstatistic[2], sfm$fstatistic[3], lower.tail = FALSE) Thomas P. __ 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.