Skip to content

Commit 13e18ef

Browse files
author
Juris Breidaks
committed
labojums
1 parent a6ba1e7 commit 13e18ef

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

vardpoor/R/vardom.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ vardom <- function(Y, H, PSU, w_final,
3333

3434
### Checking
3535

36-
fh_zero <- check_var(vars = fh_zero, varn = "fh_zero", varntype = "logical")
37-
PSU_level <- check_var(vars = PSU_level, varn = "PSU_level", varntype = "logical")
38-
outp_lin <- check_var(vars = outp_lin, varn = "outp_lin", varntype = "logical")
39-
outp_res <- check_var(vars = outp_res, varn = "outp_res", varntype = "logical")
40-
percentratio <- check_var(vars = percentratio, varn = "percentratio", varntype = "pinteger")
41-
confidence <- check_var(vars = confidence, varn = "confidence", varntype = "numeric01")
36+
fh_zero <- check_var(vars = fh_zero, varn = "fh_zero", varntype = "logical")
37+
PSU_level <- check_var(vars = PSU_level, varn = "PSU_level", varntype = "logical")
38+
outp_lin <- check_var(vars = outp_lin, varn = "outp_lin", varntype = "logical")
39+
outp_res <- check_var(vars = outp_res, varn = "outp_res", varntype = "logical")
40+
percentratio <- check_var(vars = percentratio, varn = "percentratio", varntype = "pinteger")
41+
confidence <- check_var(vars = confidence, varn = "confidence", varntype = "numeric01")
4242

4343
Y <- check_var(vars = Y, varn = "Y", dataset = dataset,
4444
check.names = TRUE, isnumeric = TRUE, grepls = "__")
@@ -360,8 +360,8 @@ vardom <- function(Y, H, PSU, w_final,
360360
tsad <- qnorm(0.5 * (1 + confidence))
361361
all_result[, absolute_margin_of_error := tsad * se]
362362
all_result[, relative_margin_of_error:= tsad * cv]
363-
all_result[, CI_lower := estim - tsad * se]
364-
all_result[, CI_upper := estim + tsad * se]
363+
all_result[, CI_lower := estim - absolute_margin_of_error]
364+
all_result[, CI_upper := estim + absolute_margin_of_error]
365365

366366
variableD <- NULL
367367
setnames(all_result, c("variable", "var_est"), c("variableD", "var"))
@@ -413,6 +413,6 @@ vardom <- function(Y, H, PSU, w_final,
413413

414414
list(lin_out = linratio_outp,
415415
res_out = res_outp,
416-
betas = betas,
416+
betas = betas,
417417
all_result = all_result)
418418
}

vardpoor/R/vardom_othstr.R

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
vardom_othstr <- function(Y, H, H2, PSU, w_final,
2-
id = NULL,
2+
id = NULL,
33
Dom = NULL,
44
period = NULL,
55
N_h = NULL,
@@ -9,18 +9,18 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
99
ind_gr = NULL,
1010
g = NULL,
1111
q = NULL,
12-
dataset = NULL,
13-
confidence = .95,
12+
dataset = NULL,
13+
confidence = .95,
1414
percentratio = 1,
1515
outp_lin = FALSE,
1616
outp_res = FALSE) {
17-
17+
1818
### Checking
1919

20-
outp_lin <- check_var(vars = outp_lin, varn = "outp_lin", varntype = "logical")
21-
outp_res <- check_var(vars = outp_res, varn = "outp_res", varntype = "logical")
22-
percentratio <- check_var(vars = percentratio, varn = "percentratio", varntype = "pinteger")
23-
confidence <- check_var(vars = confidence, varn = "confidence", varntype = "numeric01")
20+
outp_lin <- check_var(vars = outp_lin, varn = "outp_lin", varntype = "logical")
21+
outp_res <- check_var(vars = outp_res, varn = "outp_res", varntype = "logical")
22+
percentratio <- check_var(vars = percentratio, varn = "percentratio", varntype = "pinteger")
23+
confidence <- check_var(vars = confidence, varn = "confidence", varntype = "numeric01")
2424

2525
Y <- check_var(vars = Y, varn = "Y", dataset = dataset,
2626
check.names = TRUE, isnumeric = TRUE, grepls = "__")
@@ -105,31 +105,31 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
105105
pH <- NULL
106106
}
107107
setkeyv(N_h, names(N_h)[c(1 : (1 + np))])
108-
}
108+
}
109109

