Skip to content

Commit

Permalink
Added ni exact se
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Chlebicki committed May 31, 2024
1 parent 9c41bd1 commit 2fa354c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
54 changes: 54 additions & 0 deletions R/nn.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,57 @@ nonprobMI_nn <- function(data,
)
model_nn
}


nn_exact <- function(pi_ij,
weights_rand,
n_nons,
y,
X_nons,
X_rand,
k,
#control,
N) {
# if (isTRUE("ppsmat" %in% class(pi_ij))) {
# pi_ij <- pi_ij$pij
# }
# # if (!is.null(svydesign$dcheck[[1]]$dcheck)) {
# # pi_ij <- svydesign$dcheck[[1]]$dcheck
# # }
# if (is.null(pi_ij)) {
# pi_ij <- outer(1 / weights_rand, 1 / weights_rand) * (
# 1 - outer(1 - 1 / weights_rand, 1 - 1 / weights_rand) /
# sum(1 - 1 / weights_rand))
# }
# # if (!is.matrix(pi_ij)) {
# #
# # }
# add variable for loop size to control
loop_size <- 50

dd <- vector(mode = "numeric", length = loop_size)
for (jj in 1:loop_size) {

boot_samp <- sample(1:n_nons, size = n_nons, replace = TRUE)
# boot_samp <- sample(1:n_rand, size = n_rand, replace = TRUE)
y_nons_b <- y[boot_samp]
x_nons_b <- X_nons[boot_samp, , drop = FALSE]

YY <- nonprobMI_nn(
data = x_nons_b,
query = X_rand,
k = k,
searchtype = "standard",
treetype = "kd"
#TODO:: add control options
#treetype = control$treetype,
#searchtype = control$searchtype
)

dd[jj] <- weighted.mean(
apply(YY$nn.idx, 1, FUN = \(x) mean(y_nons_b[x])),
weights_rand
)
}
var(dd)
}
4 changes: 2 additions & 2 deletions R/pmm.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ pmm_exact <- function(pi_ij,
n_nons,
y,
pmm_reg_engine,
stats,
glm,
#stats, #why is this here?
#glm, #why is this here?
model_obj,
svydesign,
predictive_match,
Expand Down
16 changes: 16 additions & 0 deletions R/varianceMI.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ internal_varMI <- function(svydesign,
sigma_hat <- mean((y - y_pred)^2) # family_nonprobsvy$variance(mu = y_pred, y = y)
est_ps <- n_nons / N
var_nonprob <- n_rand / N^2 * (1 - est_ps) / est_ps * sigma_hat

if (pmm_exact_se) {

This comment has been minimized.

Copy link
@BERENZ

BERENZ Jun 5, 2024

Contributor

@Kertoo shouldn't this be nn_excact_se as a new parameter in the controlInf() function?

This comment has been minimized.

Copy link
@Kertoo

Kertoo Jun 5, 2024

Member

this was set as a placeholder but we should probably have one parameter for all this like mi_exact_se

var_nonprob <- nn_exact(
pi_ij = pi_ij,
weights_rand = weights_rand,
n_nons = n_nons,
y = y,
X_nons = X_nons,
X_rand = X_rand,
k = k,
# TODO:: add control here
#control = control
N = N
)
}

} else if (method == "glm") { # TODO add variance for count binary outcome variable control_outcome$method

beta <- parameters[, 1]
Expand Down

0 comments on commit 2fa354c

Please sign in to comment.