Generate cluster sample
Usage
cluster_gen(
n,
N = 1,
cluster_labels = NULL,
resp_labels = NULL,
cat_prop = NULL,
n_X = NULL,
n_W = NULL,
c_mean = NULL,
sigma = NULL,
cor_matrix = NULL,
separate_questionnaires = TRUE,
collapse = "none",
sum_pop = sapply(N, sum),
calc_weights = TRUE,
sampling_method = "mixed",
rho = NULL,
theta = FALSE,
verbose = TRUE,
print_pop_structure = verbose,
...
)Arguments
- n
numeric vector or list with the number of sampled observations (clusters or subjects) on each level
- N
population size of each sampled cluster element on each level. Either a numeric vector or a list of numeric vectors. If
Nis a list, it must have the same length asnand each element ofNmust have the same length as the corresponding element ofn- cluster_labels
character vector with the names of each cluster level
- resp_labels
character vector with the names of the questionnaire respondents on each level
- cat_prop
list of cumulative proportions for each item. If
theta = TRUE, the first element ofcat_propmust be a scalar 1, which corresponds to thetheta.- n_X
list of
n_Xper cluster level- n_W
list of
n_Wper cluster level- c_mean
vector of means for the continuous variables or list of vectors for the continuous variables for each level. Defaults to 0, but may change if
rhois set.- sigma
vector of standard deviations for the continuous variables or list of vectors for the continuous variables for each level. Defaults to 1, but may change if
rhois set.- cor_matrix
Correlation matrix between all variables (except weights). By default, correlations are randomly generated.
- separate_questionnaires
if
TRUE, each level will have its own questionnaire- collapse
if
TRUE, function output contains only one data frame with all answers. It can also be "none", "partial" and "full" for finer control on 3+ levels- sum_pop
total population at each level (sampled or not)
- calc_weights
if
TRUE, sampling weights are calculated- sampling_method
can be "SRS" for Simple Random Sampling, "PPS" for Probabilities Proportional to Size, "mixed" to use PPS for schools and SRS otherwise, or a vector with the sampling method for each level
- rho
intraclass correlation (scalar, vector or list, as appropriate)
- theta
if
TRUE, the first continuous variable will be labeled 'theta'. Otherwise, it will be labeled 'q1'.- verbose
if
TRUE, prints output messages- print_pop_structure
if
TRUE, prints the population hierarchical structure (as long as it differs from the sample structure)- ...
Additional parameters to be passed to
questionnaire_gen()
Details
This function relies heavily in two
sub-functions—cluster_gen_separate and cluster_gen_together—which
can be called independently. This does not make cluster_gen a simple
wrapper function, as it performs several operations prior to calling its
sub-functions, such as randomly generating n_X and n_W if they are
not determined by user.
n can have unitary length, in which case all clusters will have the same
size.
N is not the population size across all elements of a level, but the
population size for each element of one level.
Regarding the additional parameters to be passed to questionnaire_gen(),
they can be passed either in the same format as questionnaire_gen() or
as more complex objects that contain information for each cluster level.
Note
For the purpose of this function, levels are counted starting from
the top nesting/clustering level. This means that, for example, schools
are the first cluster level, classes are the second, and students are the
third and final level. This behavior can be customized by naming the n
argument or providing custom labels in cluster_labels and resp_labels.
Manually setting both c_mean and rho, while possible, may yield
unexpected results due to how those parameters work together. A high
intraclass correlation (rho) theoretically means that each group will
end up with different means so they can be better separated. If c_mean
is left untouched (i.e., at the default value of zero), then c_mean
will freely change between clusters in order to result in the expected
intraclass correlation. For large samples, c_mean will in practice
correspond to the grand mean across that level, as the means of each
element will be different no matter the sample size.
Moreover, if c_mean, sigma and rho are passed to the function, the
means will be recalculated as a function of the other two parameters. The
three are interdependent and cannot be passed simultaneously.
If in addition to rho the user also determine different means for each
level, the only way the math can check out is if the variance in each
group becomes very high. For examples of this scenario and the one
described in the previous paragraph, check out the final section of this
page.
The ranges() function should always be put inside a list(), as
putting it inside a vector (c()) will cancel its effect. For more
details, please read the documentation of the ranges() function.
The only arguments that can be used to label each level are n, N,
cluster_labels and resp_labels. Labeling other arguments such as
c_mean and cat_prop has no effect on the final results, but it is a
recommended way for users to keep track of which value corresponds to
which element in a complex hierarchical structure.
One of the extra arguments that can be passed by this function is family.
If family == "gaussian", the questionnaire will be generated
assuming that all the variables are jointly-distributed as a multivariate
normal. The default behavior is family == NULL, where the data is
generated using the polychoric correlation matrix, with no distributional
assumptions.
Examples
# Simple structure of 3 schools with 5 students each
cluster_gen(c(3, 5))
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools
#> Total respondents: 15 (5 * 3)
#> school1 (5 students)
#> school2 (5 students)
#> school3 (5 students)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.student.weight should add up to the number of students in the population (15)
#> $school
#> $school[[1]]
#> subject q1 q2 q3 school.weight within.school.weight
#> 1 1 -0.3512564 3 2 1 1
#> 2 2 1.4241946 3 1 1 1
#> 3 3 0.5854469 3 1 1 1
#> 4 4 -0.1420129 1 3 1 1
#> 5 5 1.8447791 3 1 1 1
#> final.student.weight uniqueID
#> 1 1 student1_school1
#> 2 1 student2_school1
#> 3 1 student3_school1
#> 4 1 student4_school1
#> 5 1 student5_school1
#>
#> $school[[2]]
#> subject q1 q2 q3 school.weight within.school.weight
#> 1 1 -1.44357916 2 3 1 1
#> 2 2 0.94761502 1 2 1 1
#> 3 3 -1.39300663 1 3 1 1
#> 4 4 -0.07314225 3 1 1 1
#> 5 5 -0.10173486 2 3 1 1
#> final.student.weight uniqueID
#> 1 1 student1_school2
#> 2 1 student2_school2
#> 3 1 student3_school2
#> 4 1 student4_school2
#> 5 1 student5_school2
#>
#> $school[[3]]
#> subject q1 q2 q3 school.weight within.school.weight
#> 1 1 0.81852086 2 3 1 1
#> 2 2 0.08680236 2 3 1 1
#> 3 3 0.08565820 2 3 1 1
#> 4 4 -0.85327058 1 1 1 1
#> 5 5 -0.36174103 1 1 1 1
#> final.student.weight uniqueID
#> 1 1 student1_school3
#> 2 1 student2_school3
#> 3 1 student3_school3
#> 4 1 student4_school3
#> 5 1 student5_school3
#>
#>
#> attr(,"class")
#> [1] "lsasimcluster" "list"
# Complex structure of 2 schools with different number of students,
# sampling weights and custom number of questions
n <- list(3, c(20, 15, 25))
N <- list(5, c(200, 500, 400, 100, 100))
cluster_gen(n, N, n_X = 5, n_W = 2)
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Population structure
#> school1 (200 students)
#> school2 (500 students)
#> school3 (400 students)
#> school4 (100 students)
#> school5 (100 students)
#> Sampled structure
#> Generating questionnaires for schools
#> Total respondents: 60 (20 + 15 + 25)
#> school1 (20 students)
#> school2 (15 students)
#> school3 (25 students)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.student.weight should add up to the number of students in the population (1300)
#> $school
#> $school[[1]]
#> subject q1 q2 q3 q4 q5 q6 q7
#> 1 1 0.60882092 0.41770768 -0.992349057 0.90267378 0.05846822 4 4
#> 2 2 -0.03993879 -0.29191663 0.177307985 0.19166264 0.68809114 1 3
#> 3 3 0.21258366 -1.32137216 0.095100867 1.20591992 0.71337069 1 4
#> 4 4 -0.45600231 0.15722345 -1.342911517 -0.04483426 -0.88200652 3 1
#> 5 5 -0.26265544 1.05554516 1.597475032 0.69273083 2.49892542 1 4
#> 6 6 -0.96186846 -0.22440760 0.771355018 -2.26925526 -0.06761532 2 1
#> 7 7 -0.63449016 -0.11093679 -0.595893231 -0.55792782 0.19230028 2 1
#> 8 8 -0.22830986 -1.18865131 0.385917971 -0.80957399 0.01639729 1 1
#> 9 9 0.47933990 0.83421854 -0.003942906 1.46417587 2.92920490 1 4
#> 10 10 0.45900282 -0.27110043 0.240718099 0.42738833 0.14495625 1 3
#> 11 11 0.99351929 -0.64453721 0.295291812 1.09684281 0.58653605 1 4
#> 12 12 0.55270036 -2.45858836 0.988346033 0.15953661 0.15243922 1 4
#> 13 13 -0.86448608 0.94184301 0.068290097 -0.13704448 -0.15021202 4 2
#> 14 14 1.38107851 -0.55383868 -1.574886161 -0.28674662 -1.34460770 4 3
#> 15 15 -0.43396637 1.57157135 -0.172798209 -0.40580497 -0.49827386 4 1
#> 16 16 1.21745501 1.35817722 1.505147587 1.18200285 0.83653215 1 4
#> 17 17 1.65235835 -0.58187274 1.304426246 0.63139249 0.09718685 1 4
#> 18 18 0.97655253 1.04814531 -0.052738046 0.97279976 1.75444125 1 4
#> 19 19 -0.28362707 -1.86798018 0.938801245 -1.86316900 -0.51508030 3 4
#> 20 20 0.27036284 -0.03322844 -0.468561534 0.45601880 0.34120752 3 3
#> school.weight within.school.weight final.student.weight uniqueID
#> 1 2.166667 10 21.66667 student1_school1
#> 2 2.166667 10 21.66667 student2_school1
#> 3 2.166667 10 21.66667 student3_school1
#> 4 2.166667 10 21.66667 student4_school1
#> 5 2.166667 10 21.66667 student5_school1
#> 6 2.166667 10 21.66667 student6_school1
#> 7 2.166667 10 21.66667 student7_school1
#> 8 2.166667 10 21.66667 student8_school1
#> 9 2.166667 10 21.66667 student9_school1
#> 10 2.166667 10 21.66667 student10_school1
#> 11 2.166667 10 21.66667 student11_school1
#> 12 2.166667 10 21.66667 student12_school1
#> 13 2.166667 10 21.66667 student13_school1
#> 14 2.166667 10 21.66667 student14_school1
#> 15 2.166667 10 21.66667 student15_school1
#> 16 2.166667 10 21.66667 student16_school1
#> 17 2.166667 10 21.66667 student17_school1
#> 18 2.166667 10 21.66667 student18_school1
#> 19 2.166667 10 21.66667 student19_school1
#> 20 2.166667 10 21.66667 student20_school1
#>
#> $school[[2]]
#> subject q1 q2 q3 q4 q5 q6 q7
#> 1 1 -1.2524004 0.6503943 0.47821964 1.80640526 -1.7024929 5 2
#> 2 2 -1.1685278 0.3184420 -0.22492112 1.76607608 -0.6207498 5 2
#> 3 3 1.2666976 0.7314330 -0.99606827 -0.32505147 0.7070666 2 1
#> 4 4 0.6083025 0.1552508 -0.26570207 0.94286810 -0.3891286 4 2
#> 5 5 -0.2361067 -1.4781949 -1.42486083 -0.05713959 0.1642448 1 1
#> 6 6 0.7584587 -1.0139059 -0.63471219 -0.31374630 -0.5922702 2 1
#> 7 7 -1.1912318 -0.8579847 0.81198526 0.26759229 -0.2613600 1 2
#> 8 8 -0.2102216 -2.2904122 -0.60947889 -1.23702556 -1.5961741 3 1
#> 9 9 -1.3689337 -0.3059531 1.03080898 0.62210132 -1.2319243 4 2
#> 10 10 0.4471534 1.1453210 0.04601521 0.67663855 1.9990634 1 1
#> 11 11 -1.2877899 0.4653238 0.21287930 -0.35570297 0.2078158 2 1
#> 12 12 -1.0695222 1.1113533 0.45212261 0.83698230 0.2357362 2 2
#> 13 13 1.8833758 1.3036713 -1.03508208 0.54537182 0.8505015 5 1
#> 14 14 -0.2809081 -0.2895986 1.46817548 -1.81148843 0.3417622 1 1
#> 15 15 0.8315913 0.3442247 -1.02449130 -1.14027828 1.0640378 1 1
#> school.weight within.school.weight final.student.weight uniqueID
#> 1 0.8666667 33.33333 28.88889 student1_school2
#> 2 0.8666667 33.33333 28.88889 student2_school2
#> 3 0.8666667 33.33333 28.88889 student3_school2
#> 4 0.8666667 33.33333 28.88889 student4_school2
#> 5 0.8666667 33.33333 28.88889 student5_school2
#> 6 0.8666667 33.33333 28.88889 student6_school2
#> 7 0.8666667 33.33333 28.88889 student7_school2
#> 8 0.8666667 33.33333 28.88889 student8_school2
#> 9 0.8666667 33.33333 28.88889 student9_school2
#> 10 0.8666667 33.33333 28.88889 student10_school2
#> 11 0.8666667 33.33333 28.88889 student11_school2
#> 12 0.8666667 33.33333 28.88889 student12_school2
#> 13 0.8666667 33.33333 28.88889 student13_school2
#> 14 0.8666667 33.33333 28.88889 student14_school2
#> 15 0.8666667 33.33333 28.88889 student15_school2
#>
#> $school[[3]]
#> subject q1 q2 q3 q4 q5 q6 q7
#> 1 1 0.60165370 0.66719311 -0.06682375 0.67992702 0.15887760 1 1
#> 2 2 -1.28016457 0.06737430 0.04845474 0.63268192 0.67160809 2 2
#> 3 3 1.81097039 -0.43973393 -0.51677413 0.66604570 0.26963766 1 2
#> 4 4 0.47018099 -1.38688085 1.03438648 0.73940705 0.67323611 2 2
#> 5 5 0.42817937 -0.79304172 1.52656096 0.27982415 -0.31284643 1 1
#> 6 6 0.25690292 2.24161300 -0.05685417 -0.48921989 1.16906579 1 2
#> 7 7 0.65169917 1.94949932 0.58137279 -0.03457059 0.41152056 1 1
#> 8 8 0.76440534 1.06647803 -0.13421680 1.61212454 2.47801387 1 2
#> 9 9 0.09434240 -0.70701968 1.21598657 0.72232166 1.98542223 2 2
#> 10 10 -0.48465914 1.43809159 -0.55452497 0.28875515 1.03223024 1 2
#> 11 11 -0.45325590 -0.06758610 -0.29971943 0.41288182 -1.92940617 1 1
#> 12 12 0.95816454 0.54359419 2.20329359 2.24583553 1.64179539 1 1
#> 13 13 -1.18706083 1.63743035 -1.13148486 0.86988203 0.74316485 1 2
#> 14 14 -0.14871386 -0.64676670 0.40973533 -0.38731962 -1.43172768 1 2
#> 15 15 -0.08924387 0.90790059 1.70892693 -1.18252518 2.17081256 1 2
#> 16 16 -1.40381902 1.21470131 0.01800434 -1.64425081 -0.21828415 2 2
#> 17 17 -1.49298632 2.48400354 -1.73353633 -1.06613482 0.76550568 1 2
#> 18 18 -1.35409860 0.69347596 1.71440242 -1.01047804 0.86853519 2 2
#> 19 19 0.13808050 -1.07083718 -0.29408942 -0.23253230 -1.20799942 1 2
#> 20 20 -0.27911594 -0.09128051 -0.78673536 0.04053803 -0.34882055 2 1
#> 21 21 -0.04222981 -0.15264800 -0.91762592 -0.33681665 -0.74618642 1 2
#> 22 22 0.41300348 -2.39259458 1.04208844 0.13338750 -0.01451488 2 2
#> 23 23 -1.04203916 -0.44177141 0.04971279 0.97326382 -0.58258748 2 2
#> 24 24 -0.08511888 -0.01074240 -0.44585169 -0.59058568 0.44320455 1 2
#> 25 25 0.49948884 0.33596656 0.94308286 -0.07930922 0.30245402 1 2
#> school.weight within.school.weight final.student.weight uniqueID
#> 1 1.083333 16 17.33333 student1_school3
#> 2 1.083333 16 17.33333 student2_school3
#> 3 1.083333 16 17.33333 student3_school3
#> 4 1.083333 16 17.33333 student4_school3
#> 5 1.083333 16 17.33333 student5_school3
#> 6 1.083333 16 17.33333 student6_school3
#> 7 1.083333 16 17.33333 student7_school3
#> 8 1.083333 16 17.33333 student8_school3
#> 9 1.083333 16 17.33333 student9_school3
#> 10 1.083333 16 17.33333 student10_school3
#> 11 1.083333 16 17.33333 student11_school3
#> 12 1.083333 16 17.33333 student12_school3
#> 13 1.083333 16 17.33333 student13_school3
#> 14 1.083333 16 17.33333 student14_school3
#> 15 1.083333 16 17.33333 student15_school3
#> 16 1.083333 16 17.33333 student16_school3
#> 17 1.083333 16 17.33333 student17_school3
#> 18 1.083333 16 17.33333 student18_school3
#> 19 1.083333 16 17.33333 student19_school3
#> 20 1.083333 16 17.33333 student20_school3
#> 21 1.083333 16 17.33333 student21_school3
#> 22 1.083333 16 17.33333 student22_school3
#> 23 1.083333 16 17.33333 student23_school3
#> 24 1.083333 16 17.33333 student24_school3
#> 25 1.083333 16 17.33333 student25_school3
#>
#>
#> attr(,"class")
#> [1] "lsasimcluster" "list"
# Condensing the output
set.seed(0)
cluster_gen(c(2, 4))
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools
#> Total respondents: 8 (4 * 2)
#> school1 (4 students)
#> school2 (4 students)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.student.weight should add up to the number of students in the population (8)
#> $school
#> $school[[1]]
#> subject q1 q2 q3 q4 q5 q6 q7 school.weight
#> 1 1 -1.0655906 -0.61224755 -1.1575298 3 2 2 4 1
#> 2 2 -1.5637821 -0.34298582 -0.7941681 1 1 1 4 1
#> 3 3 1.1565370 0.08706035 0.7947281 1 4 2 1 1
#> 4 4 0.8320471 2.57491186 0.6156252 4 3 2 1 1
#> within.school.weight final.student.weight uniqueID
#> 1 1 1 student1_school1
#> 2 1 1 student2_school1
#> 3 1 1 student3_school1
#> 4 1 1 student4_school1
#>
#> $school[[2]]
#> subject q1 q2 q3 q4 q5 q6 q7 school.weight
#> 1 1 -0.6545846 -0.12914470 1.14664402 2 4 3 4 1
#> 2 2 1.7672873 2.42557583 -1.78660528 4 1 1 1 1
#> 3 3 0.7167075 -0.02286969 -0.04643832 3 3 2 2 1
#> 4 4 0.9101742 0.23497914 -0.34159226 2 2 2 1 1
#> within.school.weight final.student.weight uniqueID
#> 1 1 1 student1_school2
#> 2 1 1 student2_school2
#> 3 1 1 student3_school2
#> 4 1 1 student4_school2
#>
#>
#> attr(,"class")
#> [1] "lsasimcluster" "list"
set.seed(0)
cluster_gen(c(2, 4), collapse = TRUE) # same, but in one dataset
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools
#> Total respondents: 8 (4 * 2)
#> school1 (4 students)
#> school2 (4 students)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.student.weight should add up to the number of students in the population (8)
#> $school
#> subject q1 q2 q3 q4 q5 q6 q7 school.weight
#> 1 1 -1.0655906 -0.61224755 -1.15752980 3 2 2 4 1
#> 2 2 -1.5637821 -0.34298582 -0.79416806 1 1 1 4 1
#> 3 3 1.1565370 0.08706035 0.79472811 1 4 2 1 1
#> 4 4 0.8320471 2.57491186 0.61562521 4 3 2 1 1
#> 5 5 -0.6545846 -0.12914470 1.14664402 2 4 3 4 1
#> 6 6 1.7672873 2.42557583 -1.78660528 4 1 1 1 1
#> 7 7 0.7167075 -0.02286969 -0.04643832 3 3 2 2 1
#> 8 8 0.9101742 0.23497914 -0.34159226 2 2 2 1 1
#> within.school.weight final.student.weight uniqueID
#> 1 1 1 student1_school1
#> 2 1 1 student2_school1
#> 3 1 1 student3_school1
#> 4 1 1 student4_school1
#> 5 1 1 student1_school2
#> 6 1 1 student2_school2
#> 7 1 1 student3_school2
#> 8 1 1 student4_school2
#>
#> attr(,"class")
#> [1] "lsasimcluster" "list"
# Condensing the output: 3 levels
str(cluster_gen(c(2, 2, 1), collapse = "none"))
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools, classes
#> Total respondents: 8 (2 + 2 + 1 + 1 + 1 + 1)
#> school1
#> ├─school1_class1 (1 student)
#> └─school1_class2 (1 student)
#> school2
#> ├─school2_class1 (1 student)
#> └─school2_class2 (1 student)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.teacher.weight should add up to the number of teachers in the population (4)
#> - Calculating SRS weights at the class level
#> class.weight should add up to the number of classes in the population (4, counting once per class)
#> List of 2
#> $ school:List of 2
#> ..$ :'data.frame': 2 obs. of 10 variables:
#> .. ..$ subject : int [1:2] 1 2
#> .. ..$ q1 : num [1:2] 2.308 0.106
#> .. ..$ q2 : num [1:2] 0.5695 -0.0719
#> .. ..$ q3 : Factor w/ 2 levels "1","3": 2 1
#> .. ..$ q4 : Factor w/ 2 levels "4","5": 1 2
#> .. ..$ q5 : Factor w/ 1 level "3": 1 1
#> .. ..$ school.weight : num [1:2] 1 1
#> .. ..$ within.school.weight: num [1:2] 1 1
#> .. ..$ final.teacher.weight: num [1:2] 1 1
#> .. ..$ uniqueID : chr [1:2] "class1_school1" "class2_school1"
#> ..$ :'data.frame': 2 obs. of 10 variables:
#> .. ..$ subject : int [1:2] 1 2
#> .. ..$ q1 : num [1:2] 0.412 -0.381
#> .. ..$ q2 : num [1:2] 0.322 1.731
#> .. ..$ q3 : Factor w/ 2 levels "1","3": 2 1
#> .. ..$ q4 : Factor w/ 2 levels "1","5": 1 2
#> .. ..$ q5 : Factor w/ 2 levels "1","2": 2 1
#> .. ..$ school.weight : num [1:2] 1 1
#> .. ..$ within.school.weight: num [1:2] 1 1
#> .. ..$ final.teacher.weight: num [1:2] 1 1
#> .. ..$ uniqueID : chr [1:2] "class1_school2" "class2_school2"
#> $ class :List of 4
#> ..$ :'data.frame': 1 obs. of 11 variables:
#> .. ..$ subject : int 1
#> .. ..$ q1 : num -0.147
#> .. ..$ q2 : Factor w/ 1 level "4": 1
#> .. ..$ q3 : Factor w/ 1 level "1": 1
#> .. ..$ q4 : Factor w/ 1 level "2": 1
#> .. ..$ q5 : Factor w/ 1 level "2": 1
#> .. ..$ q6 : Factor w/ 1 level "2": 1
#> .. ..$ class.weight : num 1
#> .. ..$ within.class.weight : num 1
#> .. ..$ final.student.weight: num 1
#> .. ..$ uniqueID : chr "student1_class1_school1"
#> ..$ :'data.frame': 1 obs. of 11 variables:
#> .. ..$ subject : int 1
#> .. ..$ q1 : num -0.389
#> .. ..$ q2 : Factor w/ 1 level "2": 1
#> .. ..$ q3 : Factor w/ 1 level "2": 1
#> .. ..$ q4 : Factor w/ 1 level "2": 1
#> .. ..$ q5 : Factor w/ 1 level "2": 1
#> .. ..$ q6 : Factor w/ 1 level "5": 1
#> .. ..$ class.weight : num 1
#> .. ..$ within.class.weight : num 1
#> .. ..$ final.student.weight: num 1
#> .. ..$ uniqueID : chr "student1_class2_school1"
#> ..$ :'data.frame': 1 obs. of 11 variables:
#> .. ..$ subject : int 1
#> .. ..$ q1 : num 1.63
#> .. ..$ q2 : Factor w/ 1 level "1": 1
#> .. ..$ q3 : Factor w/ 1 level "1": 1
#> .. ..$ q4 : Factor w/ 1 level "1": 1
#> .. ..$ q5 : Factor w/ 1 level "1": 1
#> .. ..$ q6 : Factor w/ 1 level "2": 1
#> .. ..$ class.weight : num 1
#> .. ..$ within.class.weight : num 1
#> .. ..$ final.student.weight: num 1
#> .. ..$ uniqueID : chr "student1_class1_school2"
#> ..$ :'data.frame': 1 obs. of 11 variables:
#> .. ..$ subject : int 1
#> .. ..$ q1 : num 1.5
#> .. ..$ q2 : Factor w/ 1 level "4": 1
#> .. ..$ q3 : Factor w/ 1 level "1": 1
#> .. ..$ q4 : Factor w/ 1 level "2": 1
#> .. ..$ q5 : Factor w/ 1 level "1": 1
#> .. ..$ q6 : Factor w/ 1 level "5": 1
#> .. ..$ class.weight : num 1
#> .. ..$ within.class.weight : num 1
#> .. ..$ final.student.weight: num 1
#> .. ..$ uniqueID : chr "student1_class2_school2"
#> - attr(*, "class")= chr [1:2] "lsasimcluster" "list"
str(cluster_gen(c(2, 2, 1), collapse = "partial"))
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools, classes
#> Total respondents: 8 (2 + 2 + 1 + 1 + 1 + 1)
#> school1
#> ├─school1_class1 (1 student)
#> └─school1_class2 (1 student)
#> school2
#> ├─school2_class1 (1 student)
#> └─school2_class2 (1 student)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.teacher.weight should add up to the number of teachers in the population (4)
#> - Calculating SRS weights at the class level
#> class.weight should add up to the number of classes in the population (4, counting once per class)
#> List of 2
#> $ school:'data.frame': 4 obs. of 14 variables:
#> ..$ subject : int [1:4] 1 2 3 4
#> ..$ q1 : num [1:4] -1.856 -0.287 0.696 0.915
#> ..$ q2 : num [1:4] 2.22 0.196 -0.727 1.334
#> ..$ q3 : Factor w/ 3 levels "3","1","2": 1 1 2 3
#> ..$ q4 : Factor w/ 2 levels "3","1": 1 1 2 2
#> ..$ q5 : Factor w/ 2 levels "1","3": 1 1 1 2
#> ..$ q6 : Factor w/ 2 levels "3","4": 1 2 1 1
#> ..$ q7 : Factor w/ 2 levels "1","3": 1 1 2 2
#> ..$ q8 : Factor w/ 2 levels "2","3": 2 1 1 2
#> ..$ q9 : Factor w/ 1 level "2": 1 1 1 1
#> ..$ school.weight : num [1:4] 1 1 1 1
#> ..$ within.school.weight: num [1:4] 1 1 1 1
#> ..$ final.teacher.weight: num [1:4] 1 1 1 1
#> ..$ uniqueID : chr [1:4] "class1_school1" "class2_school1" "class1_school2" "class2_school2"
#> $ class :'data.frame': 4 obs. of 14 variables:
#> ..$ subject : int [1:4] 1 2 3 4
#> ..$ q1 : num [1:4] 0.899 0.632 1.342 -0.435
#> ..$ q2 : Factor w/ 2 levels "1","2": 1 1 2 1
#> ..$ q3 : Factor w/ 3 levels "2","4","1": 1 2 3 2
#> ..$ q4 : Factor w/ 2 levels "4","1": 1 2 2 2
#> ..$ q5 : Factor w/ 2 levels "2","4": 1 1 1 2
#> ..$ q6 : Factor w/ 2 levels "2","1": 1 2 2 2
#> ..$ q7 : Factor w/ 3 levels "2","4","1": 1 2 2 3
#> ..$ q8 : Factor w/ 4 levels "4","2","1","3": 1 2 3 4
#> ..$ q9 : Factor w/ 2 levels "1","2": 1 2 1 2
#> ..$ class.weight : num [1:4] 1 1 1 1
#> ..$ within.class.weight : num [1:4] 1 1 1 1
#> ..$ final.student.weight: num [1:4] 1 1 1 1
#> ..$ uniqueID : chr [1:4] "student1_class1_school1" "student1_class2_school1" "student1_class1_school2" "student1_class2_school2"
#> - attr(*, "class")= chr [1:2] "lsasimcluster" "list"
str(cluster_gen(c(2, 2, 1), collapse = "full"))
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools, classes
#> Total respondents: 8 (2 + 2 + 1 + 1 + 1 + 1)
#> school1
#> ├─school1_class1 (1 student)
#> └─school1_class2 (1 student)
#> school2
#> ├─school2_class1 (1 student)
#> └─school2_class2 (1 student)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.teacher.weight should add up to the number of teachers in the population (4)
#> - Calculating SRS weights at the class level
#> class.weight should add up to the number of classes in the population (4, counting once per class)
#> Classes ‘lsasimcluster’ and 'data.frame': 4 obs. of 23 variables:
#> $ subject : int 1 2 3 4
#> $ q1.student : num 1.806 0.211 0.525 -1.406
#> $ q2.student : Factor w/ 2 levels "3","4": 1 1 1 2
#> $ q3.student : Factor w/ 3 levels "3","2","1": 1 3 2 1
#> $ q4.student : Factor w/ 2 levels "3","4": 1 1 1 2
#> $ q5.student : Factor w/ 3 levels "2","3","1": 1 3 2 2
#> $ q6.student : Factor w/ 3 levels "3","2","1": 1 3 2 1
#> $ q7.student : Factor w/ 2 levels "3","1": 1 1 1 2
#> $ q8.student : Factor w/ 3 levels "2","1","3": 1 2 2 3
#> $ class.weight : num 1 1 1 1
#> $ within.class.weight : num 1 1 1 1
#> $ final.student.weight : num 1 1 1 1
#> $ uniqueID.student : chr "student1_class1_school1" "student1_class1_school2" "student1_class2_school1" "student1_class2_school2"
#> $ q1.teacher : num -2.596 0.303 1.169 -1.956
#> $ q2.teacher : Factor w/ 3 levels "1","2","3": 1 3 1 2
#> $ q3.teacher : Factor w/ 2 levels "1","2": 2 2 1 2
#> $ q4.teacher : Factor w/ 3 levels "1","2","3": 2 1 1 3
#> $ q5.teacher : Factor w/ 2 levels "1","2": 1 1 2 1
#> $ q6.teacher : Factor w/ 2 levels "1","2": 2 1 1 1
#> $ q7.teacher : Factor w/ 3 levels "1","3","5": 1 2 2 3
#> $ school.weight.teacher : num 1 1 1 1
#> $ within.school.weight.teacher: num 1 1 1 1
#> $ final.teacher.weight.teacher: num 1 1 1 1
# Controlling the intra-class correlation and the grand mean
x <- cluster_gen(c(5, 1000), rho = .9, n_X = 2, n_W = 0, c_mean = 10)
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools
#> Total respondents: 5000 (1000 * 5)
#> school1 (1000 students)
#> school2 (1000 students)
#> school3 (1000 students)
#> school4 (1000 students)
#> school5 (1000 students)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.student.weight should add up to the number of students in the population (5000)
sapply(1:5, function(s) mean(x$school[[s]]$q1)) # means per school != 10
#> [1] 15.759573 10.402853 13.179502 7.335009 16.936785
mean(sapply(1:5, function(s) mean(x$school[[s]]$q1))) # closer to c_mean
#> [1] 12.72274
# Making the intraclass variance explode by forcing "incompatible" rho and
# c_mean
x <- cluster_gen(c(5, 1000), rho = .5, n_X = 2, n_W = 0, c_mean = 1:5)
#> ── Hierarchical structure ──────────────────────────────────────────────────────
#> Sampled structure
#> Generating questionnaires for schools
#> Total respondents: 5000 (1000 * 5)
#> school1 (1000 students)
#> school2 (1000 students)
#> school3 (1000 students)
#> school4 (1000 students)
#> school5 (1000 students)
#> ── Information on sampling weights ─────────────────────────────────────────────
#> - Calculating PPS weights at the school level
#> final.student.weight should add up to the number of students in the population (5000)
anova(x)
#>
#> ANOVA table for schools, q1
#> ANOVA estimators
#> Source Sample.statistic Population.estimate
#> 1 Within-group variance 2521.982 2521.982
#> 2 Between-group variance 4905.295 4902.773
#> 3 Total variance 6444.985 NA
#>
#> Intraclass correlation
#> Estimated Standard.error
#> q1 0.6603279 0.1587615
#>
#> Testing for group differences
#> F-statistic: 1945.016 on 4 and 4995 DF. p-value: 0
#>
#> ANOVA table for schools, q2
#> ANOVA estimators
#> Source Sample.statistic Population.estimate
#> 1 Within-group variance 2474.875 2474.875
#> 2 Between-group variance 2540.197 2537.722
#> 3 Total variance 4505.458 NA
#>
#> Intraclass correlation
#> Estimated Standard.error
#> q2 0.5062689 0.1770098
#>
#> Testing for group differences
#> F-statistic: 1026.394 on 4 and 4995 DF. p-value: 0