Master Thesis:
  • Proposal
  • Concepts
  • Sketchbooks

On this page

  • Experiences Example
    • General definitions
    • Feature 1
    • Feature 2
    • Feature 3
    • Feature 4
    • Generating situational parameters
  • Testing results
    • Helper functions
    • Results for objective features
    • Results for cognitive interpreted features
    • Results for affective interpreted features
  • < Back

Experiences

Author

Hubert Bächli

Experiences Example

General definitions

nstep <- 1e4
trait <- "N" # Neuroticism

set.seed(1)

Feature 1

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 = runif(nstep) # random objektiv expresion
    )

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

mean_1 <- 0.55
pvar_1 <- 0.6
par_1 <- calc_beta_par(mean_1, pvar_1, output = c("a", "b"))
par_1
         a   b
1 2.811111 2.3

Cognitiv and Affective interpretation

exp_1$cog <- qbeta(exp_1$obj, par_1$a, par_1$b)
exp_1$aff <- features_dist(exp_1$cog, mean_1, pvar_1, output = "prop")
head(exp_1)
  step sit trait facet feature weight       obj       cog       aff
1    1   1     N     A       1    0.5 0.2655087 0.4123507 0.8342196
2    2   2     N     A       1    0.5 0.3721239 0.4813090 0.9385064
3    3   3     N     A       1    0.5 0.5728534 0.5989731 0.9981754
4    4   4     N     A       1    0.5 0.9082078 0.8231843 0.6122860
5    5   5     N     A       1    0.5 0.2016819 0.3657890 0.7414594
6    6   6     N     A       1    0.5 0.8983897 0.8141608 0.6403038

Feature 2

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 = runif(nstep) # random objektiv expresion
    )

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

mean_2 <- 0.51
pvar_2 <- 0.4
par_2 <- calc_beta_par(mean_2, pvar_2, output = c("a", "b"))
par_2
         a     b
1 3.471122 3.335

Cognitiv and Affective interpretation

exp_2$cog <- qbeta(exp_2$obj, par_2$a, par_2$b)
exp_2$aff <- features_dist(exp_2$cog, mean_2, pvar_2, output = "prop")
head(exp_2)
  step sit trait facet feature weight        obj       cog       aff
1    1   1     N     A       2    0.5 0.06471249 0.2329002 0.4104669
2    2   2     N     A       2    0.5 0.67661240 0.6011035 0.9283567
3    3   3     N     A       2    0.5 0.73537169 0.6336600 0.8669266
4    4   4     N     A       2    0.5 0.11129967 0.2807157 0.5602888
5    5   5     N     A       2    0.5 0.04665462 0.2088123 0.3368733
6    6   6     N     A       2    0.5 0.13091031 0.2973838 0.6117012

Feature 3

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 = runif(nstep) # random objektiv expresion
    )

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

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 2.3 2.811111

Cognitiv and Affective interpretation

exp_3$cog <- qbeta(exp_3$obj, par_3$a, par_3$b)
exp_3$aff <- features_dist(exp_3$cog, mean_3, pvar_3, output = "prop")
head(exp_3)
  step sit trait facet feature weight       obj       cog       aff
1    1   1     N     D       1    0.5 0.2106027 0.2691024 0.8522034
2    2   2     N     D       1    0.5 0.1147864 0.1973725 0.6747741
3    3   3     N     D       1    0.5 0.1453641 0.2221730 0.7435275
4    4   4     N     D       1    0.5 0.3099322 0.3321864 0.9515880
5    5   5     N     D       1    0.5 0.1502421 0.2259251 0.7532668
6    6   6     N     D       1    0.5 0.5266817 0.4584776 0.9896570

Feature 4

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

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 = runif(nstep) # random objektiv expresion
    )

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

mean_4 <- 0.5
pvar_4 <- 0.8
par_4 <- calc_beta_par(mean_4, pvar_4, output = c("a", "b"))
par_4
      a     b
1 1.375 1.375
mean_5 <- 0.4
pvar_5 <- 0.6
par_5 <- calc_beta_par(mean_5, pvar_5, output = c("a", "b"))
par_5
    a   b
1 2.6 3.9

