0
mydat=structure(list(yield = c(5008L, 3402L, 4588L, 6004L, 7065L, 4449L, 
6037L, 7200L, 4341L, 5433L, 4864L), x = c(5705.160194, 3562.567871, 
4152.888076, 4428.184115, 3491.35426, 4093.656026, 5178.103678, 
3349.67327, 4708.902256, 5949.647693, 4785.498224)), class = "data.frame", row.names = c(NA, 
-11L))

I need calculate R^2 like here shown Function to calculate R2 (R-squared) in R

preds <- dt$x
actual <- dt$yield
rss <- sum((preds - actual) ^ 2)  ## residual sum of squares
tss <- sum((actual - mean(actual) ^ 2))  ## total sum of squares
rsq <- 1 - rss/tss
rsq

and rsq=1.10343 it can't be more then 1. What i did wrong?

psysky
  • 3,037
  • 5
  • 28
  • 64

2 Answers2

2

Change this:

tss <- sum((actual - mean(actual)^2))

in

tss <- sum((actual - mean(actual))^2)
Leonardo
  • 2,439
  • 33
  • 17
  • 31
1

It is likely that x is not the predictions but is a predictor that goes into a linear regression. Perform the regression, fm, in which case the predicted values are fitted(fm) and then get the R squared from summary or get it directly as shown in the alternatives.

fm <- lm(yield ~ x, mydat)
summary(fm)$r.squared
# [1] 0.02508245

# same
cor(mydat$yield, fitted(fm))^2
# [1] 0.02508245

# same
with(mydat, cor(yield, x)^2)
# [1] 0.02508245

# same
tss <- with(mydat, sum((yield - mean(yield))^2))
rss <- deviance(fm)
1 - rss/tss
# [1] 0.02508245

# same
tss <- with(mydat, sum((yield - mean(yield))^2))
rss <- sum(resid(fm)^2)
1 - rss/tss
# [1] 0.02508245


plot(yield ~ x, mydat)
abline(fm)

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341