问题
I calculated Cook's distance manually and with the function cooks.distance
and I got two different results. Can someone please help me understand why?
Below is how I manually calculate Cook's distance:
j=rnorm(100)
o=rexp(100)
p=runif(100)
model=lm(j~o+p)
O=model.matrix(model)
P = O%*% solve(t(O) %*% O) %*% t(O)
lev=diag(P)
b<-solve(t(O)%*%O)%*%t(O)%*%j
RSS <- sum((j-O%*%b)^2)
s2<- RSS/97 #three predictors (including intercept (100-3=97))
residuals(model)^2/(4*s2)*(lev/(1-lev)^2)
The above code returns 0.003180925
, but when I use cooks.distance(model)
R returns 0.004197956
.
Thank you in advance for your help!
来源:https://stackoverflow.com/questions/62107571/how-to-manually-calculate-cooks-distance