【问题标题】:Augment with newdata works with one model fails with another使用 newdata 进行的扩充适用于一种模型,但适用于另一种模型
【发布时间】:2016-09-03 12:37:28
【问题描述】:

我在这里遗漏了一些如此基本的东西——为什么增强与 一个模型而不是另一个模型?

data(iris)
library(broom)

iris$cSepal.Length <- scale(iris$Sepal.Length, center = TRUE, scale = FALSE)
nd <- expand.grid(Sepal.Length = seq(4, 8, 0.1), Species = factor(levels(iris$Species)))
nd$cSepal.Length <- nd$Sepal.Length - mean(iris$Sepal.Length)

m0 <- lm(Sepal.Width ~ Sepal.Length * Species, data = iris)
pred.0 <- augment(m0, newdata = nd)
m1 <- lm(Sepal.Width ~ cSepal.Length * Species, data = iris)
pred.1 <- augment(m1, newdata = nd)

## Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 123, 150

sessionInfo()

## R version 3.3.1 (2016-06-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 10586)
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] broom_0.4.1         RevoUtilsMath_8.0.3
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.6      knitr_1.11       magrittr_1.5     mnormt_1.5-4    
##  [5] lattice_0.20-33  R6_2.1.2         stringr_1.0.0    plyr_1.8.4      
##  [9] dplyr_0.5.0      tools_3.3.1      parallel_3.3.1   grid_3.3.1      
## [13] nlme_3.1-128     psych_1.6.6      DBI_0.4-1        htmltools_0.2.6 
## [17] yaml_2.1.13      assertthat_0.1   digest_0.6.8     tibble_1.1      
## [21] reshape2_1.4.1   formatR_1.4      tidyr_0.5.1      evaluate_0.7.2  
## [25] rmarkdown_0.9.5  stringi_1.1.1    RevoUtils_10.0.1

【问题讨论】:

  • &gt; nrow(iris); [1] 150&gt; nrow(nd); [1] 123 ; stackoverflow.com/a/26148043/2381339 好像有两个表/dfs 在这里加入了?
  • 是的,但为什么它适用于 m0 而不是 m1

标签: r broom


【解决方案1】:

这里的问题在于scale()。它返回一个矩阵而不是一个向量。将矩阵分配到 data.frame 会导致问题。您正在使用scale() 创建iris$cSepal.Length,但使用x-mean(x) 创建nd$cSepal.Length。这会创建两种不同的数据类型,当您使用cSepal.Length 列时,这会在两种不同模型的预测过程中导致问题。

最好在每次创建列时使用相同的居中方法。如果你刚刚做了

iris$cSepal.Length <- iris$Sepal.Length - mean(iris$Sepal.Length)

一切都会好起来的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2014-01-21
    • 2023-03-13
    相关资源
    最近更新 更多