Skip to content

Instantly share code, notes, and snippets.

@Dulani
Last active November 20, 2016 04:49
Show Gist options
  • Save Dulani/39a4a0387fb3ac4fe53a0c3658326c18 to your computer and use it in GitHub Desktop.
Save Dulani/39a4a0387fb3ac4fe53a0c3658326c18 to your computer and use it in GitHub Desktop.
Testing a Three Way IPF with combined margins and separate ones (using the mipfp library)
#Testing a three way IPF with combined and individual marginals.
library(mipfp)
library(dplyr)
pers <- data_frame(
gender = sample(c("m","f"),25,replace = T),
age = rep(1:5,5),
fpl = rep((5:1)/0.5,5),
w = rep(1,25)
)
pers <- pers %>%
mutate(gender = factor(gender)) %>%
mutate(ageCat = standardBreakCut(age,c(-Inf,2,4,Inf),offsetLabels = F)) %>%
mutate(fplCat = standardBreakCut(fpl,c(-Inf,5,Inf),offsetLabels = T))
persCells <- xtabs(data = pers, w ~ gender + ageCat + fplCat)
probs <- data_frame(
gender = c("m","m","m","f","f","f"),
ageCat = c("<2","3-4","5+","<2","3-4","5+"),
fplCat = c("<5","5+","<5","5+","<5","5+"),
w = (1:6) * 3
)
gendProbs <- xtabs(data = probs, w ~ gender)
ageProbs <- xtabs(data = probs, w ~ ageCat)
fplProbs <- xtabs(data = probs, w ~ fplCat)
probCells <- xtabs(data = probs, w ~ gender + ageCat + fplCat)
dimnames(persCells)
dimnames(probCells)
# Using individual marginal distributions ----------------------------------------------------------------
targetDistributions <- list(gendProbs, ageProbs, fplProbs)
targetDistributionVariableOrder <- list(1,2,3)
set.seed(pi)
#CAUTION: TAKE CARE TO ENSURE THAT THE VARIABLES USED HERE ARE IN THE CORRECT ORDER
result <- Estimate(seed = persCells,
target = targetDistributions,
target.list = targetDistributionVariableOrder,
print = T)
print(summary.mipfp(result))
# Using joint/combined marginal distributions ----------------------------------------------------------------
targetDistributions <- list(probCells)
targetDistributionVariableOrder <- list(c(1:3))
set.seed(pi)
#CAUTION: TAKE CARE TO ENSURE THAT THE VARIABLES USED HERE ARE IN THE CORRECT ORDER
result <- Estimate(seed = persCells,
target = targetDistributions,
target.list = targetDistributionVariableOrder,
print = T)
summary(result) #Works in this example, but with more complicated examples, it fails with a weird error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment