[R] Generating input population for microsimulation
Dielia Ba
dbmass18 at gmail.com
Mon Jun 20 21:35:57 CEST 2016
Hi everyone,
I really need your help !!
I am currently working on a micro-simulation project and I cannot find a
package in R that does what I want.
Here is the picture: I have macroeconomic variables such as
income,consumption, household weight and I calculated the elasticities
already.
I also have two other data sets with income growth rates and population
projection. What I want is to create a data set with an income variable for
each year (from 2014 to 2030) and the same thing for consumption, based on
the existing patterns in the input data sets.
Do I really have to code my own R package to perform the micro- simulation
?
FYI: I tried almost all R packages related to micro-simulation or
simulation ( mostly spatial - demographic and health- survival designed
tools)
I would really appreciate any constructive comments and remarks.
Thanks a lot,
Dielia
Le mardi 13 décembre 2011 18:08:21 UTC-5, Emma Thomas a écrit :
>
> Hi all,
>
> I've been struggling with some code and was wondering if you all could
> help.
>
> I am trying to generate a theoretical population of P people who are
> housed within X different units. Each unit follows the same structure- 10
> people per unit, 8 of whom are junior and two of whom are senior. I'd like
> to create a unit ID and a unique identifier for each person (person ID,
> PID) in the population so that I have a matrix that looks like:
>
> unit_id pid senior
> [1,] 1 1 0
> [2,] 1 2 0
> [3,] 1 3 0
> [4,] 1 4 0
> [5,] 1 5 0
> [6,] 1 6 0
> [7,] 1 7 0
> [8,] 1 8 0
> [9,] 1 9 1
> [10,] 1 10 1
> ...
>
> I came up with the following code, but am having some trouble getting it
> to populate my matrix the way I'd like.
>
> world <- function(units, pop_size, unit_size){
> pid <- rep(0,pop_size) #person ID
> senior <- rep(0,pop_size) #senior in charge
> unit_id <- rep(0,pop_size) #unit ID
>
> for (i in 1:pop_size){
> for (f in 1:units) {
> senior[i] = sample(c(1,1,0,0,0,0,0,0,0,0), 1, replace = FALSE)
> pid[i] = sample(c(1:10), 1, replace = FALSE)
> unit_id[i] <- f
> }}
> data <- cbind(unit_id, pid, senior)
>
> return(data)
> }
>
> world(units = 10,pop_size = 100, unit_size = 10) #call the function
>
> The output looks like:
> unit_id pid senior
> [1,] 10 7 0
> [2,] 10 4 0
> [3,] 10 10 0
> [4,] 10 9 1
> [5,] 10 10 0
> [6,] 10 1 1
> ...
>
> but what I really want is to generate is 10 different units with two
> seniors per unit, and with each person in the population having a unique
> identifier.
>
> I thought a nested for loop was one way to go about creating my data set
> of people and families, but obviously I'm doing something (or many things)
> wrong. Any suggestions on how to fix this? I had been focusing on creating
> a person and assigning them to a unit, but perhaps I should create the
> units and then populate the units with people?
>
> Thanks so much in advance.
>
> Emma
>
> ______________________________________________
> R-h... at r-project.org <javascript:> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list