Master Thesis:
  • Proposal
  • Concepts
  • Sketchbooks

On this page

  • Experiences Example
    • General definitions
    • Feature 1
    • Feature 2
    • Feature 3
    • Feature 4
    • Feature 5
    • Feature 6
    • Feature 7
    • Feature 8
    • Generating situational parameters
  • Testing results
    • Helper functions
    • Aggregation of distribution parameters
    • Results for objective features
    • Results for cognitive and affective interpreted features (Self-Concept)
    • Results for real Self-Concept and ideal Self-Concept
    • Results for Behavior (Habituation)
  • < Back

Experiences

Author

Hubert Bächli

Published

21.01.2026

Experiences Example

General definitions

nstep <- 1e5
trait <- "N" # Neuroticism
set.seed(1)

Feature 1

For Feature 1, it is assumed that there is neither cognitive bias nor any preference. Accordingly, both distributions are uniform.

facet <- "A" # Anxiety
feature <- 1
weight <- 0.5

exp_1 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
mean_1 <- 0.5
pvar_1 <- 1
par_1 <- calc_beta_par(mean_1, pvar_1, output = c("a", "b"))
par_1
  a b
1 1 1
exp_1$cog <- qbeta(pbeta(exp_1$obj, 1, 1), par_1$a, par_1$b)
exp_1$aff <- features_dist(exp_1$cog, mean_1, pvar_1, output = "probx")
head(exp_1)
  step sit trait facet feature weight         obj         cog aff sit_aff
1    1   1     N     A       1    0.5 0.00000e+00 0.00000e+00 0.5      NA
2    2   2     N     A       1    0.5 1.00001e-05 1.00001e-05 0.5      NA
3    3   3     N     A       1    0.5 2.00002e-05 2.00002e-05 0.5      NA
4    4   4     N     A       1    0.5 3.00003e-05 3.00003e-05 0.5      NA
5    5   5     N     A       1    0.5 4.00004e-05 4.00004e-05 0.5      NA
6    6   6     N     A       1    0.5 5.00005e-05 5.00005e-05 0.5      NA
  sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA          NA          NA      NA
2      NA          NA          NA      NA
3      NA          NA          NA      NA
4      NA          NA          NA      NA
5      NA          NA          NA      NA
6      NA          NA          NA      NA

Feature 2

For Feature 2, it is again assumed that no cognitive bias is present. However, a preference with a defined tendency (mean=0.4) and variance (pvar=0.6) is specified.

facet <- "A" # Anxiety
feature <- 2
weight <- 0.5

exp_2 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
mean_2 <- 0.4
pvar_2 <- 0.6
par_2 <- calc_beta_par(mean_2, pvar_2, output = c("a", "b"))
par_2
         a   b
1 1.933333 2.9
exp_2$cog <- qbeta(pbeta(exp_2$obj, 1, 1), par_1$a, par_1$b)
exp_2$aff <- features_dist(exp_2$cog, mean_2, pvar_2, output = "probx")
head(exp_2)
  step sit trait facet feature weight         obj         cog          aff
1    1   1     N     A       2    0.5 0.00000e+00 0.00000e+00 0.0000000000
2    2   2     N     A       2    0.5 1.00001e-05 1.00001e-05 0.0002267586
3    3   3     N     A       2    0.5 2.00002e-05 2.00002e-05 0.0004329395
4    4   4     N     A       2    0.5 3.00003e-05 3.00003e-05 0.0006319524
5    5   5     N     A       2    0.5 4.00004e-05 4.00004e-05 0.0008264205
6    6   6     N     A       2    0.5 5.00005e-05 5.00005e-05 0.0010175578
  sit_aff sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA      NA          NA          NA      NA
2      NA      NA          NA          NA      NA
3      NA      NA          NA          NA      NA
4      NA      NA          NA          NA      NA
5      NA      NA          NA          NA      NA
6      NA      NA          NA          NA      NA

Feature 3

For Feature 3, the situation is reversed: no preference is defined, but a cognitive bias is specified (mean=0.45, pvar=0.6).

facet <- "D" # Depression
feature <- 1
weight <- 0.5

exp_3 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
mean_3 <- 0.45
pvar_3 <- 0.6
par_3 <- calc_beta_par(mean_3, pvar_3, output = c("a", "b"))
par_3
         a        b
1 1.966667 2.403704
exp_3$cog <- qbeta(pbeta(exp_3$obj, 1, 1) , par_3$a, par_3$b)
exp_3$aff <- features_dist(exp_3$cog, mean_1, pvar_1, output = "probx")
head(exp_3)
  step sit trait facet feature weight         obj         cog aff sit_aff