110110
# N_h2
111111
if (!is.null(N_h2)) {
112112
N_h2 <- data.table(N_h2)
113-
if (anyNA(N_h2)) stop("'N_h2' has missing values")
113+
if (anyNA(N_h2)) stop("'N_h2' has missing values")
114114
if (ncol(N_h2) != np + 2) stop(paste0("'N_h2' should be ", np + 2, " columns"))
115115
if (!is.numeric(N_h2[[ncol(N_h2)]])) stop("The last column of 'N_h2' should be numeric")
116116

117117
nams2 <- c(names(period), names(H2))
118118
if (all(nams2 %in% names(N_h2))) {N_h2[, (nams2) := lapply(.SD, as.character), .SDcols = nams2]
119-
} else stop(paste0("All strata titles of 'H2'", ifelse(!is.null(period), "and periods titles of 'period'", ""), " have not in 'N_h2'"))
119+
} else stop(paste0("All strata titles of 'H2'", ifelse(!is.null(period), "and periods titles of 'period'", ""), " have not in 'N_h2'"))
120120
if (is.null(period)) {
121121
if (names(H2) != names(N_h2)[1]) stop("Strata titles for 'H2' and 'N_h2' is not equal")
122122
if (any(is.na(merge(unique(H2), N_h2, by = names(H2), all.x = TRUE)))) stop("'N_h2' is not defined for all strata")
123123
} else { pH2 <- data.table(period, H2)
124124
if (any(names(pH2) != names(N_h2)[c(1 : (1 + np))])) stop("Strata titles for 'period' with 'H2' and 'N_h2' is not equal")
125125
if (any(is.na(merge(unique(pH2), N_h2, by = names(pH2), all.x = TRUE)))) stop("'N_h2' is not defined for all strata and periods")
126-
}
126+
}
127127
setkeyv(N_h2, names(N_h2)[c(1 : (1 + np))])
128128
} else stop ("N_h2 is not defined!")
129129

130130

