在R的回归中如何处理残差中的NA? [英] How do I deal with NAs in residuals in a regression in R?
问题描述
所以我在R中的lm
横截面回归的残差中存在一些NA
值的问题.
So I am having some issues with some NA
values in the residuals of a lm
cross sectional regression in R.
问题不是NA
值本身,而是R呈现它们的方式.
The issue isn't the NA
values themselves, it's the way R presents them.
例如:
test$residuals
# 1 2 4 5
# 0.2757677 -0.5772193 -5.3061303 4.5102816
test$residuals[3]
# 4
# -5.30613
在这个简单的示例中,NA
值将使残差之一丢失.当我提取残差时,我可以清楚地看到第三个索引丢失了.到目前为止一切顺利,这里没有任何投诉.问题在于,现在相应的数字向量要短一个项目,因此第三个索引实际上是第四个索引.如何让R返回这些残差,即显式显示NA
而不是跳过索引?
In this simple example a NA
value will make one of the residuals go missing. When I extract the residuals I can clearly see the third index missing. So far so good, no complaints here. The problem is that the corresponding numeric vector is now one item shorter so the third index is actually the fourth. How can I make R return these residuals instead, i.e., explicitly showing NA
instead of skipping an index?
test$residuals
# 1 2 3 4 5
# 0.2757677 -0.5772193 NA -5.3061303 4.5102816
我需要跟踪所有单个残差,因此,如果我可以这样提取它们,将会使我的生活变得更加轻松.
I need to keep track of all individual residuals so it would make my life much easier if I could extract them this way instead.
推荐答案
我刚刚找到 深入搜索. resid
和lm
上的resid
函数是解决方法.
I just found this googling around a bit deeper. The resid
function on a lm
with na.action=na.exclude
is the way to go.
这篇关于在R的回归中如何处理残差中的NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!