require(SDaA)
data(agpop, agsrs, agstrat, package="SDaA") #loads ag datasets from SDaA
str(agpop)
str(agsrs)
str(agstrat)
agsrs$w.srs <- nrow(agpop)/nrow(agsrs) # add weights
# adds region to agsrs
state.region <- data.frame(xtabs(weight~state+region, data=agstrat))
state.region <- subset(state.region, Freq>0)
agsrs <- merge(agsrs, state.region[,1:2], by="state", all.x=TRUE)
# simulate statistical matching framework
A <- agsrs[, c("region", "acres82", "acres87", "w.srs")]
B <- agstrat[, c("region", "acres82", "acres92", "weight")]
# simplest call to rankNND.hotdeck()
# UNCONSTRAINED case
out.1 <- rankNND.hotdeck(data.rec=A, data.don=B, var.rec="acres82")
fused.1 <- create.fused(data.rec=A, data.don=B,
mtc.ids=out.1$mtc.ids, z.vars="acres92")
head(fused.1)
# call to rankNND.hotdeck() with usage of weights
# UNCONSTRAINED case
out.2 <- rankNND.hotdeck(data.rec=A, data.don=B, var.rec="acres82",
weight.rec="w.srs", weight.don="weight")
fused.2 <- create.fused(data.rec=A, data.don=B,
mtc.ids=out.2$mtc.ids, z.vars="acres92")
head(fused.2)
# call to rankNND.hotdeck() with usage of weights and don classes
# UNCONSTRAINED case
out.3 <- rankNND.hotdeck(data.rec=A, data.don=B, var.rec="acres82",
don.class="region", weight.rec="w.srs", weight.don="weight")
fused.3 <- create.fused(data.rec=A, data.don=B,
mtc.ids=out.3$mtc.ids, z.vars="acres92")
head(fused.3)
# call to rankNND.hotdeck()
# CONSTRAINED case
out.1c <- rankNND.hotdeck(data.rec=A, data.don=B, var.rec="acres82",
constrained=TRUE, constr.alg="Hungarian")
fused.1c <- create.fused(data.rec=A, data.don=B,
mtc.ids=out.1c$mtc.ids, z.vars="acres92")
head(fused.1c)
# call to rankNND.hotdeck() with usage of weights
# CONSTRAINED case
out.2c <- rankNND.hotdeck(data.rec=A, data.don=B, var.rec="acres82",
weight.rec="w.srs", weight.don="weight",
constrained=TRUE, constr.alg="Hungarian")
fused.2c <- create.fused(data.rec=A, data.don=B,
mtc.ids=out.2c$mtc.ids, z.vars="acres92")
head(fused.2c)
Run the code above in your browser using DataLab