How do you set values from the summary function ie adjusted r squared to a variable?
You can see the structure of the object returned by the summary function using the str function. If I store the output of a fit in an object called FIT, then I can run
str(summary(FIT))
To get the R-squared value
DF <- data.frame(Xval = 1:5, Yval = 11:15 +rnorm(5,0,2))
FIT <- lm(Yval ~ Xval, data = DF)
summary(FIT)
#>
#> Call:
#> lm(formula = Yval ~ Xval, data = DF)
#>
#> Residuals:
#> 1 2 3 4 5
#> 1.0790 -0.1979 -2.5099 1.2974 0.3313
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 9.5388 1.8462 5.167 0.0141 *
#> Xval 1.3981 0.5566 2.512 0.0868 .
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 1.76 on 3 degrees of freedom
#> Multiple R-squared: 0.6777, Adjusted R-squared: 0.5703
#> F-statistic: 6.308 on 1 and 3 DF, p-value: 0.08682
Rsqr <- summary(FIT)$r.squared
Rsqr
#> [1] 0.6777015
Created on 2020-03-13 by the reprex package (v0.3.0)
2 Likes
And if you need the information in a table, @tinafmills, you can also use the broom
package to help you store model information:
DF <- data.frame(Xval = 1:5, Yval = 11:15 +rnorm(5,0,2))
FIT <- lm(Yval ~ Xval, data = DF)
library(broom)
tidy(FIT)
#> # A tibble: 2 x 5
#> term estimate std.error statistic p.value
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 8.13 0.631 12.9 0.00101
#> 2 Xval 0.999 0.190 5.25 0.0135
glance(FIT)
#> # A tibble: 1 x 11
#> r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
#> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 0.902 0.869 0.602 27.6 0.0135 2 -3.28 12.6 11.4
#> # … with 2 more variables: deviance <dbl>, df.residual <int>
Created on 2020-03-13 by the reprex package (v0.3.0)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.