1    1   1     N     D       1    0.5 0.00000e+00 0.000000000 0.5      NA
2    2   2     N     D       1    0.5 1.00001e-05 0.001412874 0.5      NA
3    3   3     N     D       1    0.5 2.00002e-05 0.002010446 0.5      NA
4    4   4     N     D       1    0.5 3.00003e-05 0.002471298 0.5      NA
5    5   5     N     D       1    0.5 4.00004e-05 0.002861104 0.5      NA
6    6   6     N     D       1    0.5 5.00005e-05 0.003205389 0.5      NA
  sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA          NA          NA      NA
2      NA          NA          NA      NA
3      NA          NA          NA      NA
4      NA          NA          NA      NA
5      NA          NA          NA      NA
6      NA          NA          NA      NA

Feature 4

For Feature 4, both a cognitive bias (mean=0.4, pvar=0.6) and a preference (mean=0.55, pvar=0.4) are specified.

facet <- "D" # Depression
feature <- 2
weight <- 0.33

exp_4 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
mean_4 <- 0.4
pvar_4 <- 0.6
par_4 <- calc_beta_par(mean_4, pvar_4, output = c("a", "b"))
par_4
         a   b
1 1.933333 2.9
mean_5 <- 0.55
pvar_5 <- 0.4
par_5 <- calc_beta_par(mean_5, pvar_5, output = c("a", "b"))
par_5
         a     b
1 3.880556 3.175
exp_4$cog <- qbeta(pbeta(exp_4$obj, 1, 1), par_4$a, par_4$b)
exp_4$aff <- features_dist(exp_4$cog, mean_5, pvar_5, output = "probx")
head(exp_4)
  step sit trait facet feature weight         obj         cog          aff
1    1   1     N     D       2   0.33 0.00000e+00 0.000000000 0.000000e+00
2    2   2     N     D       2   0.33 1.00001e-05 0.001079931 1.855752e-07
3    3   3     N     D       2   0.33 2.00002e-05 0.001546081 5.211639e-07
4    4   4     N     D       2   0.33 3.00003e-05 0.001907286 9.534352e-07
5    5   5     N     D       2   0.33 4.00004e-05 0.002213735 1.463528e-06
6    6   6     N     D       2   0.33 5.00005e-05 0.002485008 2.040584e-06
  sit_aff sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA      NA          NA          NA      NA
2      NA      NA          NA          NA      NA
3      NA      NA          NA          NA      NA
4      NA      NA          NA          NA      NA
5      NA      NA          NA          NA      NA
6      NA      NA          NA          NA      NA

Feature 5

Feature 5 is a copy of Feature 1, but with the modification that the objective experiences are no longer uniformly distributed.

facet <- "E" # Emotional Volatility
feature <- 1
weight <- 0.25

