对数变换残差的 R 绝对值

R absolute value of residuals with log transformation

我在 R 中有一个形式为

的线性模型
lm(log(num_encounters) ~ log(distance)*sampling_effort, data=df)

我想解释残差,但要让它们回到 num_encounters 的规模。我见过 residuals.lm(x, type="working")residuals.lm(x, type="response") 但我不确定它们返回的值。例如,我是否仍需要使用 exp() 将残差值恢复到 num_encounters 范围内?或者他们已经达到了那个规模?我想在直方图中和之后的栅格图中绘制这些绝对值。

编辑: 基本上我的困惑是以下代码产生了 3 个不同的直方图,而我期望前 2 个是相同的。

df$predicted <- exp(predict(x, newdata=df))
histogram(df$num_encounters-df$predicted)
histogram(exp(residuals(x, type="response")))
histogram(residuals(x, type="response"))

I want to interpret the residuals but get them back on the scale of num_encounters.

你可以很容易地计算出它们:

mod <- lm(log(num_encounters) ~ log(distance)*sampling_effort, data=df)
res <- df$num_encounters - exp(predict(mod))

此外,@Roland 的建议确实是正确且有效的,我的困惑问题只是基本的高中对数代数。

确实,绝对响应残差(在原始因变量的范围内)可以按照@Roland所说的

来计算
mod <- lm(log(num_encounters) ~ log(distance)*sampling_effort, data=df)
res <- df$num_encounters - exp(predict(mod))

如果你想从模型残差中计算它们,你需要考虑对数减法规则。

log(a)-log(b)=log(a/b)

残差是根据原始模型计算的。所以在我的例子中,模型预测 log(num_encounters)。所以残差是log(observed)-log(predicted)

我想做的是

exp(resid) = exp(log(obs)-log(pred)) = exp(log(obs/pred)) = obs/pred

这显然不是我要找的数字。要从模型响应残差中获取绝对响应残差,这就是我所需要的。

obs-obs/exp(resid)

所以在 R 代码中,您也可以这样做:

mod <- lm(log(num_encounters) ~ log(distance)*sampling_effort, data=df)
abs_resid <- df$num_encounters - df$num_encounters/exp(residuals(mod, type="response"))

这导致与@Roland 描述的方法相同的数字,当然更容易。但至少我的大脑又恢复了正常。