R_User
R_User

Reputation: 11082

How to calculate the 'Coefficient of determination' for a linear model in R?

I have the following set of x and y values:

x = c(1:150)
y = x^-.5 * 155 + (runif(length(x), min=-3, max=3))

And run a linear regression on the data:

plot(x, y, log="xy", cex=.5)

model = lm(log(y) ~ log(x))
model

Now I'd like to have a measure for the quality of the regression and was told that people usually use R^2 ('Coefficient of determination'), which should be close to one.

How can I easily calculate the value in R? I've seen that residuals etc. are already calculated.

Upvotes: 2

Views: 1450

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61154

Use summary(model) to print a detailed output into de console. You can also use str to explore the extructure of an object, for example str(summary(model)) this is useful when you can to extract part of that output using $.

result <- summary(model)   # summary of the model
str(result)                # see structure of the summary
result$r.squared           # extract R squared (coefficient of determination)

result contains both, R.squared and Adjusted R squared, see for example the following output

Residual standard error: 0.09178 on 148 degrees of freedom
Multiple R-squared:  0.9643,    Adjusted R-squared:  0.964 
F-statistic:  3992 on 1 and 148 DF,  p-value: < 2.2e-16

that output is just the last part of summary(model) printed in the console

Upvotes: 3

Related Questions