Cognitiv and Affective interpretation

exp_4$cog <- qbeta(exp_4$obj, par_4$a, par_4$b)
exp_4$aff <- features_dist(exp_4$cog, mean_5, pvar_5, output = "prop")
head(exp_4)
  step sit trait facet feature weight       obj       cog        aff
1    1   1     N     D       2    0.5 0.4153090 0.4298513 0.94969669
2    2   2     N     D       2    0.5 0.1409714 0.1866145 0.70029737
3    3   3     N     D       2    0.5 0.4575043 0.4648664 0.89572688
4    4   4     N     D       2    0.5 0.8030130 0.7596068 0.19298628
5    5   5     N     D       2    0.5 0.3211128 0.3504956 0.99974754
6    6   6     N     D       2    0.5 0.8681337 0.8225222 0.09092099

Generating situational parameters

exp <- bind_rows(exp_1, exp_2, exp_3, exp_4)

exp <- exp %>%
    group_by(sit) %>%
        mutate(sit_aff = mean(aff),
               acc = 1 - 2 * (sit_aff * (1 - sit_aff)),
               beh = sample(c(0, 1), 1)               
              ) %>%
    ungroup() %>%
    arrange(sit)

tail(exp, 12)
# A tibble: 12 × 12
    step   sit trait facet feature weight    obj   cog   aff sit_aff   acc   beh
   <int> <int> <chr> <chr> <fct>    <dbl>  <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>
 1  9998  9998 N     A     1          0.5 0.503  0.559 0.997   0.796 0.675     1
 2  9998  9998 N     A     2          0.5 0.478  0.500 0.998   0.796 0.675     1
 3  9998  9998 N     D     1          0.5 0.875  0.701 0.587   0.796 0.675     1
 4  9998  9998 N     D     2          0.5 0.610  0.591 0.603   0.796 0.675     1
 5  9999  9999 N     A     1          0.5 0.568  0.596 0.999   0.737 0.612     1
 6  9999  9999 N     A     2          0.5 0.602  0.562 0.978   0.737 0.612     1
 7  9999  9999 N     D     1          0.5 0.0732 0.158 0.553   0.737 0.612     1
 8  9999  9999 N     D     2          0.5 0.693  0.662 0.417   0.737 0.612     1
 9 10000 10000 N     A     1          0.5 0.653  0.645 0.974   0.875 0.781     0
10 10000 10000 N     A     2          0.5 0.111  0.281 0.560   0.875 0.781     0
11 10000 10000 N     D     1          0.5 0.341  0.351 0.971   0.875 0.781     0
12 10000 10000 N     D     2          0.5 0.298  0.331 0.994   0.875 0.781     0

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, y, w = 1, eps = 1e-3, na.rm = FALSE) {
    if (length(w) == 1) {
        w <- rep(w, length(x))
    }
    if (na.rm) {
        ok <- is.finite(x) & is.finite(y) & is.finite(w)
        x <- x[ok]
        y <- y[ok]
        w <- w[ok]
      }

    y <- (y + eps)/eps
    w <- (w + eps)/eps
    W <- w * y
    W_sum <- sum(W)

    mu <- sum(W * x) / W_sum
    sd <- sqrt(sum(W * (x - mu)^2) / W_sum)
    
    list(mean = mu, sd = sd)
}
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(cog, aff, na.rm = TRUE)$mean,
            aff_sd = scaled_mean_sd(cog, aff, na.rm = TRUE)$sd,
            sit_aff_mean = mean(sit_aff, na.rm = TRUE),
            sit_aff_sd   = sd(sit_aff, na.rm = TRUE),
            .groups = "drop"
        ) %>%
        mutate(
            obj_var_max = pmin(obj_mean,(1 - obj_mean))^2 / 3,
            obj_pvar = pmax(pmin(obj_sd^2 / obj_var_max, 1),0.01),
            cog_var_max = pmin(cog_mean,(1 - cog_mean))^2 / 3,
            cog_pvar = pmax(pmin(cog_sd^2 / cog_var_max, 1),0.01),
            aff_var_max = pmin(aff_mean,(1 - aff_mean))^2 / 3,
            aff_pvar = pmax(pmin(aff_sd^2 / aff_var_max, 1),0.01)
        )

