1# `stats::nobs` is a standard function to retrieve the number of
2# observations used to fit a model. Unfortunately, Some packages do
3# not define a `stats::nobs.MODEL` method. This file fills-in those missing
4# methods. Ideally, we should offload these methods by submitting them for
5# adoption in the upstream packages.
6
7# These packages still need to be checked:
8
9# caret
10# joineRML
11# ergm
12# nlme
13# rstanarm
14# lavaan: conflict between stats::nobs and lavaan::nobs
15# mass-ridgelm
16# survival-*
17# quantreg-rq
18# quantreg-rqs
19
20# nnet-multinom
21nobs.multinom <- function(object, ...) {
22  nrow(object$residuals)
23}
24
25# orcutt
26nobs.orcutt <- function(object, ...) {
27  nrow(object$residuals)
28}
29
30# mass-fitdistr
31nobs.fitdistr <- function(object, ...) {
32  object$n
33}
34
35# biglm
36nobs.biglm <- function(object, ...) {
37  object$n
38}
39
40# glmnet-cv-glmnet
41nobs.cv.glmnet <- function(object, ...) {
42  stats::nobs(object$glmnet.fit)
43}
44
45# gmm
46nobs.gmm <- function(object, ...) {
47  object$n
48}
49
50# lfe - felm
51nobs.felm <- function(object, ...) {
52  object$N
53}
54
55# lmodel2
56nobs.lmodel2 <- function(object, ...) {
57  object$n
58}
59
60# mclust
61nobs.Mclust <- function(object, ...) {
62  object$n
63}
64
65# muhaz
66nobs.muhaz <- function(object, ...) {
67  length(object$pin$times)
68}
69
70# polca
71nobs.poLCA <- function(object, ...) {
72  object$N
73}
74
75# robust-glmrob
76nobs.lmRob <- function(object, ...) {
77  length(object$residuals)
78}
79nobs.glmRob <- function(object, ...) {
80  length(object$residuals)
81}
82
83# stats-loess
84nobs.loess <- function(object, ...) {
85  object$n
86}
87
88# stats-prcomp
89nobs.prcomp <- function(object, ...) {
90  NROW(object$x)
91}
92
93# stats-smooth.spline
94nobs.smooth.spline <- function(object, ...) {
95  length(object$x)
96}
97
98# bbmle
99nobs.bbmle <- function(object, ...) {
100  length(object@data[[1]])
101}
102
103# survival-aareg
104nobs.aareg <- function(object, ...) {
105  object$n[1] # obs / event times / event times in computation
106}
107
108# survival-survreg
109nobs.survreg <- function(object, ...) {
110  length(object$linear.predictors)
111}
112
113# survival-survfit
114nobs.survfit <- function(object, ...) {
115  object$n
116}
117
118# survival-survfit.cox
119nobs.survfit.cox <- function(object, ...) {
120  object$n
121}
122
123# survival-coxph
124nobs.coxph <- function(object, ...) {
125  length(object$linear.predictors)
126}
127
128# survival-pyears
129nobs.pyears <- function(object, ...) {
130  object$observations
131}
132
133# survival-survdiff
134nobs.survdiff <- function(object, ...) {
135  s <- summary(object)
136  s$nobs
137}
138
139# tseries
140nobs.garch <- function(object, ...) {
141  object$n.used
142}
143