【问题标题】:R: Issues of duplication of coefficient when running lm() with csv [duplicate]R:使用 csv 运行 lm() 时出现重复系数的问题 [重复]
【发布时间】:2016-11-17 01:35:02
【问题描述】:

所以我已经使用 read.csv 从我的 csv 中提取了我的数据,并且工作正常。 代码:

 techdata <- read.csv("/user/path", header = TRUE, sep = ",", stringsAsFactors = FALSE)

我已将数据检查为 r 中的输出,它的格式正确且看起来不错。

但是当我跑步时

techFit <- lm(techdata$reordered_in_next_30_days ~techdata$work_order_frequency)
summary(techFit)

我明白了

Call:
lm(formula = techdata$reordered_in_next_30_days ~ techdata$work_order_frequency)

Residuals:
   Min     1Q Median     3Q    Max 
-5.416 -0.886 -0.283  0.372 94.531 

Coefficients:
                                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)                        2.46871    0.03142  78.581  < 2e-16 ***
techdata$work_order_frequency1     2.94734    0.08425  34.985  < 2e-16 ***
techdata$work_order_frequency10   -0.80860    0.07465 -10.832  < 2e-16 ***
techdata$work_order_frequency100  -2.43363    0.36315  -6.702 2.08e-11 ***
techdata$work_order_frequency101  -2.26871    0.34024  -6.668 2.61e-11 ***
techdata$work_order_frequency102  -2.36591    0.26592  -8.897  < 2e-16 ***
techdata$work_order_frequency103  -2.21871    0.48387  -4.585 4.54e-06 ***
....
techdata$work_order_frequencyNULL -2.46871    0.05592 -44.151  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.731 on 72500 degrees of freedom
Multiple R-squared:  0.141, Adjusted R-squared:  0.1378 
F-statistic: 44.07 on 270 and 72500 DF,  p-value: < 2.2e-16

应该只有 1 个“techdata$work_order_frequency”,但最终会超过 100 个。

这真的让我很难解释数据发生了什么,并且想更好地理解它为什么会发生。

【问题讨论】:

  • 你能 dput() 你的数据吗?

标签: r csv lm


【解决方案1】:

看起来您的列 work_order_frequency 是一个字符列。默认情况下,如果您使用read.csv,这将作为一个因素读入。这就是 lm 函数将此列拆分为单独系数的原因。

使用选项stringsAsFactors = FALSE 再次读取您的数据。

【讨论】:

  • 我试过了,问题依旧
  • 检查数据结构以检查因素并重新启动您的 r 会话,然后再次运行所有内容。
  • 所以,结构看起来不错,列的定义和组织得当。但我可能在结构中遗漏了一些必要的东西,因此我们将不胜感激任何额外的信息/帮助。
猜你喜欢
  • 2021-05-22
  • 1970-01-01
  • 2014-10-22
  • 2011-01-19
  • 2019-06-08
  • 2020-11-25
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多