rpackagenaweighting

R anesrake package producing NA in summary statistics table


The anesrake package is listing in its summary statistics table instead of providing percentages. A reproducible example is pasted below. I cannot figure out what is causing this.

data<-NULL
library(anesrake)
#Dummy data generation
data$Q35Gender<-c(1,1,1,2,2,2,2,2,2,2,2,2)
data$ID<-c(1,2,3,4,5,6,7,8,9,10,11,12)
data<-as.data.frame(data)

#Set weight targets
gentarg<-c(0.5094,0.4906)
names(gentarg)<-c("Male","Female")
targets<-list(gentarg)

#Names
names(targets)<-c("Q35Gender")

#Calculate weights
outsave <- anesrake(targets, data, caseid=data$ID,verbose=FALSE,force1=TRUE,type="nolim",cap = 4)

#Summary to check
summary(outsave)

Solution

  • Anesrake seems to require that the variables are factors or logical, so I've set Q35Gender to factor.

    data<-NULL
    library(anesrake)
    #Dummy data generation
    data$Q35Gender<-c(1,1,1,2,2,2,2,2,2,2,2,2)
    data$ID<-c(1,2,3,4,5,6,7,8,9,10,11,12)
    data<-as.data.frame(data)
    
    #Set as factor 
    
    data$Q35Gender<-as.factor(data$Q35Gender)
    levels(data$Q35Gender)<-c("Male","Female")
    
    #Set weight targets
    gentarg<-c(0.7,0.3)
    names(gentarg)<-c("Male","Female")
    targets<-list(gentarg)
    
    #Names
    names(targets)<-c("Q35Gender")
    
    #Calculate weights
    
    outsave <- anesrake(targets, data, caseid=data$ID,verbose=FALSE,force1=TRUE,type="nolim",cap = 4)
    #Summary - check that it shows the % distribution for unweighted and weighted (not NA)
    summary(outsave)