27
27
# ' sample_to_adjust = "sample",
28
28
# ' adjusted_sample = "new_sample",
29
29
# ' adjust_by = 4)
30
- # '
31
-
32
30
adjust_samples_to_budget <- function (data , budget , sample_to_adjust , adjusted_sample = " justert_ant_prover" , adjust_by ) {
33
31
34
32
# ARGUMENT CHECKING ----
@@ -46,13 +44,18 @@ adjust_samples_to_budget <- function(data, budget, sample_to_adjust, adjusted_sa
46
44
# INITILIZE VARIABLES ----
47
45
total_estimated <- sum(data [, sample_to_adjust ], na.rm = TRUE )
48
46
n_units <- length(which(data [, sample_to_adjust ] > 0 ))
49
- difference <- c(as.numeric(total_estimated - budget ) , rep(NA , dim(data )[1 ] - 1 ))
47
+ difference <- c(as.numeric(total_estimated - budget ), rep(NA , dim(data )[1 ] - 1 ))
50
48
51
49
# ADJUST SAMPLE NUMBER ----
52
50
# Order data with largest sample size first
53
51
data <- data [order(data [, sample_to_adjust ], decreasing = TRUE ), ]
54
52
55
- # Only justify sample number when there is disagreement between budget and calculated number of samples
53
+ # If total_estimated = budget, make new column adjusted_sample based on sample_to_adjust
54
+ if (total_estimated == budget ) {
55
+ data [, adjusted_sample ] <- data [, sample_to_adjust ]
56
+ }
57
+
58
+ # Only justify sample number when there is disagreement between budget and calculated number of samples
56
59
if (total_estimated != budget ) {
57
60
# Adjust for each sampled unit with the unit having the largest sample size first
58
61
for (i in c(1 : dim(data )[1 ])) {
@@ -65,15 +68,15 @@ adjust_samples_to_budget <- function(data, budget, sample_to_adjust, adjusted_sa
65
68
justify <- ifelse(difference [i ] > 0 , - adjust_by , adjust_by )
66
69
} else {
67
70
justify <- ifelse(difference [i ] > 0 ,
68
- floor(- difference [i ] / (n_units - i )),
69
- ceiling(- difference [i ] / (n_units - i )))
71
+ floor(- difference [i ] / (n_units - i )),
72
+ ceiling(- difference [i ] / (n_units - i )))
70
73
}
71
74
if (difference [i ] == 0 ) {justify <- 0 }
72
75
73
76
# Make new column with adjusted number
74
77
data [i , adjusted_sample ] <- data [i , sample_to_adjust ] + justify
75
78
if (i < dim(data )[1 ]) {
76
- difference [i + 1 ] <- difference [i ] + justify
79
+ difference [i + 1 ] <- difference [i ] + justify
77
80
}
78
81
79
82
}
0 commit comments