131131
### Calculation
132-
132+
133133
# Domains
134134
if (!is.null(Dom)) Y1 <- domain(Y = Y, D = Dom,
135135
dataset = NULL,
@@ -144,14 +144,14 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
144144
Z0 <- copy(Z1)
145145
setnames(Z0, names(Z0), names(Y1))
146146
n_nonzero <- n_nonzero + Y1
147-
Z0 <- NULL
147+
Z0 <- NULL
148148
}
149-
if (!is.null(period)){ n_nonzero <- data.table(period, n_nonzero)
150-
n_nonzero <- n_nonzero[, lapply(.SD, function(x)
149+
if (!is.null(period)){ n_nonzero <- data.table(period, n_nonzero)
150+
n_nonzero <- n_nonzero[, lapply(.SD, function(x)
151151
sum(as.integer(abs(x) > .Machine$double.eps))),
152152
keyby = names(period),
153153
.SDcols = names(Y1)]
154-
} else n_nonzero <- n_nonzero[, lapply(.SD, function(x)
154+
} else n_nonzero <- n_nonzero[, lapply(.SD, function(x)
155155
sum(as.integer(abs(x) > .Machine$double.eps))),
156156
.SDcols = names(Y1)]
157157

@@ -168,7 +168,7 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
168168

169169
# Design weights
170170
if (!is.null(X)) w_design <- w_final / g else w_design <- w_final
171-
171+
172172
# Ratio of two totals
173173
linratio_outp <- per <- variableZ <- estim <- deff_sam <- NULL
174174
deff_est <- deff <- var_est2 <- se <- rse <- cv <- NULL
@@ -187,7 +187,7 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
187187
} else {
188188
periodap <- do.call("paste", c(as.list(period), sep = "_"))
189189
lin1 <- lapply(split(Y1[, .I], periodap), function(i)
190-
data.table(sar_nr = i,
190+
data.table(sar_nr = i,
191191
lin.ratio(Y = Y1[i], Z = Z1[i],
192192
weight = w_final[i],
193193
Dom = NULL, dataset = NULL,
@@ -198,7 +198,7 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
198198
Y2[, sar_nr := NULL]
199199
}
200200
if (any(is.na(Y2))) print("Results are calculated, but there are cases where Z = 0")
201-
if (outp_lin) linratio_outp <- data.table(idper, PSU, Y2)
201+
if (outp_lin) linratio_outp <- data.table(idper, PSU, Y2)
202202
} else {
203203
Y2 <- Y1
204204
}
@@ -227,11 +227,11 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
227227
Y3 <- rbindlist(lapply(lin1, function(x) x[[1]]))
228228
betas <- rbindlist(lapply(lin1, function(x) x[[2]]))
229229
setkeyv(Y3, "sar_nr")
230-
Y3[, sar_nr := NULL]
230+
Y3[, sar_nr := NULL]
231231
if (outp_res) res_outp <- data.table(idper, PSU, Y3)
232232
} else Y3 <- Y2
233233

234-
var_est <- variance_othstr(Y = Y3, H = H, H2 = H2,
234+
var_est <- variance_othstr(Y = Y3, H = H, H2 = H2,
235235
w_final = w_final, N_h = N_h,
236236
N_h2 = N_h2, period = period,
237237
dataset = NULL, checking = FALSE)
@@ -244,7 +244,7 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
244244
all_result <- merge(all_result, n_nonzero, all = TRUE)
245245

246246
# Variance of HT estimator under current design
247-
var_cur_HT <- variance_othstr(Y = Y2, H = H, H2 = H2,
247+
var_cur_HT <- variance_othstr(Y = Y2, H = H, H2 = H2,
248248
w_final = w_design, N_h = N_h,
249249
N_h2 = N_h2, period = period,
250250
dataset = NULL, checking = FALSE)
@@ -261,7 +261,7 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
261261
lin1 <- lapply(1 : nrow(period_agg), function(i) {
262262
per <- period_agg[i,][rep(1, nrow(Y2a)),]
263263
ind <- (rowSums(per == period) == ncol(period))
264-
data.table(period_agg[i,],
264+
data.table(period_agg[i,],
265265
var_srs(Y2a[ind], w = w_design[ind])$varsrs)
266266
})
267267
var_srs_HT <- rbindlist(lin1)
@@ -278,7 +278,7 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
278278
lin1 <- lapply(1:nrow(period_agg), function(i) {
279279
per <- period_agg[i,][rep(1, nrow(Y2a)),]
280280
ind <- (rowSums(per == period) == ncol(period))
281-
data.table(period_agg[i,],
281+
data.table(period_agg[i,],
282282
var_srs(Y3[ind], w = w_final[ind])$varsrs)
283283
})
284284
var_srs_ca <- rbindlist(lin1)
@@ -298,11 +298,11 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
298298
}
299299
Y_nov <- transpos(Y_nov, is.null(period), "Y_nov", names(period))
300300
all_result <- merge(all_result, Y_nov)
301-
301+
302302
if (!is.null(Z1)) {
303303
YZnames <- data.table(variable = names(Y1), variableDZ = names(Z1))
304304
all_result <- merge(all_result, YZnames, by = "variable")
305-
305+
306306
hZ <- data.table(Z1 * w_final)
307307
if (is.null(period)) { Z_nov <- hZ[, lapply(.SD, sum, na.rm = TRUE), .SDcols = names(Z1)]
308308
} else { hZ <- data.table(period, hZ)
@@ -314,22 +314,22 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
314314

315315
vars <- data.table(variable = names(Y1), nr_names = 1 : ncol(Y1))
316316
all_result <- merge(vars, all_result, by = "variable")
317-
317+
318318
vars <- idper <- Y1 <- Z1 <- Y_nov <- NULL
319-
Z_nov <- hY <- hZ <- YZnames <- dati <- NULL
319+
Z_nov <- hY <- hZ <- YZnames <- dati <- NULL
320320

321-
322-
all_result[, estim := Y_nov]
321+
322+
all_result[, estim := Y_nov]
323323
if (!is.null(all_result$Z_nov)) all_result[, estim := Y_nov / Z_nov]
324324

325325
if (nrow(all_result[var_est < 0]) > 0) print("Estimation of variance are negative!")
326-
326+
327327
# Design effect of sample design
328328
all_result[, deff_sam := var_cur_HT / var_srs_HT]
329-
329+
330330
# Design effect of estimator
331331
all_result[, deff_est := var_est / var_cur_HT]
332-
332+
333333
# Overall effect of sample design and estimator
334334
all_result[, deff := deff_sam * deff_est]
335335

@@ -343,8 +343,8 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
343343
tsad <- qnorm(0.5 * (1 + confidence))
344344
all_result[, absolute_margin_of_error := tsad * se]
345345
all_result[, relative_margin_of_error := tsad * cv]
346-
all_result[, CI_lower := estim - tsad * se]
347-
all_result[, CI_upper := estim + tsad * se]
346+
all_result[, CI_lower := estim - absolute_margin_of_error]
347+
all_result[, CI_upper := estim + absolute_margin_of_error]
348348

349349
setnames(all_result, c("variable", "var_est"), c("variableD", "var"))
350350
if (!is.null(all_result$Z_nov)) {
@@ -370,18 +370,18 @@ vardom_othstr <- function(Y, H, H2, PSU, w_final,
370370

371371
all_result <- merge(nosr, all_result, by = "variableD")
372372
namesDom <- nosr <- NULL
373-
373+
374374
if (!is.null(all_result$Z_nov)) {
375375
all_result[, variable := paste("R", get("variable"), get("variableZ"), sep = "__")] }
376376

377377
if (!is.null(c(Dom, period))) { all_result <- merge(all_result, nhs, all = TRUE, by = c(namesDom1, names(period)))
378378
} else { all_result[, respondent_count := nhs$respondent_count]
379-
all_result[, pop_size := nhs$pop_size]}
379+
all_result[, pop_size := nhs$pop_size]}
380380

381381
all_result[, confidence_level := confidence]
382-
variab <- c("respondent_count", "n_nonzero", "pop_size", "estim", "var", "se",
382+
variab <- c("respondent_count", "n_nonzero", "pop_size", "estim", "var", "se",
383383
"rse", "cv", "absolute_margin_of_error", "relative_margin_of_error",
384-
"CI_lower", "CI_upper", "confidence_level", "var_srs_HT", "var_cur_HT",
384+
"CI_lower", "CI_upper", "confidence_level", "var_srs_HT", "var_cur_HT",
385385
"var_srs_ca", "deff_sam", "deff_est", "deff")
386386

387387
setkeyv(all_result, c("nr_names", names(Dom), names(period)))

vardpoor/R/vardomh.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ vardomh <- function(Y, H, PSU, w_final,
2323

2424
### Checking
2525

26-
fh_zero <- check_var(vars = fh_zero, varn = "fh_zero", varntype = "logical")
27-
PSU_level <- check_var(vars = PSU_level, varn = "PSU_level", varntype = "logical")
28-
outp_lin <- check_var(vars = outp_lin, varn = "outp_lin", varntype = "logical")
29-
outp_res <- check_var(vars = outp_res, varn = "outp_res", varntype = "logical")
30-
percentratio <- check_var(vars = percentratio, varn = "percentratio", varntype = "pinteger")
31-
confidence <- check_var(vars = confidence, varn = "confidence", varntype = "numeric01")
26+
fh_zero <- check_var(vars = fh_zero, varn = "fh_zero", varntype = "logical")
27+
PSU_level <- check_var(vars = PSU_level, varn = "PSU_level", varntype = "logical")
28+
outp_lin <- check_var(vars = outp_lin, varn = "outp_lin", varntype = "logical")
29+
outp_res <- check_var(vars = outp_res, varn = "outp_res", varntype = "logical")
30+
percentratio <- check_var(vars = percentratio, varn = "percentratio", varntype = "pinteger")
31+
confidence <- check_var(vars = confidence, varn = "confidence", varntype = "numeric01")
3232

3333
if(!is.null(X)) {
3434
if (is.null(datasetX)) datasetX <- copy(dataset)
@@ -79,7 +79,7 @@ vardomh <- function(Y, H, PSU, w_final,
7979
ncols = 1, Ynrow = Ynrow, ischaracter = TRUE,
8080
isvector = TRUE, mustbedefined = FALSE, PSUs = PSU)
8181

82-
if(!is.null(X) | !is.null(ind_gr) |!is.null(g) | !is.null(q) |
82+
if(!is.null(X) | !is.null(ind_gr) |!is.null(g) | !is.null(q) |
8383
!is.null(periodX) | !is.null(X_ID_level1) | !is.null(datasetX)) {
8484
X <- check_var(vars = X, varn = "X", dataset = datasetX,
8585
check.names = TRUE, isnumeric = TRUE,
@@ -95,7 +95,7 @@ vardomh <- function(Y, H, PSU, w_final,
9595
g <- check_var(vars = g, varn = "g", dataset = datasetX,
9696
ncols = 1, Xnrow = Xnrow, isnumeric = TRUE,
9797
isvector = TRUE, dX = "X")
98-
98+
9999
q <- check_var(vars = q, varn = "q", dataset = datasetX,
100100
ncols = 1, Xnrow = Xnrow, isnumeric = TRUE,
101101
isvector = TRUE, dX = "X")
@@ -157,7 +157,7 @@ vardomh <- function(Y, H, PSU, w_final,
157157
Z0 <- copy(Z1)
158158
setnames(Z0, names(Z0), names(Y1))
159159
n_nonzero <- n_nonzero + Y1
160-
Z0 <- NULL
160+
Z0 <- NULL
161161
}
162162
if (!is.null(period)){ n_nonzero <- data.table(period, n_nonzero)
163163
n_nonzero <- n_nonzero[, lapply(.SD, function(x)
@@ -309,7 +309,7 @@ vardomh <- function(Y, H, PSU, w_final,
309309
checking = FALSE)
310310
pers0 <- ind_gr[i, .N, keyby = c(names(ind_gr))]
311311
list(data.table(sar_nr = i, resid$residuals),
312-
data.table(pers0[, N := NULL], resid$betas))
312+
data.table(pers0[, N := NULL], resid$betas))
313313
})
314314

315315
Y4 <- rbindlist(lapply(lin1, function(x) x[[1]]))
@@ -423,8 +423,8 @@ vardomh <- function(Y, H, PSU, w_final,
423423
tsad <- qnorm(0.5 * (1 + confidence))
424424
all_result[, absolute_margin_of_error := tsad * se]
425425
all_result[, relative_margin_of_error := tsad * cv]
426-
all_result[, CI_lower := estim - tsad * se]
427-
all_result[, CI_upper := estim + tsad * se]
426+
all_result[, CI_lower := estim - absolute_margin_of_error]
427+
all_result[, CI_upper := estim + absolute_margin_of_error]
428428

429429
variableD <- NULL
430430
setnames(all_result, c("variable", "var_est"), c("variableD", "var"))

vardpoor/R/varpoord.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ varpoord <- function(Y, w_final,
559559
tsad <- qnorm(0.5 * (1 + confidence))
560560
all_result[, absolute_margin_of_error := tsad * se]
561561
all_result[, relative_margin_of_error := tsad * cv]
562-
all_result[, CI_lower := value - tsad * se]
563-
all_result[, CI_upper := value + tsad * se]
562+
all_result[, CI_lower := value - absolute_margin_of_error]
563+
all_result[, CI_upper := value + absolute_margin_of_error]
564564

565565
setnames(all_result, "var_est", "var")
566566

0 commit comments

Comments
 (0)