@@ -94,7 +94,7 @@ get_varcov.default <- function(x,
94
94
vcov <- .check_vcov_args(x , vcov = vcov , verbose = verbose , ... )
95
95
96
96
if (is.null(vcov )) {
97
- vc <- suppressWarnings( stats :: vcov( x ) )
97
+ vc <- .safe_vcov( x )
98
98
} else {
99
99
vc <- .get_varcov_sandwich(x ,
100
100
vcov_fun = vcov ,
@@ -127,9 +127,9 @@ get_varcov.fixest <- function(x,
127
127
# fixest supplies its own mechanism. Vincent thinks it might not be wise to
128
128
# try `sandwich`, because there may be inconsistencies.
129
129
check_if_installed(" fixest" )
130
- args <- c(list (x , vcov = vcov ), vcov_args )
130
+ my_args <- c(list (x , vcov = vcov ), vcov_args )
131
131
FUN <- stats :: vcov
132
- do.call(" FUN" , args )
132
+ do.call(" FUN" , my_args )
133
133
}
134
134
135
135
@@ -223,19 +223,17 @@ get_varcov.DirichletRegModel <- function(x,
223
223
.check_get_varcov_dots(x , ... )
224
224
component <- match.arg(component )
225
225
if (x $ parametrization == " common" ) {
226
- vc <- stats :: vcov(x )
226
+ vc <- .safe_vcov(x )
227
+ } else if (component == " conditional" ) {
228
+ vc <- .safe_vcov(x )
229
+ keep <- grepl(" ^(?!\\ (phi\\ ))" , rownames(vc ), perl = TRUE )
230
+ vc <- vc [keep , keep , drop = FALSE ]
231
+ } else if (component == " precision" ) {
232
+ vc <- .safe_vcov(x )
233
+ keep <- startsWith(rownames(vc ), " (phi)" )
234
+ vc <- vc [keep , keep , drop = FALSE ]
227
235
} else {
228
- if (component == " conditional" ) {
229
- vc <- stats :: vcov(x )
230
- keep <- grepl(" ^(?!\\ (phi\\ ))" , rownames(vc ), perl = TRUE )
231
- vc <- vc [keep , keep , drop = FALSE ]
232
- } else if (component == " precision" ) {
233
- vc <- stats :: vcov(x )
234
- keep <- startsWith(rownames(vc ), " (phi)" )
235
- vc <- vc [keep , keep , drop = FALSE ]
236
- } else {
237
- vc <- stats :: vcov(x )
238
- }
236
+ vc <- .safe_vcov(x )
239
237
}
240
238
.process_vcov(vc , verbose , ... )
241
239
}
@@ -253,19 +251,19 @@ get_varcov.clm2 <- function(x,
253
251
n_location <- length(x $ beta )
254
252
n_scale <- length(x $ zeta )
255
253
256
- vc <- stats :: vcov (x )
254
+ vc <- .safe_vcov (x )
257
255
258
256
if (.is_negativ_matrix(vc , ... )) {
259
257
vc <- .fix_negative_matrix(vc )
260
258
}
261
259
262
- range <- switch (component ,
260
+ col_range <- switch (component ,
263
261
all = 1 : (n_scale + n_intercepts + n_location ),
264
262
conditional = 1 : (n_intercepts + n_location ),
265
263
scale = (1 + n_intercepts + n_location ): (n_scale + n_intercepts + n_location )
266
264
)
267
265
268
- vc <- vc [range , range , drop = FALSE ]
266
+ vc <- vc [col_range , col_range , drop = FALSE ]
269
267
270
268
# fix possible missings due to rank deficient model matrix
271
269
vc <- .fix_rank_deficiency(vc )
@@ -307,7 +305,7 @@ get_varcov.pgmm <- function(x,
307
305
component <- match.arg(component )
308
306
309
307
if (is.null(vcov )) {
310
- vc <- suppressWarnings( stats :: vcov( x ) )
308
+ vc <- .safe_vcov( x )
311
309
} else {
312
310
vc <- .get_varcov_sandwich(x ,
313
311
vcov_fun = vcov ,
@@ -353,7 +351,7 @@ get_varcov.mvord <- function(x,
353
351
... ) {
354
352
.check_get_varcov_dots(x , ... )
355
353
component <- match.arg(component )
356
- vc <- stats :: vcov (x )
354
+ vc <- .safe_vcov (x )
357
355
358
356
if (component != " all" ) {
359
357
fp <- find_parameters(x )[[component ]]
@@ -374,7 +372,7 @@ get_varcov.mjoint <- function(x,
374
372
... ) {
375
373
.check_get_varcov_dots(x , ... )
376
374
component <- match.arg(component )
377
- vc <- stats :: vcov (x )
375
+ vc <- .safe_vcov (x )
378
376
379
377
keep <- match(find_parameters(x , flatten = TRUE , component = component ), rownames(vc ))
380
378
vc <- vc [keep , keep , drop = FALSE ]
@@ -389,7 +387,7 @@ get_varcov.mhurdle <- function(x,
389
387
... ) {
390
388
.check_get_varcov_dots(x , ... )
391
389
component <- match.arg(component )
392
- vc <- stats :: vcov (x )
390
+ vc <- .safe_vcov (x )
393
391
394
392
# rownames(vc) <- gsub("^(h1|h2|h3)\\.(.*)", "\\2", rownames(vc))
395
393
# colnames(vc) <- rownames(vc)
@@ -405,7 +403,7 @@ get_varcov.mhurdle <- function(x,
405
403
get_varcov.truncreg <- function (x , component = c(" conditional" , " all" ), verbose = TRUE , ... ) {
406
404
.check_get_varcov_dots(x , ... )
407
405
component <- match.arg(component )
408
- vc <- stats :: vcov (x )
406
+ vc <- .safe_vcov (x )
409
407
410
408
if (component == " conditional" ) {
411
409
vc <- vc [1 : (nrow(vc ) - 1 ), 1 : (ncol(vc ) - 1 ), drop = FALSE ]
@@ -418,7 +416,7 @@ get_varcov.truncreg <- function(x, component = c("conditional", "all"), verbose
418
416
get_varcov.gamlss <- function (x , component = c(" conditional" , " all" ), verbose = TRUE , ... ) {
419
417
.check_get_varcov_dots(x , ... )
420
418
component <- match.arg(component )
421
- vc <- suppressWarnings( stats :: vcov( x ) )
419
+ vc <- .safe_vcov( x )
422
420
423
421
if (component == " conditional" ) {
424
422
cond_pars <- length(find_parameters(x )$ conditional )
@@ -520,10 +518,10 @@ get_varcov.glmmTMB <- function(x,
520
518
component <- match.arg(component )
521
519
522
520
vc <- switch (component ,
523
- conditional = stats :: vcov (x )[[" cond" ]],
521
+ conditional = .safe_vcov (x )[[" cond" ]],
524
522
zi = ,
525
- zero_inflated = stats :: vcov (x )[[" zi" ]],
526
- dispersion = stats :: vcov (x )[[" disp" ]],
523
+ zero_inflated = .safe_vcov (x )[[" zi" ]],
524
+ dispersion = .safe_vcov (x )[[" disp" ]],
527
525
stats :: vcov(x , full = TRUE )
528
526
)
529
527
.process_vcov(vc , verbose , ... )
@@ -592,7 +590,7 @@ get_varcov.brmsfit <- function(x, component = "conditional", verbose = TRUE, ...
592
590
params <- find_parameters(x , effects = " fixed" , component = component , flatten = TRUE )
593
591
params <- gsub(" ^b_" , " " , params )
594
592
595
- vc <- stats :: vcov (x )[params , params , drop = FALSE ]
593
+ vc <- .safe_vcov (x )[params , params , drop = FALSE ]
596
594
.process_vcov(vc , verbose , ... )
597
595
}
598
596
@@ -659,15 +657,15 @@ get_varcov.flac <- get_varcov.flic
659
657
get_varcov.merModList <- function (x , ... ) {
660
658
.check_get_varcov_dots(x , ... )
661
659
format_warning(" Can't access variance-covariance matrix for 'merModList' objects." )
662
- return ( NULL )
660
+ NULL
663
661
}
664
662
665
663
666
664
# ' @export
667
665
get_varcov.mediate <- function (x , ... ) {
668
666
.check_get_varcov_dots(x , ... )
669
667
format_warning(" Can't access variance-covariance matrix for 'mediate' objects." )
670
- return ( NULL )
668
+ NULL
671
669
}
672
670
673
671
@@ -715,7 +713,7 @@ get_varcov.bife <- function(x, verbose = TRUE, ...) {
715
713
.check_get_varcov_dots(x , ... )
716
714
params <- find_parameters(x , flatten = TRUE )
717
715
np <- length(params )
718
- vc <- stats :: vcov (x )[1 : np , 1 : np , drop = FALSE ]
716
+ vc <- .safe_vcov (x )[1 : np , 1 : np , drop = FALSE ]
719
717
720
718
dimnames(vc ) <- list (params , params )
721
719
.process_vcov(vc , verbose , ... )
@@ -725,7 +723,7 @@ get_varcov.bife <- function(x, verbose = TRUE, ...) {
725
723
# ' @export
726
724
get_varcov.Rchoice <- function (x , verbose = TRUE , ... ) {
727
725
.check_get_varcov_dots(x , ... )
728
- vc <- stats :: vcov (x )
726
+ vc <- .safe_vcov (x )
729
727
params <- find_parameters(x , flatten = TRUE )
730
728
dimnames(vc ) <- list (params , params )
731
729
.process_vcov(vc , verbose , ... )
@@ -803,7 +801,7 @@ get_varcov.rqs <- get_varcov.crq
803
801
get_varcov.flexsurvreg <- function (x , verbose = TRUE , ... ) {
804
802
.check_get_varcov_dots(x , ... )
805
803
pars <- find_parameters(x , flatten = TRUE )
806
- vc <- as.matrix(stats :: vcov (x ))[pars , pars , drop = FALSE ]
804
+ vc <- as.matrix(.safe_vcov (x ))[pars , pars , drop = FALSE ]
807
805
.process_vcov(vc , verbose , ... )
808
806
}
809
807
@@ -863,7 +861,7 @@ get_varcov.mixor <- function(x, effects = c("all", "fixed", "random"), verbose =
863
861
.check_get_varcov_dots(x , ... )
864
862
effects <- match.arg(effects )
865
863
params <- find_parameters(x , effects = effects , flatten = TRUE )
866
- vc <- as.matrix(stats :: vcov (x ))[params , params , drop = FALSE ]
864
+ vc <- as.matrix(.safe_vcov (x ))[params , params , drop = FALSE ]
867
865
.process_vcov(vc , verbose , ... )
868
866
}
869
867
@@ -973,7 +971,7 @@ get_varcov.vgam <- get_varcov.vglm
973
971
get_varcov.tobit <- function (x , verbose = TRUE , ... ) {
974
972
.check_get_varcov_dots(x , ... )
975
973
coef_names <- find_parameters(x , flatten = TRUE )
976
- vc <- stats :: vcov (x )[coef_names , coef_names , drop = FALSE ]
974
+ vc <- .safe_vcov (x )[coef_names , coef_names , drop = FALSE ]
977
975
.process_vcov(vc , verbose , ... )
978
976
}
979
977
@@ -1031,6 +1029,27 @@ get_varcov.LORgee <- get_varcov.gee
1031
1029
# helper-functions -----------------------------------------------------
1032
1030
1033
1031
1032
+ .safe_vcov <- function (x ) {
1033
+ vc <- tryCatch(
1034
+ suppressWarnings(stats :: vcov(x )),
1035
+ error = function (e ) e
1036
+ )
1037
+ if (inherits(vc , " error" )) {
1038
+ # check for dates or times, which can cause the error
1039
+ my_data <- get_data(x , verbose = FALSE )
1040
+ if (! is.null(my_data ) && any(vapply(my_data , inherits , FUN.VALUE = logical (1 ), what = c(" Date" , " POSIXt" , " difftime" )))) { # nolint
1041
+ msg <- " A reason might be that your model includes dates or times. Please convert them to numeric values before fitting the model." # nolint
1042
+ } else {
1043
+ msg <- NULL
1044
+ }
1045
+ format_error(paste(
1046
+ " Can't extract variance-covariance matrix. `get_varcov()` returned following error:" ,
1047
+ vc $ message
1048
+ ), msg )
1049
+ }
1050
+ vc
1051
+ }
1052
+
1034
1053
.process_vcov <- function (vc , verbose = TRUE , ... ) {
1035
1054
if (.is_negativ_matrix(vc , ... )) {
1036
1055
vc <- .fix_negative_matrix(vc )
@@ -1100,8 +1119,8 @@ get_varcov.LORgee <- get_varcov.gee
1100
1119
1101
1120
.get_weighted_varcov <- function (x , cov_unscaled ) {
1102
1121
ssd <- .weighted_crossprod(stats :: residuals(x ), w = x $ weights )
1103
- df <- sum(x $ weights )
1104
- out <- structure(list (SSD = ssd , call = x $ call , df = df ), class = " SSD" )
1122
+ weight_df <- sum(x $ weights )
1123
+ out <- structure(list (SSD = ssd , call = x $ call , df = weight_df ), class = " SSD" )
1105
1124
kronecker(stats :: estVar(out ), cov_unscaled , make.dimnames = TRUE )
1106
1125
}
1107
1126
@@ -1116,19 +1135,17 @@ get_varcov.LORgee <- get_varcov.gee
1116
1135
}
1117
1136
1118
1137
if (length(w ) == 1 || (is.vector(w ) && stats :: sd(w ) < sqrt(.Machine $ double.eps ))) {
1119
- return (w [1 ] * crossprod(x ))
1138
+ w [1 ] * crossprod(x )
1139
+ } else if (is.vector(w )) {
1140
+ if (length(w ) != nrow(x )) {
1141
+ format_error(" `w` is the wrong length." )
1142
+ }
1143
+ crossprod(x , w * x )
1120
1144
} else {
1121
- if (is.vector(w )) {
1122
- if (length(w ) != nrow(x )) {
1123
- format_error(" `w` is the wrong length." )
1124
- }
1125
- return (crossprod(x , w * x ))
1126
- } else {
1127
- if (nrow(w ) != ncol(w ) || nrow(w ) != nrow(x )) {
1128
- format_error(" `w` is the wrong dimension." )
1129
- }
1130
- return (crossprod(x , w %*% x ))
1145
+ if (nrow(w ) != ncol(w ) || nrow(w ) != nrow(x )) {
1146
+ format_error(" `w` is the wrong dimension." )
1131
1147
}
1148
+ crossprod(x , w %*% x )
1132
1149
}
1133
1150
}
1134
1151
0 commit comments