# Why normal r.squared and fitted values are not the same when computed manually?

I am wondering if the computed R-squared from lm would be the same if you compute the predictions and then calculate R and square it between them and the original values (to be predicted). In my case here they are different. the model is:

log(y) = alog(1+x1)+blog(1+x2)+c
x1 <- c(1,2,3,4,NA,5,5,6);x1 <- log(x1)
x2 <- c(0.9,0.4,0.8,4,NA,3,4,6);x2 <- log(x2)
y  <- c(1.6,4.4,5.5,8.3,3,NA,7,3)
y2 = log(y)
fm1 <- lm(y2 ~ x1+x2)
> summary(fm1)
Call:
lm(formula = y2 ~ x1 + x2)

Residuals:
1         2         3         4         7         8
-0.147261 -0.029340  0.008704  0.612128  0.229587 -0.673819

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)   0.5868     0.5551   1.057    0.368
x1            0.9509     0.5871   1.620    0.204
x2           -0.2892     0.3517  -0.822    0.471

Residual standard error: 0.549 on 3 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared:  0.5079,    Adjusted R-squared:  0.1799
F-statistic: 1.548 on 2 and 3 DF,  p-value: 0.3452


Now lets compare the correlations:

> y1= exp((0.9509*x1)+ (-0.2892 * x2) +(0.5868))
> cor(y1,y, use="na.or.complete", method="pearson")
 0.4813884
> (0.4813884)^2
 0.2317348


You can see here the values are different!

> y1
 1.647246 2.600102 2.838444 2.381542       NA 2.792149 2.639814 2.655218
> fitted(fm2)
1        2        3        4        7        8
0.661362 1.471144 1.751985 1.450974 1.728920 1.752749

• I didn't get past the fact that your x1 and x2 are identical, so the fitting should throw out one as collinear. Otherwise you seem to be comparing correlations and $R^2$. – Nick Cox May 4 '15 at 14:49
• sorry I pasted the wrong data,but they should be:x1 <- c(1,2,3,4,NA,5,5,6) x1 <- log(1+x1) x2 <- c(0.9,0.4,0.8,4,NA,3,4,6) x2 <- log(1+x2) y <- c(1.6,4.4,5.5,8.3,3,NA,7,3) – hyat May 4 '15 at 14:53
• hyat = sim??? Please merge your accounts. Could you clean up the post then? Also, the existence of NAs is just a distraction here. Post a worked example with commentary. Also, many people here do not use R, so please focus on the statistics of your question. – Nick Cox May 4 '15 at 14:58
• The fallacy here is that the correlation between $y$ and $\hat y$ should be equal to that between $y$ and $\exp(\hat y)$. Also, you are confusing yourself: you replace x1 with log(1 + x1) and similarly x2, so you don't need to do that calculation again. – Nick Cox May 4 '15 at 17:14

In the bivariate case,it would be the same as calculating the correlation between your predictor and your outcome (not the predicted values), but this is more complicated with more than one predictor in your model. Let's take an example with a small amount of data. First, the simple bivariate case in which y is predicted by x:

x<- c(1,2,3,4,5,6)
y<- 4 + .2*x - 3*y + rnorm(6)

Call:
lm(formula = y ~ x)

Residuals:
1       2       3       4       5       6
-2.7744  3.6449 -3.2407  4.0280  0.9585 -2.6162

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  -3.4765     3.4669  -1.003    0.373
x            -0.1860     0.8902  -0.209    0.845

Residual standard error: 3.724 on 4 degrees of freedom
Multiple R-squared:  0.0108,    Adjusted R-squared:  -0.2365
F-statistic: 0.04365 on 1 and 4 DF,  p-value: 0.8447


Here we get an $R^2$ of .0108. Taking the square root of this value is the same as the Pearson's correlation coefficient.

> sqrt(.0108)
 0.103923
> cor(x,y)
 -0.1039008


The difference we see in values has to be due to rounding. Your case includes 2 predictors so let's add an additional predictor variable:

z <- c(3,2,4,1,3,4)


The result of the lm():

Call: lm(formula = y ~ x + z)

Residuals:
1       2       3       4       5       6
-1.3542  1.9666  0.1159 -1.1361  0.8294 -0.4215

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)   2.8495     2.1165   1.346   0.2709
x             0.2013     0.3977   0.506   0.6476
z            -2.7111     0.6365  -4.259   0.0237 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.62 on 3 degrees of freedom
Multiple R-squared:  0.8596,    Adjusted R-squared:  0.7661
F-statistic: 9.187 on 2 and 3 DF,  p-value: 0.05258


Although it isn't something I have personally come across in my own data analysis adventures, http://www.real-statistics.com/correlation/multiple-correlation/, gives the formula for a multiple correlation coefficient as:

$$\sqrt{\frac {r_{xy}^2 + r_{zy}^2 - 2r_{xy}^2r_{zy}^2r_{xz}^2}{1-r_{xz}^2}}$$

By plugging in the correlation coefficients:

corrxy<- cor(x,y)
corryz<- cor(y,z)
corrxz<-cor(x,z)
sqrt((corrxy^2 + corryz^2 - 2*corrxy* corryz*corrxz)/(1-corrxz^2))

> sqrt((corrxy^2 + corryz^2 - 2*corrxy* corryz*corrxz)/(1-corrxz^2))
 0.9275252
> sqrt(.8596)
 0.9271462


These two values are not exactly the same and I think this is likely because of some rounding issue, but they are quite close. I am not certain what math you are doing to come up with a multivariate correlation coefficient, or what you mean by using the predicted values to obtain this number, but if you correctly calculate the correlation coefficient for three variables it should match with the the square root of the $R^2$ associated with the omnibus, 2 df, test. Hope this helps!