Skip to content

Commit 94e3bd6

Browse files
committed
v0.9.6
1 parent e5ab2d7 commit 94e3bd6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+217
-76
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: capybara
22
Type: Package
33
Title: Fast and Memory Efficient Fitting of Linear Models with High-Dimensional
44
Fixed Effects
5-
Version: 0.9.4
5+
Version: 0.9.6
66
Authors@R: c(
77
person(
88
given = "Mauricio",

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# capybara 0.9.6
2+
3+
* Calculates the rank of matrix X based on singular value decomposition instead
4+
of QR decomposition. This is more efficient and numerically stable.
5+
6+
# capybara 0.9.5
7+
8+
* Fixes and expands the 'weights' argument in the `fe*()` functions to allow for
9+
different types of weights. The default is still `NULL` (i.e., all weights
10+
equal to 1). The argument now admits weights passed as `weights = ~cyl`,
11+
`weights = mtcars$cyl`, or `w <- mtcars$cyl; weights = w`.
12+
113
# capybara 0.9.4
214

315
* Allows to estimate models without fixed effects.

R/cpp11.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Generated by cpp11: do not edit by hand
22

3+
check_linear_dependence_svd_ <- function(y, x, p) {
4+
.Call(`_capybara_check_linear_dependence_svd_`, y, x, p)
5+
}
6+
37
center_variables_r_ <- function(V_r, w_r, klist, tol, max_iter, iter_interrupt, iter_ssr) {
48
.Call(`_capybara_center_variables_r_`, V_r, w_r, klist, tol, max_iter, iter_interrupt, iter_ssr)
59
}

R/feglm.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,22 @@ feglm <- function(
197197
model_response_(data, formula)
198198

199199
# Check for linear dependence ----
200-
# check_linear_dependence_(x, p)
201-
check_linear_dependence_(cbind(y, x), p + 1L)
200+
check_linear_dependence_(y, x, p + 1L)
202201

203202
# Extract weights if required ----
204203
if (is.null(weights)) {
205204
wt <- rep(1.0, nt)
205+
} else if (exists("weights_vec")) {
206+
# Weights provided as vector
207+
wt <- weights_vec
208+
if (length(wt) != nrow(data)) {
209+
stop("Length of weights vector must equal number of observations.", call. = FALSE)
210+
}
211+
} else if (exists("weights_col")) {
212+
# Weights provided as formula - use the extracted column name
213+
wt <- data[[weights_col]]
206214
} else {
215+
# Weights provided as column name
207216
wt <- data[[weights]]
208217
}
209218

R/feglm_helpers.R

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,37 @@ col_types <- function(data) {
240240
#' @noRd
241241
model_frame_ <- function(data, formula, weights) {
242242
# Necessary columns
243-
needed_cols <- c(all.vars(formula), weights)
243+
formula_vars <- all.vars(formula)
244+
245+
# Handle different ways weights might be specified
246+
if (is.null(weights)) {
247+
# No weights specified
248+
weight_col <- NULL
249+
needed_cols <- formula_vars
250+
} else if (is.character(weights) && length(weights) == 1) {
251+
# Weights as column name
252+
weight_col <- weights
253+
needed_cols <- c(formula_vars, weight_col)
254+
} else if (inherits(weights, "formula")) {
255+
# Weights as formula like ~cyl
256+
weight_col <- all.vars(weights)
257+
needed_cols <- c(formula_vars, weight_col)
258+
# Store the extracted column name for later use
259+
assign("weights_col", weight_col, envir = parent.frame())
260+
} else if (is.numeric(weights)) {
261+
# Weights as vector - store for later use
262+
weight_col <- NULL
263+
needed_cols <- formula_vars
264+
assign("weights_vec", weights, envir = parent.frame())
265+
} else {
266+
stop("'weights' must be a column name, formula, or numeric vector", call. = FALSE)
267+
}
268+
269+
# Extract needed columns
244270
data <- data[, .SD, .SDcols = needed_cols]
245271

246272
lhs <- names(data)[1L]
247-
248273
nobs_full <- nrow(data)
249-
250274
data <- na.omit(data)
251275

252276
# Convert columns of type "units" to numeric
@@ -393,13 +417,20 @@ model_response_ <- function(data, formula) {
393417

394418
#' @title Check weights
395419
#' @description Checks if weights are valid
396-
#' @param x Regressor matrix
420+
#' @param y Dependent variable
421+
#' @param x Regressors matrix
397422
#' @param p Number of parameters
398423
#' @noRd
399-
check_linear_dependence_ <- function(x, p) {
400-
if (qr(x)$rank < p) {
424+
check_linear_dependence_ <- function(y, x, p) {
425+
# if (qr(x)$rank < p) {
426+
# stop("Linear dependent terms detected.", call. = FALSE)
427+
# }
428+
429+
if (check_linear_dependence_svd_(y, x, p) == 1L) {
401430
stop("Linear dependent terms detected.", call. = FALSE)
402431
}
432+
433+
return(TRUE)
403434
}
404435

405436
#' @title Check weights

R/felm.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,22 @@ felm <- function(formula = NULL, data = NULL, weights = NULL, control = NULL) {
138138
model_response_(data, formula)
139139

140140
# Check for linear dependence ----
141-
check_linear_dependence_(cbind(y, x), p + 1L)
141+
check_linear_dependence_(y, x, p + 1L)
142142

143143
# Extract weights if required ----
144144
if (is.null(weights)) {
145145
wt <- rep(1.0, nt)
146+
} else if (exists("weights_vec")) {
147+
# Weights provided as vector
148+
wt <- weights_vec
149+
if (length(wt) != nrow(data)) {
150+
stop("Length of weights vector must equal number of observations.", call. = FALSE)
151+
}
152+
} else if (exists("weights_col")) {
153+
# Weights provided as formula - use the extracted column name
154+
wt <- data[[weights_col]]
146155
} else {
156+
# Weights provided as column name
147157
wt <- data[[weights]]
148158
}
149159

R/fenegbin.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fenegbin <- function(
155155
model_response_(data, formula)
156156

157157
# Check for linear dependence in 'x' ----
158-
check_linear_dependence_(cbind(y, x), p + 1L)
158+
check_linear_dependence_(y, x, p + 1L)
159159

160160
# Extract weights if required ----
161161
if (is.null(weights)) {

docs/404.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/CODE_OF_CONDUCT.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/CONTRIBUTING.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)