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 77 forks source link

BLAS/LAPACK route error #145

Open pradh048 opened 2 years ago

pradh048 commented 2 years ago

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

Robinlovelace commented 2 years ago

I cannot see where the error message is. Could you try creating a 100% reproducible example with the reprex package? It's been a long time since I looked at this package so may be unable to help.