Robinlovelace / spatial-microsim-book

Code, data and prose of the book: Spatial Microsimulation with R
https://www.crcpress.com/Spatial-Microsimulation-with-R/Lovelace-Dumont/p/book/9781498711548
MIT License
114 stars 76 forks source link

GREGWT example #122

Closed tyokota closed 7 years ago

tyokota commented 8 years ago

The GREGWT example on pg. 112 isn't running. data_in <- prepareData(cbind(age, sex), ind, census_area_id = F, breaks = c(3)) Error in[.data.frame(Tx, names(Tx)[i:b]) : undefined columns selected

Robinlovelace commented 7 years ago

Any ideas @emunozh ? Just reproduced this on a new system starting from scratch. Thank for reporting @tyokota and sorry for the slow response.

emunozh commented 7 years ago

As a reference I am including the example here from 06-alternative-approaches.Rmd

# Install GREGWT (uncomment/alter as appropriate)
# devtools::install_github("emunozh/GREGWT")
# load the library (0.7.0)
library('GREGWT')

# Load the data from csv files stored under data
age = read.csv("data/SimpleWorld/age.csv")
sex = read.csv("data/SimpleWorld/sex.csv")
ind = read.csv("data/SimpleWorld/ind-full.csv")
# Make categories for age
ind$age <- cut(ind$age, breaks=c(0, 49, Inf),
               labels = c("a0.49", "a.50."))
# Add initial weights to survey
ind$w <- vector(mode = "numeric", length=dim(ind)[1]) + 1

# prepare simulation data using GREGWT::prepareData
data_in <- prepareData(cbind(age, sex),
                  ind, census_area_id = F, breaks = c(3))

There have been some modifications in the GREGWT code. Plese note that the example listed above referes the reader to the library version 0.7.0. Plese make sure that you are uning the right version. I just run the code with 0.7.0 and it runs. Let me know if you need version 0.7.0. Here is the output of my session:

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] GREGWT_0.7.0   nvimcom_0.9-21

loaded via a namespace (and not attached):
[1] tools_3.3.1

Now, if you want to run it with the lattest version (0.7.3 just updated the repo) you have to make a small modification to the code:

# prepare simulation data using GREGWT::prepareData
data_in <- prepareData(cbind(age, sex),
                  ind, census_area_id = F, breaks = c(2))

It was confusing why the brak was on 3 (it assumed a column of area ID), now the break is on column 2, as it should be.

Plese let me know if you have other difficulties with the library. I haven`t been able to take the library out of beta...