Results for objective features

round_res(res[, c("trait", "facet", "feature", grep("^obj", names(res), value = TRUE))], 2)
# A tibble: 4 × 7
  trait facet feature obj_mean obj_sd obj_var_max obj_pvar
  <chr> <chr> <fct>      <dbl>  <dbl>       <dbl>    <dbl>
1 N     A     1            0.5   0.29        0.08        1
2 N     A     2            0.5   0.29        0.08        1
3 N     D     1            0.5   0.29        0.08        1
4 N     D     2            0.5   0.29        0.08        1

Results for cognitive interpreted features

goal_cog <- data.frame(
  mean = c(mean_1, mean_2, mean_3, mean_4),
  pvar = c(pvar_1, pvar_2, pvar_3, pvar_4)
)
goal_cog
  mean pvar
1 0.55  0.6
2 0.51  0.4
3 0.45  0.6
4 0.50  0.8
round_res(res[, c("trait", "facet", "feature", grep("^cog", names(res), value = TRUE))], 2)
# A tibble: 4 × 7
  trait facet feature cog_mean cog_sd cog_var_max cog_pvar
  <chr> <chr> <fct>      <dbl>  <dbl>       <dbl>    <dbl>
1 N     A     1           0.55   0.2         0.07     0.61
2 N     A     2           0.51   0.18        0.08     0.4 
3 N     D     1           0.45   0.2         0.07     0.6 
4 N     D     2           0.5    0.26        0.08     0.8 

Results for affective interpreted features

goal_cog <- data.frame(
  mean = c(mean_1, mean_2, mean_3, mean_5),
  pvar = c(pvar_1, pvar_2, pvar_3, pvar_5)
    
)
goal_cog
  mean pvar
1 0.55  0.6
2 0.51  0.4
3 0.45  0.6
4 0.40  0.6
round_res(res[, c("trait", "facet", "feature", grep("^aff", names(res), value = TRUE))], 2)
# A tibble: 4 × 7
  trait facet feature aff_mean aff_sd aff_var_max aff_pvar
  <chr> <chr> <fct>      <dbl>  <dbl>       <dbl>    <dbl>
1 N     A     1           0.56   0.16        0.06     0.43
2 N     A     2           0.51   0.14        0.08     0.25
3 N     D     1           0.44   0.16        0.06     0.42
4 N     D     2           0.41   0.17        0.06     0.52
round_res(res[, c("trait", "facet", "feature", grep("^sit", names(res), value = TRUE))], 2)
# A tibble: 4 × 5
  trait facet feature sit_aff_mean sit_aff_sd
  <chr> <chr> <fct>          <dbl>      <dbl>
1 N     A     1               0.71       0.13
2 N     A     2               0.71       0.13
3 N     D     1               0.71       0.13
4 N     D     2               0.71       0.13
?rbeta
starting httpd help server ... done

< Back

qbeta(rbeta(10,1.5,1.5),1.5,1.5)
 [1] 0.62048106 0.63636017 0.65316292 0.51488664 0.69276409 0.68909558
 [7] 0.56001085 0.46088166 0.05929116 0.57782390
x <- seq(0,1,0.01)
y <- rbeta(length(x),1.5,1.5)
y <- qbeta(rbeta(length(x),1.5,1.5),1.5,1.5)
plot(x,y)

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

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

