estimate in lm function in R doesn't match correlation (data with NA)

linear-regression, r, statistics

Solution

In your code `scale` is applied prior to `na.omit` within `lm`. Compare these two:

DF <- data.frame(x, y) 
na.omit(scale(DF))
scale(na.omit(DF))

And then use this:

fit1<-lm(scale(y) ~ scale(x), data=na.omit(DF))

all.equal(unname(coef(fit1)[2]),
          cor(na.omit(DF))[1,2])
#[1] TRUE

Problem

I'm fitting lm model ``` x <- c(0.1, 0.3, 0.2, 0.5, NA, 0.1, 0.8, 0.4) y <- c(0.3, 0.2, 0.5, NA, 0.4, 0.5, 0.2, 0.4) fit1<-lm(scale(y) ~ scale(x), na.action=na.omit) summary(fit1) ``` This gives me a standardized estimate -0.593 When I apply the function 'cor' it gives me value of -0.577. If i subset complete cases from two vectors i.e. ``` x2 <- c(0.1, 0.3, 0.2, 0.1, 0.8, 0.4) y2 <- c(0.3, 0.2, 0.5, 0.5, 0.2, 0.4) ``` and then fit lm ``` fit2<-lm(scale(y2) ~ scale(x2)) summary(fit2) ``` the standardized estimate is the same as in the case of 'cor'(-0.577). I think standardized estimate and correlation coefficient should be the same in simple regression. The question is what is the problem with fit1? (using 'na.action=na.excluse' is not helpful).

Original source