Skip to content

Commit

Permalink
code updates and restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasgerstenberg committed Jun 9, 2024
1 parent e013bc9 commit 45a59bc
Show file tree
Hide file tree
Showing 33 changed files with 1,148 additions and 1,340 deletions.
113 changes: 47 additions & 66 deletions code/R/experiments/Experiment.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: "Explanation and Intervention Data"
author: "Tobias Gerstenberg & Lara Kirfel"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
bibliography: grateful-refs.bib
output:
bookdown::html_document2:
toc: true
Expand All @@ -13,10 +12,10 @@ output:
---

```{r, message=FALSE}
library("knitr")
library("modelr") # for bootstrapping
library("patchwork") # making figure panels
library("tidyverse") # for data wrangling, visualization, etc.
library("knitr") # for knitting
library("modelr") # for bootstrapping
library("patchwork") # making figure panels
library("tidyverse") # for data wrangling, visualization, etc.
```

# Set options
Expand All @@ -33,25 +32,24 @@ options(dplyr.summarise.inform = F)
```

# Condition 1: Hard Intervention: Positive Outcome

## Read in Data
```{r}
#### Read in Data
df.responses = read.csv(file = "explanation_selection_positive_outcome_study_2-responses.csv", stringsAsFactors = F, sep = ",") %>%
df.responses = read.csv(file = "../../../data/raw/explanation_selection_positive_outcome_study_2-responses.csv", stringsAsFactors = F, sep = ",") %>%
select(-error)
df.participants <- read.csv(file = "explanation_selection_positive_outcome_study_2-participants.csv", stringsAsFactors = F, sep = ",") %>%
df.participants = read.csv(file = "../../../data/raw/explanation_selection_positive_outcome_study_2-participants.csv", stringsAsFactors = F, sep = ",") %>%
select(-c(proliferate.condition, error))
df.data1 <- merge(df.responses, df.participants, by="workerid")
df.data1 = merge(df.responses, df.participants, by="workerid")
```

## Wrangle Data

```{r}
df.exp1 <- df.data1 %>%
df.exp1 = df.data1 %>%
gather("index", "response", -c(workerid, proliferate.condition, age, ethnicity, gender, feedback, race))%>%
mutate(CausalStructure =
case_when(
Expand Down Expand Up @@ -111,7 +109,7 @@ df.exp1 <- df.data1 %>%
## Prepate Dataset

```{r}
df.exp1_summary <- df.exp1 %>%
df.exp1_summary = df.exp1 %>%
group_by(CausalStructure) %>%
summarise(
abnormal_explanation_percentage = mean(abnormal_explanation == "abnormal") * 100,
Expand All @@ -125,7 +123,7 @@ df.exp1_summary <- df.exp1 %>%
mutate(Outcome = "positive") %>%
mutate(Experiment = "hardint")
write_csv(df.exp1_summary, "hardint_pos.csv")
write_csv(df.exp1_summary, "../../../data/aggregate/hardint_pos.csv")
```

Expand All @@ -134,23 +132,21 @@ write_csv(df.exp1_summary, "hardint_pos.csv")
## Read in Data

```{r}
#### Read in Data
df.responses = read.csv(file = "explanation_selection_negative_outcome-responses.csv", stringsAsFactors = F, sep = ",") %>%
df.responses = read.csv(file = "../../../data/raw/explanation_selection_negative_outcome-responses.csv", stringsAsFactors = F, sep = ",") %>%
select(-error)
df.participants <- read.csv(file = "explanation_selection_negative_outcome-participants.csv", stringsAsFactors = F, sep = ",") %>%
df.participants = read.csv(file = "../../../data/raw/explanation_selection_negative_outcome-participants.csv", stringsAsFactors = F, sep = ",") %>%
select(-c(proliferate.condition, error))
df.data2 <- merge(df.responses, df.participants, by="workerid")
df.data2 = merge(df.responses, df.participants, by="workerid")
```

## Wrangle Data

```{r}
df.exp2 <- df.data2 %>%
df.exp2 = df.data2 %>%
gather("index", "response", -c(workerid, proliferate.condition, age, ethnicity, gender, feedback, race))%>%
mutate(CausalStructure =
case_when(
Expand Down Expand Up @@ -207,7 +203,7 @@ df.exp2 <- df.data2 %>%
## Prepare Dataset

```{r}
df.exp2_summary <- df.exp2 %>%
df.exp2_summary = df.exp2 %>%
group_by(CausalStructure) %>%
summarise(
abnormal_explanation_percentage = mean(abnormal_explanation == "abnormal") * 100,
Expand All @@ -221,7 +217,7 @@ df.exp2_summary <- df.exp2 %>%
mutate(Outcome = "negative") %>%
mutate(Experiment = "hardint")
write_csv(df.exp2_summary, "hardint_neg.csv")
write_csv(df.exp2_summary, "../../../data/aggregate/hardint_neg.csv")
```

Expand All @@ -230,24 +226,21 @@ write_csv(df.exp2_summary, "hardint_neg.csv")
## Read in Data

```{r}
###Experiment 4
#### Read in Data
df.responses = read.csv(file = "pressbutton_positivecondition-responses.csv", stringsAsFactors = F, sep = ",") %>%
df.responses = read.csv(file = "../../../data/raw/pressbutton_positivecondition-responses.csv", stringsAsFactors = F, sep = ",") %>%
select(-error)
df.participants <- read.csv(file = "pressbutton_positivecondition-participants.csv", stringsAsFactors = F, sep = ",") %>%
df.participants = read.csv(file = "../../../data/raw/pressbutton_positivecondition-participants.csv", stringsAsFactors = F, sep = ",") %>%
select(-c(proliferate.condition, error))
df.data3 <- merge(df.responses, df.participants, by="workerid")
df.data3 = merge(df.responses, df.participants, by="workerid")
```

## Wrangle Data

```{r}
df.exp3 <- df.data3 %>%
df.exp3 = df.data3 %>%
gather("index", "response", -c(workerid, proliferate.condition, age, ethnicity, gender, feedback, race))%>%
mutate(CausalStructure =
case_when(
Expand Down Expand Up @@ -305,7 +298,7 @@ df.exp3 <- df.data3 %>%
## Prepare Dataset

```{r}
df.exp3_summary <- df.exp3 %>%
df.exp3_summary = df.exp3 %>%
group_by(CausalStructure) %>%
summarise(
abnormal_explanation_percentage = mean(abnormal_explanation == "abnormal") * 100,
Expand All @@ -319,33 +312,30 @@ df.exp3_summary <- df.exp3 %>%
mutate(Outcome = "positive") %>%
mutate(Experiment = "softint")
write_csv(df.exp3_summary, "softint_pos.csv")
write_csv(df.exp3_summary, "../../../data/aggregate/softint_pos.csv")
```

# Condition 4: Soft Intervention: Negative Outcome
## Read in Data

```{r}
###Experiment 4
#### Read in Data
df.responses = read.csv(file = "pressbutton_negativecondition-responses.csv", stringsAsFactors = F, sep = ",") %>%
df.responses = read.csv(file = "../../../data/raw/pressbutton_negativecondition-responses.csv", stringsAsFactors = F, sep = ",") %>%
select(-error)
df.participants <- read.csv(file = "pressbutton_negativecondition-participants.csv", stringsAsFactors = F, sep = ",") %>%
df.participants = read.csv(file = "../../../data/raw/pressbutton_negativecondition-participants.csv", stringsAsFactors = F, sep = ",") %>%
select(-c(proliferate.condition, error))
df.data4 <- merge(df.responses, df.participants, by="workerid") %>%
df.data4 = merge(df.responses, df.participants, by="workerid") %>%
filter(!row_number() %in% c(71, 72))
```

## Wrangle Data

```{r}
df.exp4 <- df.data4 %>%
df.exp4 = df.data4 %>%
gather("index", "response", -c(workerid, proliferate.condition, age, ethnicity, gender, feedback, race))%>%
mutate(CausalStructure =
case_when(
Expand Down Expand Up @@ -403,7 +393,7 @@ df.exp4 <- df.data4 %>%
## Prepare Dataset

```{r}
df.exp4_summary <- df.exp4 %>%
df.exp4_summary = df.exp4 %>%
group_by(CausalStructure) %>%
summarise(
abnormal_explanation_percentage = mean(abnormal_explanation == "abnormal") * 100,
Expand All @@ -417,32 +407,30 @@ df.exp4_summary <- df.exp4 %>%
mutate(Outcome = "negative") %>%
mutate(Experiment = "softint")
write_csv(df.exp4_summary, "softint_neg.csv")
write_csv(df.exp4_summary, "../../../data/aggregate/softint_neg.csv")
```

# Condition 5: Fixed Intervention: Positive Outcome

## Read in Data

```{r}
###Experiment 5
#### Read in Data
df.responses = read.csv(file = "fixedintervention_positive-responses.csv", stringsAsFactors = F, sep = ",") %>%
df.responses = read.csv(file = "../../../data/raw/fixedintervention_positive-responses.csv", stringsAsFactors = F, sep = ",") %>%
select(-error)
df.participants <- read.csv(file = "fixedintervention_positive-participants.csv", stringsAsFactors = F, sep = ",") %>%
df.participants = read.csv(file = "../../../data/raw/fixedintervention_positive-participants.csv", stringsAsFactors = F, sep = ",") %>%
select(-c(proliferate.condition, error))
df.data5 <- merge(df.responses, df.participants, by="workerid")
df.data5 = merge(df.responses, df.participants, by="workerid")
```

## Wrangle Data

```{r}
df.exp5 <- df.data5 %>%
df.exp5 = df.data5 %>%
gather("index", "response", -c(workerid, proliferate.condition, age, ethnicity, gender, feedback, race))%>%
mutate(CausalStructure =
case_when(
Expand Down Expand Up @@ -499,7 +487,7 @@ df.exp5 <- df.data5 %>%
## Prepare Dataset

```{r}
df.exp5_summary <- df.exp5 %>%
df.exp5_summary = df.exp5 %>%
group_by(CausalStructure) %>%
summarise(
abnormal_explanation_percentage = mean(abnormal_explanation == "abnormal") * 100,
Expand All @@ -513,34 +501,30 @@ df.exp5_summary <- df.exp5 %>%
mutate(Outcome = "negative") %>%
mutate(Experiment = "fixedint")
write_csv(df.exp5_summary, "fixedint_pos.csv")
write_csv(df.exp5_summary, "../../../data/aggregate/fixedint_pos.csv")
```

# Condition 6: Fixed Intervention: Negative Outcome


## Read in Data

```{r}
###Experiment 6
#### Read in Data
df.responses = read.csv(file = "fixedintervention_negative-responses.csv", stringsAsFactors = F, sep = ",") %>%
df.responses = read.csv(file = "../../../data/raw/fixedintervention_negative-responses.csv", stringsAsFactors = F, sep = ",") %>%
select(-error)
df.participants <- read.csv(file = "fixedintervention_negative-participants.csv", stringsAsFactors = F, sep = ",") %>%
df.participants = read.csv(file = "../../../data/raw/fixedintervention_negative-participants.csv", stringsAsFactors = F, sep = ",") %>%
select(-c(proliferate.condition, error))
df.data6 <- merge(df.responses, df.participants, by="workerid")
df.data6 = merge(df.responses, df.participants, by="workerid")
```

## Wrangle Data

```{r}
df.exp6 <- df.data6 %>%
df.exp6 = df.data6 %>%
gather("index", "response", -c(workerid, proliferate.condition, age, ethnicity, gender, feedback, race))%>%
mutate(CausalStructure =
case_when(
Expand Down Expand Up @@ -598,7 +582,7 @@ df.exp6 <- df.data6 %>%
## Prepare Dataset

```{r}
df.exp6_summary <- df.exp6 %>%
df.exp6_summary = df.exp6 %>%
group_by(CausalStructure) %>%
summarise(
abnormal_explanation_percentage = mean(abnormal_explanation == "abnormal") * 100,
Expand All @@ -612,15 +596,14 @@ df.exp6_summary <- df.exp6 %>%
mutate(Outcome = "negative") %>%
mutate(Experiment = "fixedint")
write_csv(df.exp6_summary, "fixedint_neg.csv")
write_csv(df.exp6_summary, "../../../data/aggregate/fixedint_neg.csv")
```

# Create Dataset including raw data from all conditions

```{r}
df.exp_all <- rbind(df.exp1, df.exp2, df.exp3, df.exp4, df.exp5, df.exp6)
df.exp_all = rbind(df.exp1, df.exp2, df.exp3, df.exp4, df.exp5, df.exp6)
```

# Bootstrapping
Expand Down Expand Up @@ -672,7 +655,6 @@ df.percentage = df.boot %>%
```{r}
set.seed(1)
# percentages with bootstrapped confidence intervals
df.confidence = df.boot %>%
group_by(structure, outcome, action) %>%
nest() %>%
Expand Down Expand Up @@ -720,8 +702,8 @@ df.combined = df.confidence %>%
### Read in model predictions

```{r, message=FALSE, warning=FALSE}
df.prediction_intervention = read_csv("intervention_predictions.csv")
df.prediction_explanation = read_csv("explanation_predictions.csv") %>%
df.prediction_intervention = read_csv("../../../data/model/intervention_predictions.csv")
df.prediction_explanation = read_csv("../../../data/model/explanation_predictions.csv") %>%
select(-truth)
df.optimal_intervention_model = df.prediction_intervention %>%
Expand All @@ -737,8 +719,8 @@ df.optimal_intervention_model = df.prediction_intervention %>%
pivot_wider(names_from = index,
values_from = prediction)
df.intervention_only_model = read_csv("explanation_predictions_intervention_only.csv")
df.truth_only_model = read_csv("explanation_predictions_truth_only.csv")
df.intervention_only_model = read_csv("../../../data/model/explanation_predictions_intervention_only.csv")
df.truth_only_model = read_csv("../../../data/model/explanation_predictions_truth_only.csv")
df.alternative_models = df.intervention_only_model %>%
mutate(model = "intervention_only") %>%
Expand Down Expand Up @@ -1075,7 +1057,6 @@ df.combined %>%
~ round(., 2)))
```


# Session info

```{r, echo=F}
Expand Down
2,071 changes: 1,017 additions & 1,054 deletions code/R/experiments/Experiment.html

Large diffs are not rendered by default.

Loading

0 comments on commit 45a59bc

Please sign in to comment.