exp_5 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = qbeta(seq(0, 1, length.out = nstep), 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
exp_5$cog <- qbeta(pbeta(exp_5$obj, 2, 2) , par_1$a, par_1$b)
exp_5$aff <- features_dist(exp_5$cog, mean_1, pvar_1, output = "probx")
head(exp_5)
  step sit trait facet feature weight         obj         cog aff sit_aff
1    1   1     N     E       1   0.25 0.000000000 0.00000e+00 0.5      NA
2    2   2     N     E       1   0.25 0.001826864 1.00001e-05 0.5      NA
3    3   3     N     E       1   0.25 0.002584229 2.00002e-05 0.5      NA
4    4   4     N     E       1   0.25 0.003165636 3.00003e-05 0.5      NA
5    5   5     N     E       1   0.25 0.003655960 4.00004e-05 0.5      NA
6    6   6     N     E       1   0.25 0.004088078 5.00005e-05 0.5      NA
  sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA          NA          NA      NA
2      NA          NA          NA      NA
3      NA          NA          NA      NA
4      NA          NA          NA      NA
5      NA          NA          NA      NA
6      NA          NA          NA      NA

Feature 6

Feature 6 is a copy of Feature 4, but also with the modification that the objective experiences are no longer uniformly distributed.

facet <- "E" # Emotional Volatility
feature <- 2
weight <- 0.25

exp_6 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = qbeta(seq(0, 1, length.out = nstep), 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
exp_6$cog <- qbeta(pbeta(exp_6$obj, 2, 2), par_4$a, par_4$b)
exp_6$aff <- features_dist(exp_6$cog, mean_5, pvar_5, output = "probx")
head(exp_6)
  step sit trait facet feature weight         obj         cog          aff
1    1   1     N     E       2   0.25 0.000000000 0.000000000 0.000000e+00
2    2   2     N     E       2   0.25 0.001826864 0.001079931 1.855752e-07
3    3   3     N     E       2   0.25 0.002584229 0.001546081 5.211639e-07
4    4   4     N     E       2   0.25 0.003165636 0.001907286 9.534352e-07
5    5   5     N     E       2   0.25 0.003655960 0.002213735 1.463528e-06
6    6   6     N     E       2   0.25 0.004088078 0.002485008 2.040584e-06
  sit_aff sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA      NA          NA          NA      NA
2      NA      NA          NA          NA      NA
3      NA      NA          NA          NA      NA
4      NA      NA          NA          NA      NA
5      NA      NA          NA          NA      NA
6      NA      NA          NA          NA      NA

Feature 7

Feature 7 is a copy of Feature 6, but with the addition that the objective experiences are randomly generated.

facet <- "E" # Emotional Volatility
feature <- 3
weight <- 0.25

exp_7 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = rbeta(nstep, 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
exp_7$cog <- qbeta(pbeta(exp_7$obj, 2, 2), par_4$a, par_4$b)
exp_7$aff <- features_dist(exp_7$cog, mean_5, pvar_5, output = "probx")
head(exp_7)
  step sit trait facet feature weight       obj       cog       aff sit_aff
1    1   1     N     E       3   0.25 0.3275025 0.2411078 0.3732068      NA
2    2   2     N     E       3   0.25 0.5516990 0.4311386 0.6291958      NA
3    3   3     N     E       3   0.25 0.2743131 0.1990916 0.2783129      NA
4    4   4     N     E       3   0.25 0.8814780 0.7784175 0.5448828      NA
5    5   5     N     E       3   0.25 0.5923401 0.4683281 0.6502303      NA
6    6   6     N     E       3   0.25 0.2780523 0.2020115 0.2851923      NA
  sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA          NA          NA      NA
2      NA          NA          NA      NA
3      NA          NA          NA      NA
4      NA          NA          NA      NA
5      NA          NA          NA      NA
6      NA          NA          NA      NA

Feature 8

The final Feature 8 is a copy of Feature 7, with the modification that the cognitive distribution uses a pvar greater than 1 (specifically, 1.6).

facet <- "E" # Emotional Volatility
feature <- 4
weight <- 0.25

exp_8 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = rbeta(nstep, 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
mean_6 <- 0.4
pvar_6 <- 1.6
par_6 <- calc_beta_par(mean_6, pvar_6, output = c("a", "b"))
par_6
      a      b
1 0.475 0.7125
exp_8$cog <- qbeta(pbeta(exp_8$obj, 2, 2), par_6$a, par_6$b)
exp_8$aff <- features_dist(exp_8$cog, mean_5, pvar_5, output = "probx")
head(exp_8)
  step sit trait facet feature weight       obj        cog         aff sit_aff
1    1   1     N     E       4   0.25 0.5837254 0.50667818 0.664617629      NA
2    2   2     N     E       4   0.25 0.2829150 0.04884107 0.009692856      NA
3    3   3     N     E       4   0.25 0.5027416 0.33846776 0.539896739      NA
4    4   4     N     E       4   0.25 0.2458955 0.02893792 0.002261821      NA
5    5   5     N     E       4   0.25 0.3522969 0.10738620 0.076186716      NA
6    6   6     N     E       4   0.25 0.3338101 0.08885347 0.047592014      NA
  sit_beh sit_acc_pos sit_acc_neg sit_acc
1      NA          NA          NA      NA
2      NA          NA          NA      NA
3      NA          NA          NA      NA
4      NA          NA          NA      NA
5      NA          NA          NA      NA
6      NA          NA          NA      NA

Generating situational parameters

exp <- bind_rows(exp_1, exp_2, exp_3, exp_4, exp_5, exp_6, exp_7, exp_8)

exp <- exp %>%
    group_by(step) %>%
        mutate(sit_aff = mean(aff),
               sit_beh = rbinom(1, 1, sit_aff),
               sit_acc_pos = sit_aff,
               sit_acc_neg = 1 - sit_aff,
               acc = 1 - 2 * (sit_acc_pos * sit_acc_neg)              
              ) %>%
    ungroup() %>%
    arrange(step)

head(exp, 12)
# A tibble: 12 × 15
    step   sit trait facet feature weight       obj       cog        aff sit_aff
   <int> <int> <chr> <chr> <fct>    <dbl>     <dbl>     <dbl>      <dbl>   <dbl>
 1     1     1 N     A     1         0.5  0         0            5   e-1   0.317
 2     1     1 N     A     2         0.5  0         0            0         0.317
 3     1     1 N     D     1         0.5  0         0            5   e-1   0.317
 4     1     1 N     D     2         0.33 0         0            0         0.317
 5     1     1 N     E     1         0.25 0         0            5   e-1   0.317
 6     1     1 N     E     2         0.25 0         0            0         0.317
 7     1     1 N     E     3         0.25 0.328     0.241        3.73e-1   0.317
 8     1     1 N     E     4         0.25 0.584     0.507        6.65e-1   0.317
 9     2     2 N     A     1         0.5  0.0000100 0.0000100    5   e-1   0.267
10     2     2 N     A     2         0.5  0.0000100 0.0000100    2.27e-4   0.267
11     2     2 N     D     1         0.5  0.0000100 0.00141      5   e-1   0.267
12     2     2 N     D     2         0.33 0.0000100 0.00108      1.86e-7   0.267
# ℹ 5 more variables: sit_beh <int>, sit_acc_pos <dbl>, sit_acc_neg <dbl>,
#   sit_acc <lgl>, acc <dbl>

Testing results

Helper functions

round_res <- function(res, size = 2) {
  res[] <- lapply(res, function(x) {
    if (is.numeric(x)) round(x, size) else x
  })
  res
}

scaled_mean_sd <- function(x, x_prob = NULL, w = 1, eps = 1e-6, na.rm = FALSE) {
    if (length(w) == 1) {
        w <- rep(w, length(x))
    }

    if (is.null(x_prob)) {
        x_prob <- x
    }
    
    if (length(x_prob) == 1) {
        x_prob <- rep(x_prob, length(x))
    }
    
    if (na.rm) {
        ok <- is.finite(x) & is.finite(x_prob) & is.finite(w)
        x <- x[ok]
        x_prob <- x_prob[ok]
        w <- w[ok]
    }

    x_mu <- mean(x_prob)
    x_mu <- pmin(pmax(x_mu, eps), 1 - eps)
    x_var <-  var(x_prob)
    prec <- (x_mu * (1 - x_mu)) / x_var - 1
    a <- prec * x_mu
    b <- prec * (1 - x_mu)
    prob <- dbeta(x_prob, a, b)
    prob <- pmax(prob, eps)

    w <- pmax(w, eps)

    W <- w / prob
    W_sum <- sum(W)

    mu <- sum(W * x) / W_sum
    sd <- sqrt(sum(W * (x - mu)^2) / W_sum)
    
    list(mean = mu, sd = sd)
}

Aggregation of distribution parameters

res <- exp %>%
    group_by(trait, facet, feature) %>%
        summarise(
            obj_mean = mean(obj, na.rm = TRUE),
            obj_sd   = sd(obj, na.rm = TRUE),
            cog_mean = mean(cog, na.rm = TRUE),
            cog_sd = sd(cog, na.rm = TRUE),
            aff_mean = scaled_mean_sd(x = cog, w = aff, na.rm = TRUE)$mean,
            aff_sd = scaled_mean_sd(x = cog, w = aff, na.rm = TRUE)$sd,
            aff_mean_1 = scaled_mean_sd(x = cog, w = aff * sit_beh, na.rm = TRUE)$mean,
            aff_sd_1 = scaled_mean_sd(x = cog, w = aff * sit_beh, na.rm = TRUE)$sd,
            aff_mean_0 = scaled_mean_sd(x = cog, w = aff * (1-sit_beh), na.rm = TRUE)$mean,
            aff_sd_0 = scaled_mean_sd(x = cog, w = aff * (1-sit_beh), na.rm = TRUE)$sd,
            beh_mean = scaled_mean_sd(x = cog, x_prob = 1, w = sit_beh, na.rm = TRUE)$mean,
            beh_sd   = scaled_mean_sd(x = cog, x_prob = 1, w = sit_beh, na.rm = TRUE)$sd,
            .groups = "drop"
        ) %>%
        mutate(
            obj_var_max = obj_mean * (1 - obj_mean) / (pmax(1 / obj_mean,1 / (1 - obj_mean)) + 1),
            obj_pvar = pmax(obj_sd^2 / obj_var_max, 0.01),
            cog_var_max = cog_mean * (1 - cog_mean) / (pmax(1 / cog_mean,1 / (1 - cog_mean)) + 1),
            cog_pvar = pmax(cog_sd^2 / cog_var_max, 0.01),
            aff_var_max = aff_mean * (1 - aff_mean) / (pmax(1 / aff_mean,1 / (1 - aff_mean)) + 1),
            aff_pvar = pmax(pmin(aff_sd^2 / aff_var_max, 1), 0.01), # limitet to max = 1
            aff_var_max_1 = aff_mean_1 * (1 - aff_mean_1) / (pmax(1 / aff_mean_1,1 / (1 - aff_mean_1)) + 1),
            aff_pvar_1 = pmax(pmin(aff_sd_1^2 / aff_var_max_1, 1), 0.01), # limitet to max = 1
            aff_var_max_0 = aff_mean_0 * (1 - aff_mean_0) / (pmax(1 / aff_mean_0,1 / (1 - aff_mean_0)) + 1),
            aff_pvar_0 = pmax(pmin(aff_sd_0^2 / aff_var_max_0, 1), 0.01), # limitet to max = 1
            beh_var_max = beh_mean * (1 - beh_mean) / (pmax(1 / beh_mean,1 / (1 - beh_mean)) + 1),
            beh_pvar = pmax(beh_sd^2 / beh_var_max, 0.01)
        )

Results for objective features

round_res(res[, c("trait", "facet", "feature", "obj_mean", "obj_pvar")], 2)
# A tibble: 8 × 5
  trait facet feature obj_mean obj_pvar
  <chr> <chr> <fct>      <dbl>    <dbl>
1 N     A     1            0.5      1  
2 N     A     2            0.5      1  
3 N     D     1            0.5      1  
4 N     D     2            0.5      1  
5 N     E     1            0.5      0.6
6 N     E     2            0.5      0.6
7 N     E     3            0.5      0.6
8 N     E     4            0.5      0.6

Results for cognitive and affective interpreted features (Self-Concept)

goal <- data.frame(
    cog_mean = c(mean_1, mean_1, mean_3, mean_4, mean_1, mean_4, mean_4, mean_6),
    cog_pvar = c(pvar_1, pvar_1, pvar_3, pvar_4, pvar_1, pvar_4, pvar_4, pvar_6),
    aff_mean = c(mean_1, mean_2, mean_1, mean_5, mean_1, mean_5, mean_5, mean_5),
    aff_pvar = c(pvar_1, pvar_2, pvar_1, pvar_5, pvar_1, pvar_5, pvar_5, pvar_5)
)
goal
  cog_mean cog_pvar aff_mean aff_pvar
1     0.50      1.0     0.50      1.0
2     0.50      1.0     0.40      0.6
3     0.45      0.6     0.50      1.0
4     0.40      0.6     0.55      0.4
5     0.50      1.0     0.50      1.0
6     0.40      0.6     0.55      0.4
7     0.40      0.6     0.55      0.4
8     0.40      1.6     0.55      0.4
round_res(res[, c("trait", "facet", "feature", "cog_mean", "cog_pvar", "aff_mean", "aff_pvar")], 3)
# A tibble: 8 × 7
  trait facet feature cog_mean cog_pvar aff_mean aff_pvar
  <chr> <chr> <fct>      <dbl>    <dbl>    <dbl>    <dbl>
1 N     A     1          0.5      1        0.5      1    
2 N     A     2          0.5      1        0.421    0.713
3 N     D     1          0.45     0.6      0.5      1    
4 N     D     2          0.4      0.6      0.543    0.526
5 N     E     1          0.5      1        0.5      1    
6 N     E     2          0.4      0.6      0.543    0.526
7 N     E     3          0.4      0.604    0.543    0.529
8 N     E     4          0.402    1.60     0.543    0.526

Results for real Self-Concept and ideal Self-Concept

round_res(res[, c("trait", "facet", "feature", "aff_mean_1", "aff_pvar_1", "aff_mean_0", "aff_pvar_0")], 3)
# A tibble: 8 × 7
  trait facet feature aff_mean_1 aff_pvar_1 aff_mean_0 aff_pvar_0
  <chr> <chr> <fct>        <dbl>      <dbl>      <dbl>      <dbl>
1 N     A     1            0.516      0.926      0.486      1    
2 N     A     2            0.439      0.644      0.405      0.773
3 N     D     1            0.505      0.861      0.5        1    
4 N     D     2            0.533      0.471      0.551      0.574
5 N     E     1            0.516      0.926      0.486      1    
6 N     E     2            0.533      0.471      0.551      0.574
7 N     E     3            0.545      0.505      0.542      0.55 
8 N     E     4            0.544      0.509      0.542      0.542

Results for Behavior (Habituation)

round_res(res[, c("trait", "facet", "feature", "beh_mean", "beh_pvar")], 2)
# A tibble: 8 × 5
  trait facet feature beh_mean beh_pvar
  <chr> <chr> <fct>      <dbl>    <dbl>
1 N     A     1           0.52     0.93
2 N     A     2           0.52     0.93
3 N     D     1           0.46     0.52
4 N     D     2           0.41     0.52
5 N     E     1           0.52     0.93
6 N     E     2           0.41     0.52
7 N     E     3           0.41     0.57
8 N     E     4           0.41     1.49

< Back

Back to top
Source Code
---
title: "Experiences"
author: "Hubert Bächli"
date: last-modified
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(here)
library(tidyverse)

calc_beta_par <- function(tendencies, pvar, output = NULL, eps = 1e-6) {
    len <- length(tendencies * pvar)
    df <- data.frame(ten = rep(0, len))
    
    df$ten <- pmin(pmax(tendencies, eps), 1 - eps)
    df$pvar <- pmax(pmin(pvar, 2), eps)
    
    df$ab_min <- pmax(1/df$ten,1/(1-df$ten))
    df$var_max <- df$ten * (1 - df$ten) / (df$ab_min + 1)
    
    df$var <- df$pvar * df$var_max
    
    df$prec <- (df$ten * (1 - df$ten)) / df$var - 1
    
    df$a <- df$prec * df$ten
    df$b <- df$prec * (1 - df$ten)

    if (is.null(output)) {
        return(df)
    } else {
        missing <- setdiff(output, colnames(df))
        if (length(missing) > 0) {
            stop("Unknown output columns: ", paste(missing, collapse = ", "))
        } else {
            df[, output]            
        }
    }
}

features_dist <- function(x, tendencies, pvar, output = NULL, eps = 1e-6) {
    par <- calc_beta_par(tendencies, pvar, output = c("a","b"), eps)
    lenpar <- length(par$a)
    lenx <- length(x)
    df <- data.frame(
        set = rep(1:lenpar, each = lenx),
        x = rep(x,lenpar),
        a = rep(par$a, each = lenx),
        b = rep(par$b, each = lenx)
    )
    df$freq <- dbeta(df$x, df$a, df$b)
    df$mode <- (df$a - 1) / (df$a + df$b - 2)
    df$mode <- pmin(pmax(df$mode, 0), 1)
    df$max <- ifelse(is.na(df$mode), 
                     df$freq, 
                     dbeta(df$mode, df$a, df$b))
    df$probx <- df$freq / df$max
    df$prob <- pbeta(df$x, df$a, df$b)

    if (is.null(output)) {
        return(df)
    } else {
        missing <- setdiff(output, colnames(df))
        if (length(missing) > 0) {
            stop("Unknown output columns: ", paste(missing, collapse = ", "))
        } else {
            df[, output]            
        }
    }
}

features_dist <- function(x, tendencies, pvar, output = NULL, eps = 1e-6) {
    par <- calc_beta_par(tendencies, pvar, output = c("a", "b", "pvar"), eps)
    lenpar <- length(par$a)
    lenx <- length(x)
    df <- data.frame(
        set = rep(1:lenpar, each = lenx),
        x = rep(x,lenpar),
        a = rep(par$a, each = lenx),
        b = rep(par$b, each = lenx),
        pvar = rep(par$pvar, each = lenx)
    )
    df$freq <- dbeta(df$x, df$a, df$b)
    df$prob <- pbeta(df$x, df$a, df$b)
    df$probx <- df$freq / (1 + df$freq) 
    
    if (is.null(output)) {
        return(df)
    } else {
        missing <- setdiff(output, colnames(df))
        if (length(missing) > 0) {
            stop("Unknown output columns: ", paste(missing, collapse = ", "))
        } else {
            df[, output]            
        }
    }
}
```

# Experiences Example

## General definitions

```{r}
nstep <- 1e5
trait <- "N" # Neuroticism
set.seed(1)
```

## Feature 1

For Feature 1, it is assumed that there is neither cognitive bias nor any preference. Accordingly, both distributions are uniform.

```{r}
facet <- "A" # Anxiety
feature <- 1
weight <- 0.5

exp_1 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
mean_1 <- 0.5
pvar_1 <- 1
par_1 <- calc_beta_par(mean_1, pvar_1, output = c("a", "b"))
par_1
```

```{r}
exp_1$cog <- qbeta(pbeta(exp_1$obj, 1, 1), par_1$a, par_1$b)
exp_1$aff <- features_dist(exp_1$cog, mean_1, pvar_1, output = "probx")
head(exp_1)
```

## Feature 2

For Feature 2, it is again assumed that no cognitive bias is present. However, a preference with a defined tendency (mean=0.4) and variance (pvar=0.6) is specified.

```{r}
facet <- "A" # Anxiety
feature <- 2
weight <- 0.5

exp_2 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
mean_2 <- 0.4
pvar_2 <- 0.6
par_2 <- calc_beta_par(mean_2, pvar_2, output = c("a", "b"))
par_2
```

```{r}
exp_2$cog <- qbeta(pbeta(exp_2$obj, 1, 1), par_1$a, par_1$b)
exp_2$aff <- features_dist(exp_2$cog, mean_2, pvar_2, output = "probx")
head(exp_2)
```

## Feature 3

For Feature 3, the situation is reversed: no preference is defined, but a cognitive bias is specified (mean=0.45, pvar=0.6).

```{r}
facet <- "D" # Depression
feature <- 1
weight <- 0.5

exp_3 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
mean_3 <- 0.45
pvar_3 <- 0.6
par_3 <- calc_beta_par(mean_3, pvar_3, output = c("a", "b"))
par_3
```

```{r}
exp_3$cog <- qbeta(pbeta(exp_3$obj, 1, 1) , par_3$a, par_3$b)
exp_3$aff <- features_dist(exp_3$cog, mean_1, pvar_1, output = "probx")
head(exp_3)
```

## Feature 4

For Feature 4, both a cognitive bias (mean=0.4, pvar=0.6) and a preference (mean=0.55, pvar=0.4) are specified.

```{r}
facet <- "D" # Depression
feature <- 2
weight <- 0.33

exp_4 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = seq(0, 1, length.out = nstep), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
mean_4 <- 0.4
pvar_4 <- 0.6
par_4 <- calc_beta_par(mean_4, pvar_4, output = c("a", "b"))
par_4

mean_5 <- 0.55
pvar_5 <- 0.4
par_5 <- calc_beta_par(mean_5, pvar_5, output = c("a", "b"))
par_5
```

```{r}
exp_4$cog <- qbeta(pbeta(exp_4$obj, 1, 1), par_4$a, par_4$b)
exp_4$aff <- features_dist(exp_4$cog, mean_5, pvar_5, output = "probx")
head(exp_4)
```

## Feature 5

Feature 5 is a copy of Feature 1, but with the modification that the objective experiences are no longer uniformly distributed.

```{r}
facet <- "E" # Emotional Volatility
feature <- 1
weight <- 0.25

exp_5 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = qbeta(seq(0, 1, length.out = nstep), 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
exp_5$cog <- qbeta(pbeta(exp_5$obj, 2, 2) , par_1$a, par_1$b)
exp_5$aff <- features_dist(exp_5$cog, mean_1, pvar_1, output = "probx")
head(exp_5)
```

## Feature 6

Feature 6 is a copy of Feature 4, but also with the modification that the objective experiences are no longer uniformly distributed.

```{r}
facet <- "E" # Emotional Volatility
feature <- 2
weight <- 0.25

exp_6 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = qbeta(seq(0, 1, length.out = nstep), 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
exp_6$cog <- qbeta(pbeta(exp_6$obj, 2, 2), par_4$a, par_4$b)
exp_6$aff <- features_dist(exp_6$cog, mean_5, pvar_5, output = "probx")
head(exp_6)
```

## Feature 7

Feature 7 is a copy of Feature 6, but with the addition that the objective experiences are randomly generated.

```{r}
facet <- "E" # Emotional Volatility
feature <- 3
weight <- 0.25

exp_7 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = rbeta(nstep, 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
exp_7$cog <- qbeta(pbeta(exp_7$obj, 2, 2), par_4$a, par_4$b)
exp_7$aff <- features_dist(exp_7$cog, mean_5, pvar_5, output = "probx")
head(exp_7)
```

## Feature 8

The final Feature 8 is a copy of Feature 7, with the modification that the cognitive distribution uses a *pvar* greater than 1 (specifically, 1.6).

```{r}
facet <- "E" # Emotional Volatility
feature <- 4
weight <- 0.25

exp_8 <- data.frame(
    step = 1:nstep,
    sit = 1:nstep, # Situation ID
    trait = rep(trait, nstep),
    facet = rep(facet, nstep),
    feature = factor(rep(feature, nstep)),
    weight = rep(weight, nstep),
    obj = rbeta(nstep, 2, 2), 
    cog = rep(NA, nstep),
    aff = rep(NA, nstep),
    sit_aff = rep(NA, nstep),
    sit_beh = rep(NA, nstep),
    sit_acc_pos = rep(NA, nstep),
    sit_acc_neg = rep(NA, nstep),
    sit_acc =rep(NA, nstep)
    )
```

```{r}
mean_6 <- 0.4
pvar_6 <- 1.6
par_6 <- calc_beta_par(mean_6, pvar_6, output = c("a", "b"))
par_6
```

```{r}
exp_8$cog <- qbeta(pbeta(exp_8$obj, 2, 2), par_6$a, par_6$b)
exp_8$aff <- features_dist(exp_8$cog, mean_5, pvar_5, output = "probx")
head(exp_8)
```

## Generating situational parameters

```{r}
exp <- bind_rows(exp_1, exp_2, exp_3, exp_4, exp_5, exp_6, exp_7, exp_8)

exp <- exp %>%
    group_by(step) %>%
        mutate(sit_aff = mean(aff),
               sit_beh = rbinom(1, 1, sit_aff),
               sit_acc_pos = sit_aff,
               sit_acc_neg = 1 - sit_aff,
               acc = 1 - 2 * (sit_acc_pos * sit_acc_neg)              
              ) %>%
    ungroup() %>%
    arrange(step)

head(exp, 12)
```

# Testing results

### Helper functions

```{r}
round_res <- function(res, size = 2) {
  res[] <- lapply(res, function(x) {
    if (is.numeric(x)) round(x, size) else x
  })
  res
}

scaled_mean_sd <- function(x, x_prob = NULL, w = 1, eps = 1e-6, na.rm = FALSE) {
    if (length(w) == 1) {
        w <- rep(w, length(x))
    }

    if (is.null(x_prob)) {
        x_prob <- x
    }
    
    if (length(x_prob) == 1) {
        x_prob <- rep(x_prob, length(x))
    }
    
    if (na.rm) {
        ok <- is.finite(x) & is.finite(x_prob) & is.finite(w)
        x <- x[ok]
        x_prob <- x_prob[ok]
        w <- w[ok]
    }

    x_mu <- mean(x_prob)
    x_mu <- pmin(pmax(x_mu, eps), 1 - eps)
    x_var <-  var(x_prob)
    prec <- (x_mu * (1 - x_mu)) / x_var - 1
    a <- prec * x_mu
    b <- prec * (1 - x_mu)
    prob <- dbeta(x_prob, a, b)
    prob <- pmax(prob, eps)

    w <- pmax(w, eps)

    W <- w / prob
    W_sum <- sum(W)

    mu <- sum(W * x) / W_sum
    sd <- sqrt(sum(W * (x - mu)^2) / W_sum)
    
    list(mean = mu, sd = sd)
}
```

## Aggregation of distribution parameters

```{r}
res <- exp %>%
    group_by(trait, facet, feature) %>%
        summarise(
            obj_mean = mean(obj, na.rm = TRUE),
            obj_sd   = sd(obj, na.rm = TRUE),
            cog_mean = mean(cog, na.rm = TRUE),
            cog_sd = sd(cog, na.rm = TRUE),
            aff_mean = scaled_mean_sd(x = cog, w = aff, na.rm = TRUE)$mean,
            aff_sd = scaled_mean_sd(x = cog, w = aff, na.rm = TRUE)$sd,
            aff_mean_1 = scaled_mean_sd(x = cog, w = aff * sit_beh, na.rm = TRUE)$mean,
            aff_sd_1 = scaled_mean_sd(x = cog, w = aff * sit_beh, na.rm = TRUE)$sd,
            aff_mean_0 = scaled_mean_sd(x = cog, w = aff * (1-sit_beh), na.rm = TRUE)$mean,
            aff_sd_0 = scaled_mean_sd(x = cog, w = aff * (1-sit_beh), na.rm = TRUE)$sd,
            beh_mean = scaled_mean_sd(x = cog, x_prob = 1, w = sit_beh, na.rm = TRUE)$mean,
            beh_sd   = scaled_mean_sd(x = cog, x_prob = 1, w = sit_beh, na.rm = TRUE)$sd,
            .groups = "drop"
        ) %>%
        mutate(
            obj_var_max = obj_mean * (1 - obj_mean) / (pmax(1 / obj_mean,1 / (1 - obj_mean)) + 1),
            obj_pvar = pmax(obj_sd^2 / obj_var_max, 0.01),
            cog_var_max = cog_mean * (1 - cog_mean) / (pmax(1 / cog_mean,1 / (1 - cog_mean)) + 1),
            cog_pvar = pmax(cog_sd^2 / cog_var_max, 0.01),
            aff_var_max = aff_mean * (1 - aff_mean) / (pmax(1 / aff_mean,1 / (1 - aff_mean)) + 1),
            aff_pvar = pmax(pmin(aff_sd^2 / aff_var_max, 1), 0.01), # limitet to max = 1
            aff_var_max_1 = aff_mean_1 * (1 - aff_mean_1) / (pmax(1 / aff_mean_1,1 / (1 - aff_mean_1)) + 1),
            aff_pvar_1 = pmax(pmin(aff_sd_1^2 / aff_var_max_1, 1), 0.01), # limitet to max = 1
            aff_var_max_0 = aff_mean_0 * (1 - aff_mean_0) / (pmax(1 / aff_mean_0,1 / (1 - aff_mean_0)) + 1),
            aff_pvar_0 = pmax(pmin(aff_sd_0^2 / aff_var_max_0, 1), 0.01), # limitet to max = 1
            beh_var_max = beh_mean * (1 - beh_mean) / (pmax(1 / beh_mean,1 / (1 - beh_mean)) + 1),
            beh_pvar = pmax(beh_sd^2 / beh_var_max, 0.01)
        )
```

## Results for objective features

```{r}
round_res(res[, c("trait", "facet", "feature", "obj_mean", "obj_pvar")], 2)
```

## Results for cognitive and affective interpreted features (Self-Concept)

```{r}
goal <- data.frame(
    cog_mean = c(mean_1, mean_1, mean_3, mean_4, mean_1, mean_4, mean_4, mean_6),
    cog_pvar = c(pvar_1, pvar_1, pvar_3, pvar_4, pvar_1, pvar_4, pvar_4, pvar_6),
    aff_mean = c(mean_1, mean_2, mean_1, mean_5, mean_1, mean_5, mean_5, mean_5),
    aff_pvar = c(pvar_1, pvar_2, pvar_1, pvar_5, pvar_1, pvar_5, pvar_5, pvar_5)
)
goal

round_res(res[, c("trait", "facet", "feature", "cog_mean", "cog_pvar", "aff_mean", "aff_pvar")], 3)
```

## Results for real Self-Concept and ideal Self-Concept

```{r}
round_res(res[, c("trait", "facet", "feature", "aff_mean_1", "aff_pvar_1", "aff_mean_0", "aff_pvar_0")], 3)
```

## Results for Behavior (Habituation)

```{r}
round_res(res[, c("trait", "facet", "feature", "beh_mean", "beh_pvar")], 2)
```

# \< [Back](../Personality.qmd#experiences)