R t仅产生缺失值

R t仅产生缺失值,r,prediction,na,multi-level,tapply,R,Prediction,Na,Multi Level,Tapply,我试图估算出一个国家某个城市中天主教徒的百分比,我使用了多层次回归和调查数据的后分层 该方法适用于多水平logit,并生成因变量的预测概率。然后,它使用样本的后分层对概率进行加权,以统计数据 我可以生成初始估计值(这基本上只是调查数据中给定个人成为天主教教徒的预测概率)。但是,当我尝试取下最后一行代码的平均值时,它只返回每个城市的NA。最初的单元格预测有一些缺失值,但远未达到多数 我不明白为什么我不能生成市政加权平均数,因为我使用不同的数据遵循了这个过程。任何帮助都将不胜感激 rm(list=l

我试图估算出一个国家某个城市中天主教徒的百分比,我使用了多层次回归和调查数据的后分层

该方法适用于多水平logit,并生成因变量的预测概率。然后,它使用样本的后分层对概率进行加权,以统计数据

我可以生成初始估计值(这基本上只是调查数据中给定个人成为天主教教徒的预测概率)。但是,当我尝试取下最后一行代码的平均值时,它只返回每个城市的NA。最初的单元格预测有一些缺失值,但远未达到多数

我不明白为什么我不能生成市政加权平均数,因为我使用不同的数据遵循了这个过程。任何帮助都将不胜感激

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
rm(list=ls(all=TRUE))
图书馆(“arm”)
图书馆(“外国”)
#在megapoll中读取并附加

ES.data如果没有数据,大量的代码是完全冗余的!我假设对象
cellPredwerted1
中有
NA
s,默认情况下
sum()
传播
NA
s到答案,因为如果向量的一个或多个元素是
NA
,那么根据定义,这些元素的总和也是
NA

如果这里是上述情况,那么只需将
na.rm=TRUE
添加到
tapply()
调用中就可以解决问题

tapply(cellpredweighted1, Census$municode, sum, na.rm = TRUE)
您应该问问自己,为什么在这个阶段会出现
NA
s,以及这些是否是由于流程早期的错误造成的