calc_beta_par <- function(tendencies, pvar, output = NULL, eps = 1e-3) {
    len <- length(tendencies * pvar)
    df <- data.frame(ten = rep(0, len))
    
    df$ten <- pmin(pmax(tendencies, eps), 1 - eps)
    df$pvar <- pmax(pmin(pvar, 1), eps)
    df$sd_max <- pmin(df$ten, 1 - df$ten)
    df$var_max <- df$sd_max ** 2 / 3
    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-3) {
    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$max <- ifelse(is.na(df$mode), df$freq, dbeta(df$mode, df$a, df$b))
    df$prop <- df$freq / df$max

    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 <- 1e4
trait <- "N" # Neuroticism

set.seed(1)
```

## Feature 1

```{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 = runif(nstep) # random objektiv expresion
    )
```

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

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

#### Cognitiv and Affective interpretation

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

## Feature 2

```{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 = runif(nstep) # random objektiv expresion
    )
```

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

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

#### Cognitiv and Affective interpretation

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

## Feature 3

```{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 = runif(nstep) # random objektiv expresion
    )
```

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

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

#### Cognitiv and Affective interpretation

```{r}
exp_3$cog <- qbeta(exp_3$obj, par_3$a, par_3$b)
exp_3$aff <- features_dist(exp_3$cog, mean_3, pvar_3, output = "prop")
head(exp_3)
```

## Feature 4

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

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 = runif(nstep) # random objektiv expresion
    )
```

Distributions parameter to be generated for cognition and affect. In later stages, these distributions are intended to differ; however, for the present example, they are treated as identical.

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

```{r}
mean_5 <- 0.4
pvar_5 <- 0.6
par_5 <- calc_beta_par(mean_5, pvar_5, output = c("a", "b"))
par_5
```

#### Cognitiv and Affective interpretation

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

## Generating situational parameters

```{r}
exp <- bind_rows(exp_1, exp_2, exp_3, exp_4)

exp <- exp %>%
    group_by(sit) %>%
        mutate(sit_aff = mean(aff),
               acc = 1 - 2 * (sit_aff * (1 - sit_aff)),
               beh = sample(c(0, 1), 1)               
              ) %>%
    ungroup() %>%
    arrange(sit)

tail(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, y, w = 1, eps = 1e-3, na.rm = FALSE) {
    if (length(w) == 1) {
        w <- rep(w, length(x))
    }
    if (na.rm) {
        ok <- is.finite(x) & is.finite(y) & is.finite(w)
        x <- x[ok]
        y <- y[ok]
        w <- w[ok]
      }

    y <- (y + eps)/eps
    w <- (w + eps)/eps
    W <- w * y
    W_sum <- sum(W)

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

```{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(cog, aff, na.rm = TRUE)$mean,
            aff_sd = scaled_mean_sd(cog, aff, na.rm = TRUE)$sd,
            sit_aff_mean = mean(sit_aff, na.rm = TRUE),
            sit_aff_sd   = sd(sit_aff, na.rm = TRUE),
            .groups = "drop"
        ) %>%
        mutate(
            obj_var_max = pmin(obj_mean,(1 - obj_mean))^2 / 3,
            obj_pvar = pmax(pmin(obj_sd^2 / obj_var_max, 1),0.01),
            cog_var_max = pmin(cog_mean,(1 - cog_mean))^2 / 3,
            cog_pvar = pmax(pmin(cog_sd^2 / cog_var_max, 1),0.01),
            aff_var_max = pmin(aff_mean,(1 - aff_mean))^2 / 3,
            aff_pvar = pmax(pmin(aff_sd^2 / aff_var_max, 1),0.01)
        )
```

## Results for objective features

```{r}
round_res(res[, c("trait", "facet", "feature", grep("^obj", names(res), value = TRUE))], 2)
```

## Results for cognitive interpreted features

```{r}
goal_cog <- data.frame(
  mean = c(mean_1, mean_2, mean_3, mean_4),
  pvar = c(pvar_1, pvar_2, pvar_3, pvar_4)
)
goal_cog
round_res(res[, c("trait", "facet", "feature", grep("^cog", names(res), value = TRUE))], 2)
```

## Results for affective interpreted features

```{r}
goal_cog <- data.frame(
  mean = c(mean_1, mean_2, mean_3, mean_5),
  pvar = c(pvar_1, pvar_2, pvar_3, pvar_5)
    
)
goal_cog
round_res(res[, c("trait", "facet", "feature", grep("^aff", names(res), value = TRUE))], 2)
```

```{r}
round_res(res[, c("trait", "facet", "feature", grep("^sit", names(res), value = TRUE))], 2)
?rbeta
```

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

```{r}
qbeta(rbeta(10,1.5,1.5),1.5,1.5)
x <- seq(0,1,0.01)
y <- rbeta(length(x),1.5,1.5)
y <- qbeta(rbeta(length(x),1.5,1.5),1.5,1.5)
plot(x,y)
```