简体   繁体   English

用R绘制线性模型(lm)时产生的NaN

[英]NaNs produced when plotting a linear model (lm) with R

I am trying to create a normal regression model and a logistic one to predict fraud in real state data. 我正在尝试创建一个正常的回归模型和一个逻辑模型来预测真实状态数据中的欺诈行为。 I work with a mixed data set (categorical and numerical variables) where I have done the pre-processing and recoding so that I had balanced weight of each level per categorical variable (avoiding variables containing levels with only 1 registry mixed with levels that have many observations, and so on). 我使用混合数据集(分类变量和数值变量)进行预处理和重新编码,以使每个分类变量的每个级别的权重保持平衡(避免仅包含1个注册表的级别与包含许多级别的级别混合的变量)观察,等等)。 I added an interaction to increase the R^2 of my lm. 我添加了一个交互来增加我的lm的R ^ 2。 When I want to plot my linear model I get this warning: 当我要绘制线性模型时,会收到以下警告:

    Warning messages:
1: In sqrt(crit * p * (1 - hh)/hh) : NaNs produced
2: In sqrt(crit * p * (1 - hh)/hh) : NaNs produced

It appears to be related to Cook's distance - https://bugs.r-project.org/bugzilla3/show_bug.cgi?format=multiple&id=9316 - (influent factors, even though I removed outliers...). 它似乎与库克的距离有关-https://bugs.r-project.org/bugzilla3/show_bug.cgi?format=multiple&id =9316- (影响因素,即使我删除了异常值...)。 Any idea what is causing this error and what can be done to plot the linear model? 知道导致此错误的原因是什么,如何绘制线性模型?

Example of my code: 我的代码示例:

lm.a3 <- lm(log(response) ~(.-file_status)*file_status, data=data) 
final.lm3 <- stepAIC(lm.a3,direction="both")
summary(final.lm3) #R^2 = 64%
par(mfrow=c(2,2))
plot(final.lm3)

Thanks for your time and I appreciate your answers 感谢您的宝贵时间,我感谢您的答复

The problem was that I did logarithm transformations before the stepAIC function was run to improve the fit. 问题是我在运行stepAIC函数以提高拟合度之前进行了对数转换。 As some of my response variables where equal to 1, when doing log(response_variable) the output of this function was equal to zero for some cases. 正如我的某些响应变量等于1一样,当执行log(response_variable)时,在某些情况下此函数的输出等于零。 Adding a minimum quantity to the argument of the logarithm function resolved the issue: log(response_variable + 0.0001234). 向对数函数的参数添加最小数量可以解决此问题:log(response_variable + 0.0001234)。 Thanks to @LyzandeR for his feedback. 感谢@LyzandeR的反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM