Skip to content

Commit

Permalink
Add sparse_model_matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebutts committed Jun 24, 2024
1 parent 2151fec commit 54374fd
Show file tree
Hide file tree
Showing 8 changed files with 936 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Authors@R:
family = "Lenth",
role = "ctb",
email = "russell-lenth@uiowa.edu"))
Imports: stats, graphics, grDevices, tools, utils, methods, numDeriv, nlme, sandwich, Rcpp(>= 1.0.5), dreamerr(>= 1.4.0), stringmagic(>= 1.1.2)
Imports: stats, graphics, grDevices, tools, utils, methods, numDeriv, nlme, sandwich, Rcpp(>= 1.0.5), dreamerr(>= 1.4.0), stringmagic(>= 1.1.2), Matrix
Suggests: knitr, rmarkdown, data.table, plm, MASS, pander, ggplot2, lfe, tinytex, pdftools, emmeans, estimability, AER
LinkingTo: Rcpp
Depends: R(>= 3.5.0)
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export(as.dict)
# setters & getters
exportPattern("^(s|g)etFixest")

# sparse_model_matrix
export(sparse_model_matrix)

# coeftable and co
export(coeftable, se, pvalue, tstat)
S3method(coeftable, default)
Expand Down
32 changes: 32 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@

# fixest 0.12.1

## New Features

- Add the ability to create sparse model matrices from regression objects and formula in a memory-efficient way.

```R
est = feols(mpg ~ drat | cyl, mtcars)

sparse_model_matrix(est, type = "lhs")
#> 32 x 1 sparse Matrix of class "dgCMatrix"
#> mpg
#> [1,] 21.0
#> [2,] 21.0
#> [3,] 22.8

sparse_model_matrix(est, type = c("rhs", "fixef"))
#> 32 x 4 sparse Matrix of class "dgCMatrix"
#> drat cyl::4 cyl::6 cyl::8
#> [1,] 3.90 . 1 .
#> [2,] 3.90 . 1 .
#> [3,] 3.85 1 . .

# Or you can pass a (linear) formula
sparse_model_matrix(mpg ~ i(vs) | gear^cyl, data = mtcars, type = c("rhs", "fixef"))
#> 32 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘vs::0’, ‘vs::1’, ‘gear^cyl::4_6’ ... ]]
#>
#> [1,] 1 . 1 . . . . . . .
#> [2,] 1 . 1 . . . . . . .
#> [3,] . 1 . 1 . . . . . .
```


## Major bugs affecting R versions <= 4.1.2

- require `stringmagic` version >= 1.1.2 to fix several major bugs affecting R versions <= 4.1.2
Expand Down
6 changes: 5 additions & 1 deletion R/miscfuns.R
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ i = function(factor_var, var, ref, keep, bin, ref2, keep2, bin2, ...){
items_name = items
}

if(FROM_FIXEST){
if(FROM_FIXEST || is_sparse){
# Pour avoir des jolis noms c'est un vrai gloubiboulga,
# mais j'ai pas trouve plus simple...
if(IS_INTER_FACTOR){
Expand Down Expand Up @@ -1487,6 +1487,10 @@ i = function(factor_var, var, ref, keep, bin, ref2, keep2, bin2, ...){
fe_colid = to_integer(fe_num[valid_row], sorted = TRUE)

values = if(length(var) == 1) rep(1, length(valid_row)) else var

# Clean names
col_names = sub("^.*__CLEAN__", "", col_names)

res = list(rowid = which(valid_row), values = values,
colid = fe_colid, col_names = col_names)

Expand Down
Loading

0 comments on commit 54374fd

Please sign in to comment.