【问题标题】:Error: invalid type (list) for variable when executing rlm错误:执行 rlm 时变量的类型(列表)无效
【发布时间】:2015-03-19 03:19:57
【问题描述】:

我正在尝试为我的数据集中的每个位置拟合一个模型。我运行以下代码(重新创建了一个示例以反映我正在使用的数据):

library(plyr)
library(MASS)

month.abbr <- c("Jan", "Feb", "Mar", "Apr", "May",
     "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
month <- factor(rep(month.abbr, length = 1200), levels = month.abbr)
county_code <- sample(1:100, 1200, replace = T)
store_rev <- sample(2000:85000, 1200, replace = T)
color_levels <-c('blue', 'red', 'green')
colors <- factor(rep(color_levels, length = 1200), levels = color_levels)
data <- data.frame(month, county_code, store_rev, colors)

product_aggregate_values <-ddply(data, ~month+county_code+colors, summarise, total_rev = sum(store_rev))
deseasf <- function(total_rev) rlm(total_rev~month-1, maxit = 50)
models <- ddply(product_aggregate_values, ~county_code + colors, deseasf)
failed <- ddply(models, function(x) !x$converged) 

我收到以下错误:

Error: invalid type (list) for variable 'total_rev'

我认为这可能是因为我使用 product_aggregate_values 作为 data.frame,但是当我尝试使用 daply 创建它并相应地调整代码时,我收到以下错误:

Error in splitter_a(.data, .margins, .expand) : 
'pairlist' object cannot be coerced to type 'integer'
Error in inherits(.data, "split") : object 'models' not found

【问题讨论】:

  • 请花时间做你的榜样reproducible。提供一个示例 data 对象,以便我们知道您正在使用哪些类型的对象。理想情况下,我们可以将您的代码复制/粘贴到 R 中并得到与您相同的错误。这样可以更轻松地为您提供帮助。

标签: r plyr


【解决方案1】:

问题出在您的deseasf 函数中。传入的是子集的 data.frame,而不仅仅是 total_rev 向量。您应该将其用作data=rlm 参数。该功能应该是

deseasf <- function(x) rlm(total_rev~month-1, data=x, maxit = 50)

这实际上仍然会对您的测试数据产生错误,因为您正在尝试估计每个月的系数,而您每个月只有一个值,因此您得到了 rlm() 实际上抱怨的“完美”。希望它适用于您的真实数据。

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    相关资源
    最近更新 更多