Guest User

Untitled

a guest
Jul 22nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. library(mipfp)
  2.  
  3. # Define input data
  4. sex <- c(Male = 23, Female = 27) # n. in each sex category
  5. age <- c(Less18 = 16, Workage = 20, Senior = 14) # age bands
  6. diploma <- c(Level1 = 20, Level2 = 18, Level3 = 6, Level4 = 6)
  7.  
  8. ## Example with 2D (non-crosstabulated) constraints
  9.  
  10. # Define target
  11. target <- list (sex, age, diploma)
  12.  
  13. # Make sure the algorithm knows to which constraint each target refers
  14. descript <- list (1, 2, 3)
  15.  
  16. names <- list (names(sex), names(age), names(diploma))
  17. weight_init <- array (1, c(2,3,4), dimnames = names)
  18.  
  19. # Those younger than 18 can't have advanced degrees
  20. weight_init[, c("Less18"), c("Level3","Level4")] <- 0
  21.  
  22. result <- Ipfp(weight_init, descript, target, iter = 50,
  23. print = TRUE, tol = 1e-5)
  24.  
  25. result$x.hat # print the result
  26.  
  27. ## Exmaple with 3D (crosstabulated) constraints
  28.  
  29. # Define the cross table
  30. cross <- cbind(c(11,5,0,0), c(3,9,4,4), c(6,4,2,2))
  31. rownames (cross) <- names (diploma)
  32. colnames (cross) <- names(age)
  33.  
  34. # The target and descript have to be updated to include the cross table.
  35. # Pay attention to the order of the arguments by declaring a contingency table.
  36. # Then, we can execute the task and run Ipfp again:
  37.  
  38. # defining the new target and associated descript
  39. target <- list(sex, age, diploma, cross)
  40. descript <- list(1, 2, 3, c(3,2))
  41.  
  42. # running the Ipfp function
  43. result <- Ipfp(weight_init, descript, target, iter = 50,
  44. print = TRUE, tol = 1e-5)
  45.  
  46. result$x.hat
Add Comment
Please, Sign In to add comment