conroylau / lpinfer

lpinfer: An R Package for Inference in Linear Programs
GNU General Public License v3.0
3 stars 5 forks source link

Suggestions: Adding support for sparse matrices #106

Closed nadavkun closed 3 years ago

nadavkun commented 3 years ago

Hi, As some of the methods in the package are used for inference of large-scale linear programming problem I think it would be useful to allow the lpmodel function to accept sparse matrices

Nadav

conroylau commented 3 years ago

Thanks for your suggestion Nadav. I have added support for sparseMatrix in the lpmodel object. An example is as follows.

rm(list = ls())
library(lpinfer)
library(Matrix)
source("./inst/example/dgp_mixedlogit.R")

set.seed(1)
dgp <- mixedlogit_dgp(dimw = 4, dimv = 16)
df <- mixedlogit_draw(dgp, n = 4000)
lpm <- lpmodel(A.obs = as(mixedlogit_Aobs(dgp), "sparseMatrix"),
               beta.obs = function(d) as(mixedlogit_betaobs(d, dgp), "sparseMatrix"),
               A.shp = t(as(rep(1, nrow(dgp$vdist)), "sparseMatrix")),
               beta.shp = 1,
               A.tgt = t(as(mixedlogit_Atgt_dfelast(dgp, w2eval = 1, eeval = -1),
                          "sparseMatrix")))

set.seed(1)
fsst(data = df, lpmodel = lpm, beta.tgt = .25, R = 500)
#> p-value (by data-driven 'lambda'): 0.01
nadavkun commented 3 years ago

Hi Conroy, I haven't had the time to check the new improvements, but just wanted to point out that in the FSST function there some cases in which you use as.matrix() function on the lpm model matrices, which turn them back into non-sparse matrices. I don't know if you changed them as well, but if not, I think it's worthwhile to do this as well.

Best, Nadav

conroylau commented 3 years ago

Hi Nadav,

Thanks for your feedback. It is handled, except for a few places when we pass the matrices to some model objects in the gurobi function. For example, model$obj cannot be a sparseMatrix, so I coerce it as a matrix there.