Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(mipfp)
- # Define input data
- sex <- c(Male = 23, Female = 27) # n. in each sex category
- age <- c(Less18 = 16, Workage = 20, Senior = 14) # age bands
- diploma <- c(Level1 = 20, Level2 = 18, Level3 = 6, Level4 = 6)
- ## Example with 2D (non-crosstabulated) constraints
- # Define target
- target <- list (sex, age, diploma)
- # Make sure the algorithm knows to which constraint each target refers
- descript <- list (1, 2, 3)
- names <- list (names(sex), names(age), names(diploma))
- weight_init <- array (1, c(2,3,4), dimnames = names)
- # Those younger than 18 can't have advanced degrees
- weight_init[, c("Less18"), c("Level3","Level4")] <- 0
- result <- Ipfp(weight_init, descript, target, iter = 50,
- print = TRUE, tol = 1e-5)
- result$x.hat # print the result
- ## Exmaple with 3D (crosstabulated) constraints
- # Define the cross table
- cross <- cbind(c(11,5,0,0), c(3,9,4,4), c(6,4,2,2))
- rownames (cross) <- names (diploma)
- colnames (cross) <- names(age)
- # The target and descript have to be updated to include the cross table.
- # Pay attention to the order of the arguments by declaring a contingency table.
- # Then, we can execute the task and run Ipfp again:
- # defining the new target and associated descript
- target <- list(sex, age, diploma, cross)
- descript <- list(1, 2, 3, c(3,2))
- # running the Ipfp function
- result <- Ipfp(weight_init, descript, target, iter = 50,
- print = TRUE, tol = 1e-5)
- result$x.hat
Add Comment
Please, Sign In to add comment