Shouldn't -2*loglike of an lm object = deviance of that lm object in R? I want to make sure my R code below is accurate. Because generally -2*logLik(lm_object) should equal deviance(lm_object).
But in my R code below, these two numbers don't match. Do I have a bug I'm missing?
set.seed(2)
n = 300
x <- rnorm(n)
e <- rnorm(n, 0, 3)
B0 = .5 ; B1 = 2

y <- B0 + B1*x + e
m <- lm(y~x)

-2*logLik(m) #   1500.681 !!

deviance(m)  #  2612.792  !!

 A: As this CrossValidated answer points out, the deviance (which is the difference between -2*log(L) for the model and -2*log(L) for the saturated model, multiplied by the dispersion) for the linear model is equal to the sum of squared residuals (weighted, if  necessary).
Here is the computational analogue of the algebra done in the linked answer:
deviance(m) ## 2612.792
sum(residuals(m)^2) ## 2612.792
nll2_m <- -2*sum(dnorm(y, predict(m), sd = sigma(m), log=TRUE))  ## 1500.688
nll2_s <- -2*sum(dnorm(y, y, sd=sigma(m), log=TRUE))  ## 1202.688
(nll2_m - nll2_s)*sigma(m)^2  ## 2612.792

A: You could try testing with the original statistical definition of log likelihood:
#Data
set.seed(2)
n = 300
x <- rnorm(n)
e <- rnorm(n, 0, 3)
B0 = .5 ; B1 = 2
#Model
y <- B0 + B1*x + e
m <- lm(y~x)

What you did is correct:
#Code 1
-2*logLik(m) 

Output:
'log Lik.' 1500.681 (df=3)

And the statistical perspective:
#Code 2 below statistical definition
sigma <- summary(m)$sigma
-2* sum(log(dnorm(x = y, mean = predict(m), sd = sigma)))

Output:
[1] 1500.688

