ncn-foreigners / nonprobsvy

An R package for modern methods for non-probability surveys
https://ncn-foreigners.github.io/nonprobsvy/
Other
29 stars 4 forks source link

error when `svydesign` is provided with no `weights` argument #9

Closed BERENZ closed 1 year ago

BERENZ commented 1 year ago
library(survey)
library(nonprobsvy)
### generate data under simple random sampling
set.seed(123)

N <- 10000
n <- 1000
x1 <- rnorm(n = N, mean = 1, sd = 1)
x2 <- rexp(n = N, rate = 1)
epsilon <- rnorm(n = N) # rnorm(N)
y1 <- 1 + x1 + x2 + epsilon
y2 <- 0.5*(x1 - 0.5)^2 + x2 + epsilon
p1 <- exp(x2)/(1+exp(x2))
p2 <- exp(-0.5+0.5*(x2-2)^2)/(1+exp(-0.5+0.5*(x2-2)^2))
populacja <- data.frame(x1,x2,y1,y2,p1,p2)
flag_p1 <- rbinom(n = N, size = 1, prob = populacja$p1)
flag_p2 <- rbinom(n = N, size = 1, prob = populacja$p2)
flag_srs <- sample(x = 1:N, size = n)
source_nonprob_p1 <- populacja[flag_p1 == 1, ]
source_nonprob_p2 <- populacja[flag_p2 == 1, ]
source_prob <- svydesign(ids = ~ 1, data = populacja[flag_srs, ], weights = rep(N/n, n))
suppressWarnings(
  source_prob_no_weights <- svydesign(ids = ~ 1, data = populacja[flag_srs, ])
)

Running this code results

test1b <- nonprob(selection = ~ x1+ x2,
                    target = ~ y1,
                    data = source_nonprob_p1,
                    svydesign = source_prob_no_weights)

results with the following errors

Error in ps_method(X_nons, log_like, gradient, hessian, start, optim_method) :
Inifinite value of log_like in fitting ps_est by maxLik, error code 5

Traceback

6. stop("Inifinite value of log_like in fitting ps_est by maxLik, error code 5") at logitModel.R#57
5. ps_method(X_nons, log_like, gradient, hessian, start, optim_method) at EstimationMethods.R#105
4. estimation_method$model_selection(X, X_nons, X_rand, weights,
weights_rand, R, method_selection, optim_method, h = h, est_method,
maxit, varcov, ...) at internals.R#25
3. internal_selection(X = X, X_nons = X_nons, X_rand = X_rand, weights = weights,
weights_rand = weights_rand, R = R, method_selection = method_selection,
optim_method = optim_method, h = h, est_method = est_method,
maxit = maxit, varcov = TRUE) at nonprobIPW.R#104
2. nonprobIPW(selection, target, data, svydesign, pop_totals, pop_means,
pop_size, method_selection, family_selection, subset, strata,
weights, na_action, control_selection, control_inference,
start, verbose, contrasts, model, x, y, ...) at nonprob.R#116
1. nonprob(selection = ~x1 + x2, target = ~y1, data = source_nonprob_p1,
svydesign = source_prob_no_weights)