When I was executing code on line 103-106 of the Lalonde_replication.r (See https://dataverse.harvard.edu/dataset.xhtml?persistentId=hdl:1902.1/16077) :
Entropy Balancing (with trimmed weights)
out.ebtr <- ebalance.trim(ebalanceobj=out.eb,)
I found the following error: Error in if (loss.old <= loss.new) { : missing value where TRUE/FALSE needed.
I refer to other similar questions on the website,but there is no “NA” in the original code.
Moreover, the original code does not have the following problems :
The double equals operator can't tolerate an NA on either side. If I define: x = NA and then do an if (x == NA){ ... } then this error will be thrown at runtime when the parser examines the left hand side of the double equals. To remedy this error, make sure every variable in your conditional is not NA using is.na(your_variable). – Eric Leschinski Apr 9 at 1:26
The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.
if(NA){}## Error in if (NA) { : missing value where TRUE/FALSE needed
This can happen accidentally as the results of calculations:
if(TRUE && sqrt(-1)) {} ## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed. To test whether an object is missing use is.na(x) rather than x == NA.How can I solve this problem? Tanks a lot.
The original code is as follows:
#code to replicate analysis of Lalonde data
rm(list=ls())
library(foreign)
library(Matching)
library(survey)
library(lattice)
# package that implements entropy balancing
library(ebal)
dat <- read.table("cps1re74.csv",header=T)
# unemployed
dat$u74 <- as.numeric(dat$re74==0)
dat$u75 <- as.numeric(dat$re75==0)
# covars
covars <- c("age","educ","black","hispan","married","nodegree","re74","re75","u74","u75")
# compute all interactions
X <- as.matrix(dat[,covars])
XX <- matrixmaker(X)
# prepare data: exclude non-sensical and co-linear vars, code treatment and outcome, change names
out <- c("black.black","nodegree.nodegree","nodegree.educ","married.married","hispan.hispan","hispan.black",
"u75.u75","u74.u74","u74.re74","u75.re75","u74.re74","u75.re75","re74.re74","re75.re75","re75.re74")
XX <- XX[,(colnames(XX) %in% out)==F]
dat <- data.frame(dat[,(names(dat) %in% covars)==F],XX)
covars <- names(dat)[-which((names(dat) %in% c("treat","re78")))]
X <- dat[,covars]
X <- as.matrix(X)
colnames(X) <-gsub(".age","*Age",colnames(X))
colnames(X) <-gsub(".educ*","*Schooling",colnames(X))
colnames(X) <-gsub(".black","*Black",colnames(X))
colnames(X) <-gsub(".hispan","*Hispanic",colnames(X))
colnames(X) <-gsub(".married","*Married",colnames(X))
colnames(X) <-gsub(".re74","*Earnings 1974",colnames(X))
colnames(X) <-gsub(".re75","*Earnings 1975",colnames(X))
colnames(X) <-gsub(".u74","*Unemployed 1974",colnames(X))
colnames(X) <-gsub(".u75","*Unemployed 1975",colnames(X))
colnames(X) <-gsub(".nodegree","*HS Dropout",colnames(X))
colnames(X) <-gsub("age","Age",colnames(X))
colnames(X) <-gsub("educ","Schooling",colnames(X))
colnames(X) <-gsub("black","Black",colnames(X))
colnames(X) <-gsub("hispan","Hispanic",colnames(X))
colnames(X) <-gsub("married","Married",colnames(X))
colnames(X) <-gsub("re74","Earnings 1974",colnames(X))
colnames(X) <-gsub("re75","Earnings 1975",colnames(X))
colnames(X) <-gsub("u74","Unemployed 1974",colnames(X))
colnames(X) <-gsub("u75","Unemployed 1975",colnames(X))
colnames(X) <-gsub("nodegree","HS Dropout",colnames(X))
dat <- data.frame(dat,X)
Y <- dat$re78
W <- dat$tr
# balance before matching
bout.nm <- MatchBalance(W~X,match.out = NULL,ks=FALSE)
bal.nm <- baltest.collect(matchbal.out=bout.nm ,var.names=colnames(X),after=FALSE)
round(bal.nm,3)
# Maha Dist Matching
mout.maha <- Match(Y,W,X,BiasAdjust=F,estimand="ATT",M=1)
summary(mout.maha)
bout.maha <- MatchBalance(W~X,match.out = mout.maha,ks=FALSE)
bal.maha <- baltest.collect(matchbal.out=bout.maha ,var.names=colnames(X),after=TRUE)
round(bal.maha,3)
# Genetic Matching ATT
g.weights <- GenMatch(Tr=W, X=X, BalanceMatrix=X, estimand="ATT", M=1,print.level=0)
mout.gm <- Match(Y,W,X,BiasAdjust=F,Weight.matrix=g.weights,estimand="ATT",M=1)
summary(mout.gm)
bout.gm <- MatchBalance(W~X,match.out = mout.gm,print.level=0,ks=FALSE)
bal.gm <- baltest.collect(matchbal.out=bout.gm,var.names=colnames(X),after=TRUE)
round(bal.gm,3)
## Logistic PS weighting + matching
PS <- glm(W~X,family=binomial(link=logit))$fitted
PSM <- PS
PS <- PS[W==0]
PS <- PS/(1-PS)
# PS Matching
mout.psm <- Match(Y,W,X=PSM,BiasAdjust=F,estimand="ATT",M=1)
summary(mout.psm)
bout.psm <- MatchBalance(W~X,match.out = mout.psm,print.level=0,ks=FALSE)
bal.psm <- baltest.collect(matchbal.out=bout.psm,var.names=colnames(X),after=TRUE)
round(bal.psm,3)
# PS Weighting
bout.psw <- MatchBalance(W~X,weights=c(W[W==1],PS),ks=FALSE)
bal.psw <- baltest.collect(matchbal.out=bout.psw,var.names=colnames(X),after=FALSE)
round(bal.psw,2)
# Entropy Balancing
out.eb <- ebalance(
Treatment=W,
X=X
)