2015-01-20 36 views
2

我试图将模型拟合到数据集中的每个位置。我运行下面的代码(用样品重新镜像我使用的数据):错误:执行rlm时变量的类型(列表)无效

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 
+0

请花时间让您的示例[reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。提供一个样本'数据'对象,以便我们知道您正在使用的对象类型。理想情况下,我们可以将您的代码复制/粘贴到R中,并获得与您相同的错误。这会让你更容易帮助你。 – MrFlick 2015-01-20 19:11:12

回答

0

的问题是在你的deseasf功能。传入的是子集数据帧,而不仅仅是total_rev载体。您应该使用它作为data=参数rlm。这个函数应该是

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

这实际上还是产生错误与您的测试数据,因为你正试图估计系数每个月,你只需要每个月一个值,让你获得AA“完美”,这rlm()实际上抱怨。希望它能处理你的真实数据。

相关问题