Tapply只产生缺失值

时间:2014-06-28 22:39:39

标签: r prediction na multi-level tapply

我试图估算一个国家某个城市中天主教徒百分比的估计值,并且我使用多级回归和调查数据的后分层。

该方法适合多级logit并生成因变量的预测概率。然后,它使用样本的后分层对人口普查数据加权概率。

我可以生成初始估计值(这基本上只是调查数据中给定个体的天主教徒的预测概率。)但是,当我尝试使用下面最后一行代码的平均值时,它只返回NA& #39;为每个城市。最初的细胞预测有一些缺失值,但远不及多数。

我不明白为什么我无法生成市政加权平均值,因为我使用了不同的数据。任何帮助将不胜感激。

rm(list=ls(all=TRUE))

library("arm")
library("foreign")

#read in megapoll and attach
ES.data <- read.dta("ES4.dta", convert.underscore = TRUE) 

#read in municipal-level dataset

munilevel <- read.dta("election.dta",convert.underscore = TRUE)
munilevel <- munilevel[order(munilevel$municode),]

#read in Census data
Census <- read.dta("poststratification4.dta",convert.underscore = TRUE)
Census <- Census[order(Census$municode),]
Census$municode <-  match(Census$municode, munilevel$municode)

#Create index variables

#At level of megapoll

ES.data$ur.female <- (ES.data$female *2) + ES.data$ur
ES.data$age.edr <- 6 * (ES.data$age -1) + ES.data$edr

#At census level (same coding as above for all variables)
Census$cur.cfemale <- (Census$cfemale *2) + Census$cur
Census$cage.cedr <- 6 * (Census$cage -1) + Census$cedr

##Municipal level variables 
Census$c.arena<-  munilevel$c.arena[Census$municode]
Census$c.fmln <- munilevel$c.fmln[Census$municode]



#run individual-level opinion model

individual.model1 <- glmer(formula = catholic ~ (1|ur.female) + (1|age) 
+ (1|edr) + (1|age.edr) + (1|municode) + p.arena +p.fmln
 ,data=ES.data, family=binomial(link="logit"))
display(individual.model1)



#examine random effects and standard errors for urban-female
ranef(individual.model1)$ur.female
se.ranef(individual.model1)$ur.female

#create vector of state ranefs and then fill in missing ones
muni.ranefs <- array(NA,c(66,1))
dimnames(muni.ranefs) <- list(c(munilevel$municode),"effect")
for(i in munilevel$municode){
muni.ranefs[i,1] <- ranef(individual.model1)$municode[i,1]
}
muni.ranefs[,1][is.na(muni.ranefs[,1])] <- 0 #set states with missing REs (b/c not in      data) to zero


#create a prediction for each cell in Census data
cellpred1 <- invlogit(fixef(individual.model1)["(Intercept)"]
    +ranef(individual.model1)$ur.female[Census$cur.cfemale,1]
    +ranef(individual.model1)$age[Census$cage,1]
    +ranef(individual.model1)$edr[Census$cedr,1]
    +ranef(individual.model1)$age.edr[Census$cage.cedr,1]
    +muni.ranefs[Census$municode,1]
    +(fixef(individual.model1)["p.fmln"] *Census$c.fmln) # municipal level 
    +(fixef(individual.model1)["p.arena"] *Census$c.arena)) # municipal level



#weights the prediction by the freq of cell                                       
cellpredweighted1 <- cellpred1 * Census$cpercent.muni

#calculates the percent within each municipality (weighted average of responses)
munipred <- 100* as.vector(tapply(cellpredweighted1, Census$municode, sum))
munipred

1 个答案:

答案 0 :(得分:1)

如果没有数据,大量的代码是完全冗余的!我猜您在对象NA中有cellpredweighted1个,默认情况下sum()会将NA传播到答案,因为如果向量的一个或多个元素是{{1}然后根据定义,这些元素的总和也是NA

如果上述情况属于上述情况,那么只需将NA添加到na.rm = TRUE来电即可解决问题。

tapply()

你应该问自己为什么在这个阶段有tapply(cellpredweighted1, Census$municode, sum, na.rm = TRUE) ,如果这些是由于过程早期的错误造成的。