library(flourishR)
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)The synthetic records mimic the combined annual GFS file structure.
They are not GFS observations or calibrated survey weights. In real use,
supply the path to the combined CSV to gfs_prepare().
set.seed(42)
n <- 240L
raw <- data.frame(ID = seq_len(n), COUNTRY = rep(c(3, 9, 23), each = 80),
STRATA = rep(rep(1:4, each = 20), 3),
PSU = rep(rep(1:20, each = 4), 3))
for (j in 1:10) raw[[paste0("TRAITS", j, "_Y1")]] <- sample(1:7, n, TRUE)
person <- rnorm(n)
for (w in 1:3) {
active <- if (w == 1) rep(TRUE, n) else runif(n) < .85
x <- pmin(10, pmax(0, 5 + person + rnorm(n)))
y <- pmin(10, pmax(0, 4 + .4*x + person + rnorm(n)))
raw[[paste0("WAVE_Y", w)]] <- ifelse(active, w, NA)
raw[[paste0("LONELY_Y", w)]] <- ifelse(active, x, NA)
raw[[paste0("LIFE_SAT_Y", w)]] <- ifelse(active, y, NA)
raw[[paste0("ANNUAL_WEIGHT_C", w)]] <- ifelse(active, 1, NA)
}
raw$ANNUAL_WEIGHT_L2 <- raw$ANNUAL_WEIGHT_C2
raw$ANNUAL_WEIGHT_L3 <- ifelse(!is.na(raw$WAVE_Y2) & !is.na(raw$WAVE_Y3), 1, NA)
gfs <- score_tipi(gfs_prepare(raw))Inspect documented variables and weight choices. Keep nonparticipating rows for retention; restrict item missingness to participants.
gfs_dictionary("LIFE_SAT")
#> variable year1 retention year2 year3 year3_countries person_level repeated
#> 1 LIFE_SAT TRUE FALSE TRUE TRUE <NA> FALSE TRUE
#> weight
#> 1 FALSE
gfs_countries(c(3, 9, 23))
#> [1] "Brazil" "Japan" "Sweden"
gfs_weights(1:3)
#> [1] "ANNUAL_WEIGHT_L3"
missing <- check_missingness(gfs[!is.na(gfs$WAVE), ], c("LIFE_SAT", "LONELY"), by = c("COUNTRY", "wave"))
head(missing)
#> COUNTRY wave variable n_missing pct_missing n
#> 1 Brazil 1 LIFE_SAT 0 0 80
#> 2 Brazil 1 LONELY 0 0 80
#> 3 Brazil 2 LIFE_SAT 0 0 70
#> 4 Brazil 2 LONELY 0 0 70
#> 5 Brazil 3 LIFE_SAT 0 0 64
#> 6 Brazil 3 LONELY 0 0 64
plot_missingness(missing)retention <- check_retention(gfs, group = "COUNTRY")
plot_retention(retention, group = "COUNTRY", cumulative = TRUE)Use cross-sectional weights for annual estimates. The synthetic design has multiple PSUs per stratum. For real data, choose the singleton-PSU variance method based on the sampling design; no adjustment is imposed by default.
if (requireNamespace("survey", quietly = TRUE)) {
design <- gfs_design(gfs, "Japan", waves = 3)
survey::svymean(~LIFE_SAT, design, na.rm = TRUE)
means <- describe_countries(gfs, "LIFE_SAT", wave = 3)
print(plot_cross_national(means, "COUNTRY", "LIFE_SAT", estimates = TRUE))
trajectories <- do.call(rbind, lapply(1:3, function(w) describe_countries(gfs, "LIFE_SAT", wave = w)))
print(plot_longitudinal(trajectories, "ID", "wave", "LIFE_SAT", country_var = "COUNTRY", estimates = TRUE))
results <- loop_countries(gfs, "COUNTRY", formula = LIFE_SAT ~ LONELY, wave = 3, weighted = TRUE)
print(country_moderation(results, "LONELY"))
if (requireNamespace("metafor", quietly = TRUE)) {
pooled <- meta_countries(results, "LONELY")
print(plot_country_effects(results, "LONELY", pooled = pooled))
}
}#> term k Q df p.value
#> 1 LONELY 3 7.260371 2 0.02651127
Lags refer to exact annual waves. Prospective regressions use the longitudinal weight at the endpoint. Within-between variables use observed values in the supplied panel, not necessarily the final model sample. Mixed models are unweighted Gaussian participant random-intercept models.
panel <- create_lags(gfs, c("LIFE_SAT", "LONELY"))
changes <- change_scores(gfs, "LIFE_SAT")
head(changes)
#> ID COUNTRY wave STRATA PSU TRAITS1_Y1 TRAITS2_Y1 TRAITS3_Y1 TRAITS4_Y1
#> 3 1 Brazil 3 1 1 1 5 5 5
#> 6 2 Brazil 3 1 1 5 4 6 2
#> 9 3 Brazil 3 1 1 1 1 5 4
#> 12 4 Brazil 3 1 1 1 6 3 3
#> 15 5 Brazil 3 1 2 2 3 5 2
#> 18 6 Brazil 3 1 2 4 4 2 2
#> TRAITS5_Y1 TRAITS6_Y1 TRAITS7_Y1 TRAITS8_Y1 TRAITS9_Y1 TRAITS10_Y1
#> 3 7 2 6 5 7 5
#> 6 4 7 6 6 7 3
#> 9 5 1 3 7 7 4
#> 12 1 4 4 5 1 1
#> 15 7 7 5 4 4 1
#> 18 7 4 1 4 5 2
#> ANNUAL_WEIGHT_C1 ANNUAL_WEIGHT_C2 ANNUAL_WEIGHT_C3 ANNUAL_WEIGHT_L2
#> 3 1 1 1 1
#> 6 1 1 1 1
#> 9 1 1 1 1
#> 12 1 1 1 1
#> 15 1 1 1 1
#> 18 1 1 NA 1
#> ANNUAL_WEIGHT_L3 WAVE LONELY LIFE_SAT extraversion agreeableness
#> 3 1 3 2.914448 6.018170 4.5 3.5
#> 6 1 3 4.809414 6.404043 5.0 3.0
#> 9 1 3 4.780578 4.610318 4.0 3.0
#> 12 1 3 6.012764 4.867914 5.5 5.0
#> 15 1 3 5.601320 5.528738 6.5 3.0
#> 18 NA NA NA NA 4.0 5.5
#> conscientiousness emotional_stability openness LIFE_SAT_change_1_3
#> 3 4.0 3.0 3.0 0.1881599
#> 6 4.0 1.5 3.5 -1.0232193
#> 9 5.0 2.5 3.5 -1.8351809
#> 12 5.0 5.0 4.0 -0.3627130
#> 15 3.5 3.0 1.0 -0.7459685
#> 18 5.0 2.5 1.5 NA
panel <- within_between(panel, "LONELY")
if (requireNamespace("survey", quietly = TRUE)) {
prospective <- loop_longitudinal(panel, LIFE_SAT ~ LONELY_lag1 + LIFE_SAT_lag1)
print(prospective)
}
#> COUNTRY term estimate std.error conf.low conf.high n status
#> 1 Brazil (Intercept) 3.7005190 1.0719068 1.40150742 5.9995305 56 ok
#> 2 Brazil LONELY_lag1 -0.1856120 0.1892251 -0.59145960 0.2202355 56 ok
#> 3 Brazil LIFE_SAT_lag1 0.5407219 0.1863815 0.14097339 0.9404704 56 ok
#> 4 Japan (Intercept) 1.6789747 0.8775843 -0.20325639 3.5612058 63 ok
#> 5 Japan LONELY_lag1 0.2297855 0.1180303 -0.02336433 0.4829354 63 ok
#> 6 Japan LIFE_SAT_lag1 0.4428548 0.1082312 0.21072197 0.6749876 63 ok
#> 7 Sweden (Intercept) 1.6304377 0.7665289 -0.01360320 3.2744786 54 ok
#> 8 Sweden LONELY_lag1 0.2984839 0.1651931 -0.05581998 0.6527879 54 ok
#> 9 Sweden LIFE_SAT_lag1 0.4558070 0.1023123 0.23636903 0.6752451 54 ok
#> warning weight lonely_psu
#> 1 ANNUAL_WEIGHT_L3 fail
#> 2 ANNUAL_WEIGHT_L3 fail
#> 3 ANNUAL_WEIGHT_L3 fail
#> 4 ANNUAL_WEIGHT_L3 fail
#> 5 ANNUAL_WEIGHT_L3 fail
#> 6 ANNUAL_WEIGHT_L3 fail
#> 7 ANNUAL_WEIGHT_L3 fail
#> 8 ANNUAL_WEIGHT_L3 fail
#> 9 ANNUAL_WEIGHT_L3 fail
if (requireNamespace("lme4", quietly = TRUE)) {
mixed <- loop_longitudinal(panel, LIFE_SAT ~ LONELY_within + LONELY_between + factor(wave), model = "mixed")
print(table(mixed$status))
}
#>
#> ok
#> 15Join one contextual record per country. Meta-analysis pools comparable country coefficients, not population-weighted global effects. With only three synthetic countries, meta-regression is a software demonstration, not substantive inference.
context <- data.frame(COUNTRY = c("Brazil", "Japan", "Sweden"), context_index = c(2, 4, 3))
if (exists("results", inherits = FALSE) && requireNamespace("metafor", quietly = TRUE)) {
linked <- join_country_data(results, context)
meta_countries(linked, "LONELY", moderators = ~context_index)
}
#> term moderator k estimate std.error conf.low conf.high p.value
#> 1 LONELY intrcpt 3 0.64787418 0.5773188 -6.687657 7.983405 0.4633793
#> 2 LONELY context_index 3 0.05200076 0.1836397 -2.281362 2.385364 0.8243290
#> tau2 I2 Q p.heterogeneity
#> 1 0.05376613 86.20669 7.249894 0.00709052
#> 2 0.05376613 86.20669 7.249894 0.00709052