Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R DFFIT的计算作为回归中杠杆和影响的诊断_R_Regression_Linear Regression_Lm - Fatal编程技术网

R DFFIT的计算作为回归中杠杆和影响的诊断

R DFFIT的计算作为回归中杠杆和影响的诊断,r,regression,linear-regression,lm,R,Regression,Linear Regression,Lm,我正试图手工计算DFFITS。获得的值应等于通过dffits函数获得的第一个值。然而,我自己的计算肯定有问题 attach(cars) x1 <- lm(speed ~ dist, data = cars) # all observations x2 <- lm(speed ~ dist, data = cars[-1,]) # without first obs x <- model.matrix(speed ~ dist) # x matrix h <- d

我正试图手工计算DFFITS。获得的值应等于通过
dffits
函数获得的第一个值。然而,我自己的计算肯定有问题

attach(cars)

x1 <- lm(speed ~ dist, data = cars) # all observations

x2 <-  lm(speed ~ dist, data = cars[-1,]) # without first obs

x <- model.matrix(speed ~ dist) # x matrix

h <- diag(x%*%solve(crossprod(x))%*%t(x)) # hat values

num_dffits <- x1$fitted.values[1] - x2$fitted.values[1] #Numerator

denom_dffits <- sqrt(anova(x2)$`Mean Sq`[2]*h[1]) #Denominator

df_fits <- num_dffits/denom_dffits #DFFITS

dffits(x1)[1] # DFFITS function
附加(车辆)

你的分子错了。由于您已从第二个模型中删除了第一个数据,相应的预测值不在
拟合(x2)
中。我们需要使用
predict(x2,cars[1,])
代替
fitted(x2)[1]


Hat值可通过以下方式有效计算:

h <- rowSums(qr.Q(x1$qr) ^ 2)

在分子中,它是具有所有观测值的第一个拟合值与不具有第一个观测值的第一个拟合值之间的差值。在分母中,它是拟合值的方差平方根,没有第一个观测值。
h <- hat(x1$qr, FALSE)
h <- lm.influence(x1, FALSE)$hat
h <- hatvalues(x1)
c(crossprod(x2$residuals)) / x2$df.residual