How to manually calculate Cook's distance

这一生的挚爱 提交于 2021-02-11 14:16:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!