2

I am attempting to get the Adjusted R-Square value in R (the programming language) and store it as a variable. I am not sure how to accomplish this.

I can see the R-Square value if I call:

summary(lm(x~y))

Along with the rest of the data, but how do I get the specific value?

TestinginProd
  • 1,085
  • 1
  • 15
  • 35

3 Answers3

4

summary( lm(y~x) )$adj.r.squared

  • 3
    Also, it is probably worth exploring the str() command (short for structure). for example str(summary(lm(x~y))) – SamPassmore Nov 24 '15 at 00:32
4

CrockGill's answer is correct, but I also think it is important you know how to find the code to get these variables.

You can use the attributes function like this:

attributes(summary(lm(x~y)))

This returns:

$names
 [1] "call"          "terms"         "residuals"     "coefficients"  "aliased"       "sigma"        
 [7] "df"            "r.squared"     "adj.r.squared" "fstatistic"    "cov.unscaled" 

$class
[1] "summary.lm"

From this you can find that $adj.r.squared is what you need to type after summary(lm(x~y)).

Yang Li
  • 462
  • 1
  • 8
  • 21
1

Stackoverflow is the better place to ask this kind of question, but briefly,

x <- 1:5
y <- jitter(x * 2) + rnorm(5)
fit <- lm(y~x)
names(fit)
str(fit) # more detail

For the coefficients,

fit$coef

will print them. The commands str and names can help you figure a lot of stuff out.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • 3
    Adjusted $R^2$ is actually a component of `summary(fit)`, so `names(fit)` will not be helpful, but `names(summary(fit))` would be. –  Nov 24 '15 at 00:31