---
title: "Pilot Stimuli"
output: html_document
date: "2023-06-05"
---

```{r setup, include=FALSE}

library(tidyverse)
library(ggdist)
library(xts)
library(distributional)
library(moments)
library(ggfx)
library(ggbeeswarm)

# Set random seed
set.seed(2)

# Variable for setting the x-axis range on either side of the mean
x_range <- 4

```

# Generating Distributions

```{r distribution generator}

## set values

n <- 13 # number of stimuli to generate
target <- 32 # target value of interest
range <- 2 # range for distance from target value
min_mean <- target - range # lower end of possible mean values
max_mean <- target + range # higher end of possible mean values
sd <- 1 # set value for standard deviation of the distributions - set based on the uncertain about uncertainty. sd = 1 led to slight increase in crossover, sd = 2 led to slight decrease in crossover. sd = 1.5 to balance that? or should just pick one - probably sd = 1
dist_n <- 100 # number of values in the distribution
tail_pct <- 0 # pct likelihood of tail value


## create data frame

df <- data.frame(
  trial = 0:(n-1),
  mean = NA,
  sd = sd,
  freeze.pct = NA,
  min = NA,
  max = NA, 
  fifty.1 = NA,
  fifty.2 = NA,
  distribution = NA,
  skew = NA
)

allDists <- data.frame(
  1:100
)

## generate distributions

for (i in 1:n) {
  
  mean <- sample(seq(min_mean, max_mean), 1) # randomly select the mean of the distribution
  df$mean[i] <- mean # add to the data frame
  data <- rnorm(dist_n, mean, sd) # create distribution
  df$distribution[i] <- list(data) # add to the data frame
  
  allDists <- cbind(allDists, data)  # save data into df for use in prototyping
  colnames(allDists)[ncol(allDists)] <- paste0("t", i-1)
  
  prob_target <- sum(data <= target) / length(data)
  df$freeze.pct[i] <- prob_target # add to the data frame
  
  df$min[i] <- round(min(data), 0)
  
  df$max[i] <- round(max(data), 0)
  
  df$fifty.1[i] <-  round(quantile(data, 0.25), 0) # first value in the 50% confidence interval
  df$fifty.2[i] <-  round(quantile(data, 0.75), 0) # second value in the 50% confidence interval
  
  df$skew[i] <- skewness(data) # calculate and store the skew value for the distribution
  
  
}

allDists <- allDists %>% select(contains("t"))

write_excel_csv(allDists, "distributions.csv")

```

# Text Template

```{r}

df_text <- data.frame(
  trial = 0:(n-1),
  forecast = NA,
)


# Define intervals and labels for skew
skew_likelihood_intervals <- c(0, 0.1, 0.3, 0.5, 1)
skew_likelihood_label <- 
  c(
    "slightly ",
    "somewhat ",
    "",
    "significantly "
  )

skew_direction_intervals <- c(-1, 0, 1)
skew_direction_label <- 
  c(
    "warmer than cooler",
    "cooler than warmer"
  )

for (i in 1:n) {
  
  data <- unlist(df$distribution[i])
  
  skew_likelihood <- cut(abs(df$skew[i]), 
                         breaks = skew_likelihood_intervals, 
                         labels = skew_likelihood_label, 
                         right = TRUE)
  skew_direction <- cut(df$skew[i], 
                        breaks = skew_direction_intervals, 
                        labels = skew_direction_label, 
                        right = TRUE)
  
  
  # text template
  df_text$forecast[i] <- paste0(
    "The most likely temperature low tonight might be around ",
    df$mean[i],
    "ºF. There is a 50% chance that the temperature could fall between ",
    round(quantile(data, 0.25), 0),
    " and ",
    round(quantile(data, 0.75), 0),
    "ºF. While the range of possible lows could potentially span ",
    df$min[i],
    " to ",
    df$max[i],
    "ºF, those extremes are less likely. It also appears ",
    skew_likelihood,
    "more likely to be ",
    skew_direction,
    " within that range."
  )
  

```






