如何预测随机效应和固定效应模型?

7

最近我从STATA转到R,对于STATA命令“xtlogit,fe或re”和“predict”的R等效实现有些困难。请问是否能提供一些帮助,调整以下情形:

  data <- read.table("http://people.stern.nyu.edu/wgreene/Econometrics/healthcare.csv",header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)

   require(caret) # for confusionMatrix

   #### subset into test & train according to the panel nature (split  individuals rather then observations)
   nID <- length(unique(data$id))
   p = 0.50# partition

   inTrain <- sample(unique(data$id), round(nID * p), replace=FALSE)

   training <- data[data$id %in% inTrain, ] 

   testing <- data[!data$id %in% inTrain, ] 


   pooled <- glm(WORKING~WHITEC+FEMALE+BLUEC+HHNINC+AGE+AGESQ+EDUC+DOCVIS,data=training, family=binomial(link="logit"))

   prediction.working= round(predict(pooled,newdata=testing,type="response"))

   confusionMatrix(prediction.working,testing$WORKING) # Accuracy between both

此外,我想对随机效应和固定效应进行这些程序。因此,我首先尝试了随机效应,但未成功:
   library(glmmML)
   RE <- glmmML(WORKING~WHITEC+FEMALE+BLUEC+HHNINC+AGE+AGESQ+EDUC+DOCVIS, family=binomial(link="logit"), data=training, cluster=id, method="ghq", n.points=12)



    prediction.working= round(predict(RE,newdata=testing,type="response"))

但似乎这并不起作用。我想请问如何调整glm模型的随机效应和固定效应,以便使用predict函数。


1
我认为你正在寻找条件逻辑模型。请尝试访问http://cran.r-project.org/web/packages/mclogit/mclogit.pdf。 - user227710
1个回答

4

欢迎使用R。我也是STATA的转换者。

这是一个棘手的问题,但它的答案对于理解非常重要。要了解为什么predict函数不能用于glmmML,您需要了解S3方法(参见http://adv-r.had.co.nz/OO-essentials.html)。

让我稍微解释一下。R是一种面向对象的语言。这意味着R中的每个东西都是一个对象(即向量、函数、data.frame)。每个对象都包含属性。属性实际上是关于对象本身的元数据。例如,data.frame中变量的名称就是一种属性。所有对象都具有的一个属性是类。要查看任何对象的类,只需调用class()函数。

很多功能使用S3方法,但并非全部。其中之一是predict函数。这意味着当您调用predict时,predict函数会查看对象的类别。然后根据类别选择要使用的其他预测函数。例如,如果您的对象是lm类,则预测函数将调用predict.lm函数。其他predict函数包括:针对glm类的predict.glm、针对loess类的predict.loess、针对nls类的predict.nls等(要查看完整列表,请阅读predict帮助文档)。不幸的是,没有predict.glmmML函数存在。因此,当您在glmmML类的对象上调用predict函数时,会出现错误。
id <- factor(rep(1:20, rep(5, 20)))
y <- rbinom(100, prob = rep(runif(20), rep(5, 20)), size = 1)
x <- rnorm(100)
dat <- data.frame(y = y, x = x, id = id)
fit.2 <- glmmML(y ~ x, data = dat, cluster = id)
predict(fit.2)
Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "glmmML"
class(fit.2)
 [1] "glmmML"

错误信息非常有用。它基本上表示R尝试使用S3方法,但是没有找到'predict.glmmML'。
至于用户227710建议的mclogit函数,我们来看看。
data(Transport)

fit <- mclogit(
  cbind(resp,suburb)~distance+cost,
  data=Transport
)

class(fit)
[1] "mclogit" "lm"

fit的类别是mclogitlm。使用predict函数可以吗?可以!当你调用predict(fit)时,predict函数首先会寻找predict.mclogit,但是这个函数不存在。它接下来会寻找predict.lm,而这个函数是存在的。


1
感谢您的建议。根据“mclogit”手册,只需指定随机效应即可。请问如何使用该库确定固定效应的预测值?谢谢(THX)。 - Mamba
此外,根据您的示例,还需要创建响应计数,使用 cbind(resp,suburb) - Googme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接