Generates a simulated dataset of WaszKrak residents scored by the
fictional LifeCalc algorithm. The function reproduces the same correlation
structure as the bundled lifecalc dataset but allows changing n and
seed for experiments, teaching, or simulation studies.
The true model for SocialScore is sparse — dominated by DistrictScore
and prior_flag — but the dataset contains 22 additional correlated
proxy variables that illustrate multicollinearity and the need for
regularisation.
Value
A data frame with n rows and 25 columns. See lifecalc for
full variable documentation.
Examples
# Reproduce the bundled dataset exactly
df <- generate_lifecalc(n = 5000, seed = 2047)
all.equal(df, lifecalc) # TRUE
#> [1] TRUE
# Generate a smaller dataset for quick experiments
df_small <- generate_lifecalc(n = 500, seed = 42)
dim(df_small)
#> [1] 500 25
# Demonstrate that LASSO recovers the true predictors
if (requireNamespace("glmnet", quietly = TRUE)) {
X <- model.matrix(SocialScore ~ ., data = df_small)[, -1]
y <- df_small$SocialScore
cv <- glmnet::cv.glmnet(X, y, alpha = 1, nfolds = 10)
coefs <- coef(cv, s = "lambda.min")
coefs[coefs[, 1] != 0, , drop = FALSE]
}
#> 20 x 1 sparse Matrix of class "dgCMatrix"
#> lambda.min
#> (Intercept) 8.512570e+00
#> age -8.352042e-02
#> gender -2.883562e+00
#> DistrictScore 3.387468e-01
#> prior_flag -1.337650e+01
#> EducationScore 8.444763e-02
#> EmploymentScore 1.929397e-01
#> CuriosityScore 3.766723e-02
#> NetworkScore 1.821643e-02
#> ConsumptionScore 1.203577e-02
#> MobilityScore 8.459681e-02
#> GeneticRiskScore -1.523476e-02
#> NutritionScore 4.205501e-02
#> SleepScore 5.735564e-02
#> StressIndex -1.967439e-02
#> RecoveryScore 1.616743e-02
#> ChronicLoadScore -4.519715e-02
#> MedicalScore 3.520275e-02
#> ComplianceScore 8.264127e-05
#> AttentionScore 7.064233e-03