【问题标题】:How to understand the usage of rep() in this case?在这种情况下如何理解 rep() 的用法?
【发布时间】:2020-02-18 22:13:43
【问题描述】:

有一个数据集randomdat包含299个obs,两个分类变量,var 9包含With XYZWithout XYZ等值,var8包含Group A/Group B/Group C等值, var1 是一个数值变量。

然后是模型:

m7 <- lm(var3~var1+I(var1^2)+I(var1^3)+var9, data=randomdat)

检查summary(m7),它显示Without XYZ总是比With XYZ小34451.4。

> summary(m7)

Call:
lm(formula = var3 ~ var1 + I(var1^2) + I(var1^3) + var9, data = randomdat)

Residuals:
    Min      1Q  Median      3Q     Max 
-391506  -75127    4799   77175  323856 

Coefficients:
                     Estimate    Std. Error t value            Pr(>|t|)    
(Intercept)     -162934.42035   18571.30251  -8.773 <0.0000000000000002 ***
var1              10927.87454     741.36511  14.740 <0.0000000000000002 ***
I(var1^2)          -180.82979      10.44006 -17.321 <0.0000000000000002 ***
I(var1^3)             0.99499       0.04223  23.562 <0.0000000000000002 ***
var9Without XYZ  -34451.43378   14570.55030  -2.364              0.0187 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 117500 on 294 degrees of freedom
Multiple R-squared:  0.8642,    Adjusted R-squared:  0.8624 
F-statistic: 467.9 on 4 and 294 DF,  p-value: < 0.00000000000000022

那么有两个预测模型:

m7_predictwith <- predict(m7,list(var1=randomdat$var1, var9 = rep("With XYZ",299)))
m7_predictwout <- predict(m7,list(var1=randomdat$var1, var9 = rep("Without XYZ",299)))

如果你绘制它们,你会看到两条线没有重叠。

ggplot(randomdat, aes(x = var1, y = var3)) + 
    geom_point(aes(colour = var8, shape = var8)) + 
    geom_line(aes(x=randomdat$var1,y=m7_predictwith), color = 'red', lty = 2) + 
    geom_line(aes(x=randomdat$var1,y=m7_predictwout), color = 'black', lty = 3)

现在问题来了,在这种情况下如何理解var9 = rep("With XYZ",299)var9 = rep("Without XYZ",299)?它们不是意味着将var9 中的所有值替换为With XYZWithout XYZ 吗? var1m7_predictwithm7_predictwout中是一样的,它们的情节线应该是同一条线?在这种情况下,对rep() 的语法用法非常困惑。

【问题讨论】:

  • 有人吗?......

标签: r data-visualization data-analysis rep


【解决方案1】:

rep() 重复值:

> rep("With XYZ", 5)
[1] "With XYZ" "With XYZ" "With XYZ" "With XYZ" "With XYZ"

这里它被用来创建包含以下内容的数据集:

  • var1 的观测值
  • var9 的固定值。

var9是一个因子变量,在回归中它的估计系数是-34451.43378。因此,如果您预测 "With XYZ" 的固定值为 var9 的一条线,然后预测另一条固定值为 "Without XYZ" 的线,"Without XYZ" 线将向下移动恒定值 34451,创建平行行。

【讨论】:

  • 这是我不明白的部分,系数-34451.43378是"Without XYZ""Without XYZ"的,对吧?因为var9With XYZ 是基线,所以它没有出现在summary(m7),对吧?由于数据集randomdat 包含299 行,var9 = rep("With XYZ",299)var9 = rep("Without XYZ",299),它基本上重复"With XYZ""Without XYZ" 299 次,我可以将其解释为var9 列中的所有单元格现在是"With XYZ""Without XYZ"?在这种情况下,系数 -34451.43378 似乎没有意义。
  • 系数意味着,在所有其他变量相同的情况下,观察“有 XYZ”和“没有 XYZ”之间的差异是 -34451.43378。
  • 是的,但不是var9 = rep("With XYZ",299)var9 = rep("Without XYZ",299) 已经替换了"With XYZ""Without XYZ"var9 中的所有值,因为它重复了299 次并且只有299 var9 中的obs?如果var9 中的所有值都相同,它们如何成为平行线?
  • 预测反映了var1var9 的值,并且var1 的值发生了变化,因此您在var1 中得到不同的预测结果。我不确定这是否能回答您的问题。
  • var1m7_predictwithm7_predictwout 中是相同的,从上面的预测模型可以看出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-19
  • 2014-05-17
  • 1970-01-01
相关资源
最近更新 更多