2
$\begingroup$

Beforehand

Usually, we can get intercept and slope of a linear regression from numbers we get from a covariance analysis of same variables. This goes like this:

slope = cov(x, y)/ var(x)
intercept = mean(y) - slope * mean(x)

For example, if I try this with x = c(1, 4, 17, 20) and y = c(0, 4, 6, 9), I do get same slope and intercept as from linear regression. In both cases, the intercept is 0.8472 and the slope is 0.3717.


Problem

My actual data is x = c(-62135599651, -62135599057, -62135598463, -62135597869, -62135597275, -62135596681, -62135596087, -62135595493, -62135594899, -62135594305) and y = c(0.1, 0.2, 0.4, 0.3, 0.5, 0.5, 0.9, 0.9, 0.8, 1.0). If I try to fit a linear regression here, I get no estimate for the slope! See here:

lm(y ~ x)
Coefficients:
(Intercept)            x  
       0.56           NA 

After reading why this happens I think this is because of rank deficiency. I then tried to get the numbers from the covariance analysis as described above and I get an intercept of 10523935.9730680529 and a slope of 0.0001693705. I was surprised that this worked and did not return an error. Further, the regression line looks plausible, i.e. is not far away from points or something:

plot(x, y)
abline(intercept, slope)

plot

Question

  • Why does in this case the relation slope = cov(x, y)/ var(x) and intercept = mean(y) - slope * mean(x) not work?
  • Is it legetime to calculate intercept and slope from numbers of the covariance analysis if linear regression fails (as I did for my data above)?
$\endgroup$

1 Answer 1

1
$\begingroup$

The $x$-values in your data are enormous and not very different from each other, and that appears to be causing some kind of issue when it comes to performing the calculations in the lm function. Further, while the issue causes the slope not to be calculated, the intercept need not be reliable, as your subsequent calculations showed.

Since the two methods you've discussed are mathematically equivalent, your question is equivalent to the following.

Is it okay to use my knowledge of mathematics to get around a computer hiccup?

YES, it is okay to use your knowledge of mathematics to get around a computer hiccup.

For what it's worth, if you add 62135590000 to your x, you wind up with the same slope estimate that you got.

x <- c(-62135599651, -62135599057, -62135598463, -62135597869, -62135597275, -62135596681, -62135596087, -62135595493, -62135594899, -62135594305)+62135590000
y <- c(0.1, 0.2, 0.4, 0.3, 0.5, 0.5, 0.9, 0.9, 0.8, 1.0)
L <- lm(y ~ x)
summary(L)
Call:
lm(formula = y ~ x)

Residuals:
      Min        1Q    Median        3Q       Max 
-0.112121 -0.085000 -0.008788  0.064545  0.189091 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 1.742e+00  1.404e-01  12.405 1.66e-06 ***
x           1.694e-04  1.955e-05   8.665 2.45e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1055 on 8 degrees of freedom
Multiple R-squared:  0.9037,    Adjusted R-squared:  0.8917 
F-statistic: 75.08 on 1 and 8 DF,  p-value: 2.447e-05

EDIT

When I adjust the intercept to account for my shift in $x$, I get about the same values that you got. Given how enormous these numbers are, I consider this acceptable.

(summary(L)$coef[2, 1] * 62135590000) - summary(L)$coef[1, 1] = 10523932.4893337

The numbers under consideration are on the order of ten-million, and they differ by about three.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.