Git Product home page Git Product logo

robinlovelace / spatial-microsim-book Goto Github PK

View Code? Open in Web Editor NEW
112.0 19.0 77.0 29.01 MB

Code, data and prose of the book: Spatial Microsimulation with R

Home Page: https://www.crcpress.com/Spatial-Microsimulation-with-R/Lovelace-Dumont/p/book/9781498711548

License: MIT License

R 41.67% Stata 0.49% CSS 1.00% JavaScript 0.09% TeX 29.20% NetLogo 27.18% Makefile 0.11% Shell 0.26%
spatial spatial-microsimulation r simulation urban-planning agent-based-modeling netlogo

spatial-microsim-book's Introduction

Spatial Microsimulation with R

DOI Build Status

This repository hosts the code and data used in Spatial Microsimulation with R, a book by Robin Lovelace and Morgane Dumont, (with chapter 10 contributed by Johan Barthélemy, chapter 11 contributed by Richard Ellison and David Hensher and chapter 12 contributed by Maja Založnik).

The book is now published and is available from CRC Press.

We hope to continue to update the book as methods evolve and we encourage contributions on any part of the book, including:

  • Improvements to the text, e.g. clarifying unclear sentences, fixing typos (see guidance from Yihui Xie).
  • Changes to the code, e.g. to do things in a more efficient way.
  • Suggestions on content (see the project's issue tracker).

The latest version of the book can be viewed at the book's homepage at spatial-microsim-book.robinlovelace.net. Anyone can contribute to this book here.

spatial-microsim-book's People

Contributors

emunozh avatar jgleeson avatar jojo- avatar krlmlr avatar majazaloznik avatar philmikejones avatar richardellison avatar robinlovelace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spatial-microsim-book's Issues

ipfp function generate NaN weights

I am using the ipfp package to generate a synthetic population for 100 zones, a total of 523,930 people. My sample size is 24,000. I have 4 constraints: age (5 levels), sex (2 levels), race (4 levels) and employment status (3 levels)。

I have made sure that the totals across different constraints are consistent. I basically use the same code that's provided by the code to do the microsimulation.

weights_ga <- array(NA, dim=c(nrow(ind_ga),nrow(cons_ga)))

ga_ind_agg <- matrix(colSums(ga_ind_cat), nrow(cons_ga), ncol(cons_ga), byrow = T)

# Iterative proportional fitting (IPF) stage
library(ipfp) # load the ipfp package -may need install.packages("ipfp")
cons_ga <- apply(cons_ga, 2, as.numeric) # convert the constraints to 'numeric'

ga_ind_catt <- t(ga_ind_cat) # transpose the dummy variables for ipfp

x0 <- rep(1, nrow(ind_ga)) # set the initial weights

weights_ga_f <- apply(cons_ga, 1, function(x) ipfp(x, ga_ind_catt, x0))

Created on 2018-08-16 by the reprex
package
(v0.2.0).

``
And my weights looks like this
screen shot 2018-08-16 at 6 27 35 pm

What could be possibly going wrong? I have several 0s in the constraints, but there are not many of them. I tried to replace them to 0.0001, but still doesn't work.

fresh compile errors

build.R:
install.packages("rgeos", "mipfp", "rgdal", "gridExtra")
uncomment lines 4128 to 4130 in book.Rmd (install GREGWT)
missing figure (row 863) figures/msim-schema.png"
missing file (row 4045) cache-smsim-in-R.RData"
(row 5133) error in "R/CakeMapInts.R" - data.frame different lenghts of columns(ints_df)

GREGWT example

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

BLAS/LAPACK route error

I have been trying to implement the IPF algorithm from SMiR's Chapter 7 on a dataset I created using the American Community Survey (ACS). I have been using almost the similar data structure as the CakeMap example, but I get the following error message:

Error in ipfp(x, ind_catt, x0, maxit = 100) :
BLAS/LAPACK routine 'DGEMV DGER DSBMV DS' gave error code -6

I have attached the R code and necessary files here. I would be extremely grateful if someone could help me troubleshoot the issue.

`
ind <- read.csv("ind.csv")
cons <- read.csv("cons.csv")

ind <- ind %>%
select(eap_eligible, vehicle, home_ownership, hh_income_cat, race)
cons <- cons[, -1]

con1 <- cons[1:10] # load the tenure/race constraint
con2 <- cons[11:12] # load the car/no car constraint
con3 <- cons[13:28] # income class

cat_labs <- names(cons) # category names, from correct from cons.R

create new homeownership/race variable

HR <- paste0(ind$home_ownership, ind$race)
unique(HR)

matrix for constraint 1 - tenure/race

m1 <- model.matrix(~HR-1)
head(cons)
head(m1)
m1_names <- c("Asian_Owner", "Black_Owner", "Indigenous_Owner", "Multiracial_Owner", "White_Owner",
"Asian_Renter", "Black_Renter", "Indigenous_Renter", "Multiracial_Renter", "White_Renter")
colnames(m1) <- names(m1_names)
head(m1)
summary(rowSums(m1))

matrix for con2 (car ownership)

ind$vehicle <- as.character(ind$vehicle)
m2 <- model.matrix(~ind$vehicle-1)
head(m2)
summary(m2)

matrix for con3 (income category)

ind$hh_income_cat<- as.character(ind$hh_income_cat)
m3 <- model.matrix(~ind$hh_income_cat-1)
head(m3)
names(cons)

ind_cat <- data.frame(cbind(m1, m2, m3))
rm(m1, m2, m3)

names(ind_cat) <- cat_labs
names(ind_cat)[1:10] <- m1_names
head(ind_cat)

Check constraint totals - should be true

sum(ind_cat[,1:ncol(con1)]) == nrow(ind) # is the number in each category correct?
sum(ind_cat[,ncol(con1)+1:ncol(con2)]) == nrow(ind)

weights <- array(NA, dim=c(nrow(ind),nrow(cons)))

ind_agg <- matrix(colSums(ind_cat), nrow(cons), ncol(cons), byrow = T)

Iterative proportional fitting (IPF) stage

library(ipfp) # load the ipfp package -may need install.packages("ipfp")

cons <- apply(cons, 2, as.numeric) # convert the constraints to 'numeric'
ind_catt <- t(ind_cat) # transpose the dummy variables for ipfp
x0 <- rep(1, nrow(ind)) # set the initial weights

weights <- apply(cons, 1, function(x) ipfp(x, ind_catt, x0, maxit = 100))`

cons.csv
ind.csv

Broken code in GREGWT section

This code fails here:

for(area in seq(dim(age)[1])){
    gregwt = GREGWT(data_in, area_code = area)
    fw <- gregwt$final_weights
    fweights <- c(fweights, fw)
    ## Estimate income
    sum.income <- sum(fw * ind$income)
    cap.income <- sum(fw * ind$income / sum(fw))
    Result[area,] <- c(area, sum.income, cap.income)
}

## Error in Tx_complete[i, ] : subscript out of bounds

Just not running the code is not a good solution! f02fb39

Please take a look @emunozh

New version of GREGWT is looking good.

Problem with int_pp()

There is a problem with the int_pp function (PP integerisation) due to the limitation of summary() by maxsum argument, whose default is 100 for factors.
Try int_pp(x=rep(1.3333,100000)) to reproduce the error.

My patch is:

 # Aggregate the result into a weight vector
   xsumm <- summary(as.factor(xs), maxsum = length(levels(as.factor(xs)))+1)

rjava reference not found in bib

Warning: "pandoc-citeproc: reference rjava not found"

When knitting 13-smsim-for-abm, presumably missing a label in the bib.

File still knits, but thought you should know.

Error in code: p6 05-sms-in-r

On page 6:
ind_agg0 <- t(apply(cons, 1, function(x) x^0 * ind_agg))
Returns this error:
Error in x^0 : non-numeric argument to binary operator

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.