开发者

How to grab coefficients with R when estimating a Zero Inflation Model

Probably pretty easy, but I want to know, how to grab coefficients when using the zeroinfl command?

treatment <- factor(rep(c(1, 2), c(43, 41)), 
    levels = c(1, 2),labels = c("placebo", "treated"))
improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)),
    levels = c(1, 2, 3),labels = c("none", "some", "marked"))

numberofdrugs <- rpois(84, 2)
healthvalue <- rpois(84,0.5)
y <- data.frame(healthvalue,numberofdrugs, tre开发者_运维问答atment, improved)

require(pscl)
ZIP<-zeroinfl(healthvalue~numberofdrugs+treatment+improved, y)

summary(ZIP)

I usually use ZIP$coef[1] to grab a coefficient, but unfortunately here you grab a whole bunch. So how can I grab one single coeficients from a ZIP model?


Use the coef extraction function to list all coefficients in one long vector, and then you can use single index notation to select them:

coef(ZIP)[1]

count_(Intercept) 
        0.1128742 

Alternatively, you need to select which model you want to get the coefficients from first:

ZIP$coef$count[1]

(Intercept) 
  0.1128742

ZIP$coef[[1]][1]

(Intercept) 
  0.1128742 


If you wanted to get fancy you could split the coefficients into a list:

clist <- function(m) {
  cc <- coef(m)
  ptype <- gsub("_.+$","",names(cc))
  ss <- split(cc,ptype)
  lapply(ss, function(x) names(x) <- gsub("^.*_","",names(x)))
}
> clist(ZIP)
$count
     (Intercept)    numberofdrugs treatmenttreated     improvedsome 
     -1.16112045       0.16126724      -0.07200549      -0.34807344 
  improvedmarked 
      0.23593220 

$zero
     (Intercept)    numberofdrugs treatmenttreated     improvedsome 
        7.509235       -14.449669       -58.644743        -8.060501 
  improvedmarked 
       58.034805 


c1 <- clist(ZIP)
c1$count["numberofdrugs"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