应用分组模型分组 [英] Apply grouped model group-wise

查看:138
本文介绍了应用分组模型分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题非常类似于这一个,但问题我面对的是一个扭曲,这些答案没有解决。具体来说,我估计一个空间模型, y = rho * lw * y + X * beta 。由于观察结果与矩阵 lw 相关,所以我必须同时将模型应用于整个 X 矩阵。因为这些答案是按行行的,它们不适用。

My question is very similar to this one, but the problem I am facing has a twist that those answers do not address. Specifically, I am estimating a spatial model, y=rho * lw * y + X *beta. Because the observations are related by the matrix lw, I must apply the model to the entire X matrix simultaneously. Because those answers operate row-wise, they do not apply.

这里是MWE数据,由三组中的二十分和空间权重矩阵组成:

Here is MWE data, consisting of twenty points across three groups and a spatial weights matrix:

library(spdep)
#Coordinates
pointcoords <- data.frame(x = runif(n=20, min =10, max = 100), y = runif(n=20, min = 10, max = 100), ID = as.character(1:20))
pointsSP <- SpatialPoints(pointcoords[,1:2])
# Weights matrix
lw <- nb2listw(knn2nb(knearneigh(pointsSP, k = 4, RANN = FALSE), 
                      row.names = pointcoords$ID))

# Data
MyData <- data.frame(ID = rep(1:20, each = 3),
                     Group = rep(1:3, times = 20),
                     DV = rnorm(60),IV = rnorm(60))

I可以通过 Group dplyr

library(dplyr)
models <- MyData %>% group_by(Group) %>% 
  do(lm = lm(DV ~ IV, data = .), 
     sar = lagsarlm(DV ~ IV, data = ., listw = lw))

通过此答案预测新数据将按行顺序运行,对 lm正常工作对象

Predicting to new data with this answer operates on a row-wise basis, working fine for the lm objects,

MyData2 <- data.frame(ID = rep(1:20, each = 3),
                      Group = rep(1:3, times = 20),
                      IV = rnorm(60))

MyData2 %>% left_join(models) %>% rowwise %>%
  mutate(lmPred = predict(lm, newdata = list("IV" = IV))) %>% head()
#Joining by: "Group"
#Source: local data frame [6 x 6]
#Groups: 

#  ID Group         IV      lm        sar      lmPred
#1  1     1 -0.8930794 <S3:lm> <S3:sarlm> -0.21378814
#2  1     2 -1.6637963 <S3:lm> <S3:sarlm>  0.42547796
#3  1     3  0.5243841 <S3:lm> <S3:sarlm> -0.23372996
#4  2     1 -0.1956969 <S3:lm> <S3:sarlm> -0.20860280
#5  2     2  0.8149920 <S3:lm> <S3:sarlm>  0.14771431
#6  2     3 -0.3000439 <S3:lm> <S3:sarlm>  0.05082524

但不适用于 sar 型号:

MyData2 %>% left_join(models) %>% rowwise %>%
  mutate(sarPred = predict(sar, newdata = list("IV" = IV), listw=lw)) %>% head()
#Joining by: "Group"
#Error in if (nrow(newdata) != length(listw$neighbours)) stop("mismatch between newdata and spatial weights") : 
  argument is of length zero

我认为应该有一个更好的方式来做到这一点,没有加入模型到每一行。另外,如果您有几个或更改的预测变量,则为 newdata 创建列表对象将不起作用。似乎 dplyr 的方式应该是这样的:

I think there should be a better way of doing this, without joining the model to every row. Also, creating a list object for newdata won't work if you have several or changing predictor variables. It seems that the dplyr way should be something like this:

MyData2 %>% group_by(Group) %>%
  mutate(sarPred = predict(models$sar[[Group]],  newdata = ., listw=lw))

但是, [[Group]] 索引不太正确。

推荐答案

我把它放在那里,因为它确实做了我想要的,即使它需要使用 for loop(gasp)

I'm putting this out there because it does do what I want it to, even if it needs to use a for loop (gasp)

predictobj <- list()
for(i in models$Group){
  predictobj[[i]] <- predict.sarlm(models$sar[[i]], 
                                   newdata = filter(MyData2, Group == i),
                                   listw = lw)
}

任何人都有一个 dplyr 解决方案?

Anybody have a dplyr solution?

这篇关于应用分组模型分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