Greetings,
I’ve tried to incorporate everything I learned in this thread into the attached file. Eventually I am able to do the t-tests I want. However, I am fairly confident a better way exists, I will just have to keep looking and go through trial/error.
I am completely open to suggestions on what's wrong and where to look for improvements.
~ Jason the #rstatsnewbie
---
title: "forum_help"
author: "Jason"
date: "12/22/2020"
output:
html_document:
toc: yes
number_sections: yes
editor_options:
chunk_output_type: console
---
#```{r setup, include=FALSE}
#knitr::opts_chunk$set(echo = TRUE)
#```
#```
# LIBRARIES
#```{r Libraries, eval=TRUE, echo=TRUE, results=FALSE, collapse=TRUE, message=FALSE}
suppressPackageStartupMessages({
library(dplyr)
library(tidyverse)
library(reshape)
library(reprex)
library(here)
here()
dr_here(show_reason = FALSE)
})
#```
# CONTINGENCY TABLE
#```{r ContingencyTable, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
zdat <-
structure(list(
ID = structure(1:10,
.Label = c(
"IP004", "IP005", "IP007", "IP008", "IP009",
"IP010", "IP012", "IP013", "IP015", "IP016"
),
class = "factor"
),
group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L),
.Label = c("gp1", "gp2"),
class = "factor"
),
v1.cond = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L),
.Label = c("pos", "neg"), class = "factor"
),
v2.cond = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L),
.Label = c("pos", "neg"),
class = "factor"
),
v1pre = c(7, 1, 0, 0, 1, 0, 0, 4, 0, 6),
v1post = c(9, 5, 5, 5.5, 5, 2, 7, 9, 5, 6),
v2pre = c(1, 5, 1, 3, 5, 0, 3, 0, 5, 0),
v2post = c(5, 6, 4, 5.5, 5, 4, 4, 3, 7, 3)
),
row.names = c(NA, -10L),
class = "data.frame"
)
# Convert to long format
# convention disfavors using . as a name delimiter
zdat_melt <- melt(zdat, id.vars = c("ID", "group", "v1.cond", "v2.cond"))
colnames(zdat_melt)
# alternative renaming
colnames(zdat_melt) <- c("ID","group","v1_cond","v2_cond","event","score")
#```
# POSITIVE PRE
## Gp1 Pos Pre
#```{r Gp1PosPre, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
# VISIT-1
# positive and pre only
gp1v1pos_pre <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp1") %>%
filter(v1_cond == "pos") %>%
filter(event == "v1pre")
gp1v1pos_pre$event <- str_replace_all(gp1v1pos_pre$event, c("v1pre" = "pre"))
# Change name to facilitate the full join later
names(gp1v1pos_pre)[names(gp1v1pos_pre) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp1v1pos_pre <- as_tibble(gp1v1pos_pre)
################ VISIT-2 ################
# positive and pre only
gp1v2pos_pre <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp1") %>%
filter(v2_cond == "pos") %>%
filter(event == "v2pre")
gp1v2pos_pre$event <- str_replace_all(gp1v2pos_pre$event,
c("v2pre" = "pre"))
# Change name to facilitate the full join later
names(gp1v2pos_pre)[names(gp1v2pos_pre) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp1v2pos_pre <- as_tibble(gp1v2pos_pre)
############## JOIN GROUPS ############
# Join the two tables representing Gp1 positive pre scores
gp1_pos_pre <- full_join(gp1v1pos_pre, gp1v2pos_pre)
# CLEAN UP: REMOVE OLD DATA
# rm(gp1v1pos_pre, gp1v2pos_pre)
#```
## Gp2 Pos Pre
#```{r Gp2PosPre, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
#### GROUP-2 POSITIVE PRE ####
# VISIT-1
# positive and pre only
gp2v1pos_pre <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp2") %>%
filter(v1_cond == "pos") %>%
filter(event == "v1pre")
gp2v1pos_pre$event <- str_replace_all(gp2v1pos_pre$event,
c("v1pre" = "pre"))
# Change name to facilitate the full join later
names(gp2v1pos_pre)[names(gp2v1pos_pre) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp2v1pos_pre <- as_tibble(gp2v1pos_pre)
################ VISIT-2 ################
# positive and pre only
gp2v2pos_pre <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp2") %>%
filter(v2_cond == "pos") %>%
filter(event == "v2pre")
gp2v2pos_pre$event <- str_replace_all(gp2v2pos_pre$event,
c("v2pre" = "pre"))
# Change name to facilitate the full join later
names(gp2v2pos_pre)[names(gp2v2pos_pre) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp2v2pos_pre <- as_tibble(gp2v2pos_pre)
############## JOIN GROUPS ############
# Join the two tables representing Gp2 positive pre scores
gp2_pos_pre <- full_join(gp2v1pos_pre, gp2v2pos_pre)
# CLEAN UP: REMOVE OLD DATA
# rm(gp2v1pos_pre, gp2v2pos_pre)
#```
# POSITIVE POST
## Gp1 Pos Post
#```{r Gp1PosPost, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
######## VISIT-1 ################
# positive and post only
gp1v1pos_post <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp1") %>%
filter(v1_cond == "pos") %>%
filter(event == "v1post")
gp1v1pos_post$event <- str_replace_all(gp1v1pos_post$event,
c("v1post" = "post"))
# Change name to facilitate the full join later
names(gp1v1pos_post)[names(gp1v1pos_post) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp1v1pos_post <- as_tibble(gp1v1pos_post)
######## VISIT-2 ################
# positive and post only
gp1v2pos_post <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp1") %>%
filter(v2_cond == "pos") %>%
filter(event == "v2post")
gp1v2pos_post$event <- str_replace_all(gp1v2pos_post$event,
c("v2post" = "post"))
# Change name to facilitate the full join later
names(gp1v2pos_post)[names(gp1v2pos_post) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp1v2pos_post <- as_tibble(gp1v2pos_post)
############## JOIN GROUPS ############
# Join the two tables representing Gp1 positive post scores
gp1_pos_post <- full_join(gp1v1pos_post, gp1v2pos_post)
# CLEAN UP: REMOVE OLD DATA
#rm(gp1v1pos_post, gp1v2pos_post)
#```
# Gp2 Pos Post
#```{r gp2PosPost, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
######## VISIT-1 ################
# positive and post only
gp2v1pos_post <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp2") %>%
filter(v1_cond == "pos") %>%
filter(event == "v1post")
gp2v1pos_post$event <- str_replace_all(gp2v1pos_post$event,
c("v1post" = "post"))
# Change name to facilitate the full join later
names(gp2v1pos_post)[names(gp2v1pos_post) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp2v1pos_post <- as_tibble(gp2v1pos_post)
################ VISIT-2 ################
# positive and post only
gp2v2pos_post <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp2") %>%
filter(v2_cond == "pos") %>%
filter(event == "v2post")
gp2v2pos_post$event <- str_replace_all(gp2v2pos_post$event,
c("v2post" = "post"))
# Change name to facilitate the full join later
names(gp2v2pos_post)[names(gp2v2pos_post) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp2v2pos_post <- as_tibble(gp2v2pos_post)
############## JOIN GROUPS ############
# Join the two tables representing Gp2 positive pre scores
gp2_pos_post <- full_join(gp2v1pos_post, gp2v2pos_post)
# CLEAN UP: REMOVE OLD DATA
# rm(gp2v1pos_post, gp2v2pos_post)
#```
# NEGATIVE PRE
## Gp1 Neg Pre
#```{r Gp1negPre, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
######## VISIT-1 ################
# negative and pre only
gp1v1neg_pre <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp1") %>%
filter(v1_cond == "neg") %>%
filter(event == "v1pre")
gp1v1neg_pre$event <- str_replace_all(gp1v1neg_pre$event,
c("v1pre" = "pre"))
# Change name to facilitate the full join later
names(gp1v1neg_pre)[names(gp1v1neg_pre) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp1v1neg_pre <- as_tibble(gp1v1neg_pre)
################ VISIT-2 ################
# negative and pre only
gp1v2neg_pre <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp1") %>%
filter(v2_cond == "neg") %>%
filter(event == "v2pre")
gp1v2neg_pre$event <- str_replace_all(gp1v2neg_pre$event,
c("v2pre" = "pre"))
# Change name to facilitate the full join later
names(gp1v2neg_pre)[names(gp1v2neg_pre) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp1v2neg_pre <- as_tibble(gp1v2neg_pre)
############## JOIN GROUPS ############
# Join the two tables representing Gp1 negative pre scores #
gp1_neg_pre <- full_join(gp1v1neg_pre, gp1v2neg_pre)
# CLEAN UP: REMOVE OLD DATA
# rm(gp1v1neg_pre, gp1v2neg_pre)
#```
## Gp2 Neg Pre
#```{r gp2negPre, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
######## VISIT-1 ################
# NEGATIVE and pre only
gp2v1neg_pre <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp2") %>%
filter(v1_cond == "neg") %>%
filter(event == "v1pre")
gp2v1neg_pre$event <- str_replace_all(gp2v1neg_pre$event,
c("v1pre" = "pre"))
# Change name to facilitate the full join later
names(gp2v1neg_pre)[names(gp2v1neg_pre) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp2v1neg_pre <- as_tibble(gp2v1neg_pre)
################ VISIT-2 ################
# negative and pre only
gp2v2neg_pre <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp2") %>%
filter(v2_cond == "neg") %>%
filter(event == "v2pre")
gp2v2neg_pre$event <- str_replace_all(gp2v2neg_pre$event,
c("v2pre" = "pre"))
# Change name to facilitate the full join later
names(gp2v2neg_pre)[names(gp2v2neg_pre) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp2v2neg_pre <- as_tibble(gp2v2neg_pre)
############## JOIN GROUPS ############
# Join the two tables representing Gp2 negative pre scores
gp2_neg_pre <- full_join(gp2v1neg_pre, gp2v2neg_pre)
# CLEAN UP: REMOVE OLD DATA
# rm(gp2v1neg_pre, gp2v2neg_pre)
#```
# NEGATIVE POST
## Gp1 Neg post
#```{r Gp1negpost, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
######## VISIT-1 ################
# negative and post only
gp1v1neg_post <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp1") %>%
filter(v1_cond == "neg") %>%
filter(event == "v1post")
gp1v1neg_post$event <- str_replace_all(gp1v1neg_post$event,
c("v1post" = "post"))
# Change name to facilitate the full join later
names(gp1v1neg_post)[names(gp1v1neg_post) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp1v1neg_post <- as_tibble(gp1v1neg_post)
################ VISIT-2 ################
# negative and post only
gp1v2neg_post <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp1") %>%
filter(v2_cond == "neg") %>%
filter(event == "v2post")
gp1v2neg_post$event <- str_replace_all(gp1v2neg_post$event,
c("v2post" = "post"))
# Change name to facilitate the full join later
names(gp1v2neg_post)[names(gp1v2neg_post) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp1v2neg_post <- as_tibble(gp1v2neg_post)
############## JOIN GROUPS ############
# Join the two tables representing Gp1 negative post scores
gp1_neg_post <- full_join(gp1v1neg_post, gp1v2neg_post)
# CLEAN UP: REMOVE OLD DATA
# rm(gp1v1neg_post, gp1v2neg_post)
#```
## Gp2 Neg post
#```{r gp2negpost, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
######## VISIT-1 ################
# negative and post only
gp2v1neg_post <- zdat_melt %>% dplyr::select( - v2_cond) %>%
filter(group == "gp2") %>%
filter(v1_cond == "neg") %>%
filter(event == "v1post")
gp2v1neg_post$event <- str_replace_all(gp2v1neg_post$event,
c("v1post" = "post"))
# Change name to facilitate the full join later
names(gp2v1neg_post)[names(gp2v1neg_post) == 'v1_cond'] <- 'cond'
# Tibble needed for joining later
gp2v1neg_post <- as_tibble(gp2v1neg_post)
################ VISIT-2 ################
# negative and post only
gp2v2neg_post <- zdat_melt %>% dplyr::select( - v1_cond) %>%
filter(group == "gp2") %>%
filter(v2_cond == "neg") %>%
filter(event == "v2post")
gp2v2neg_post$event <- str_replace_all(gp2v2neg_post$event,
c("v2post" = "post"))
# Change name to facilitate the full join later
names(gp2v2neg_post)[names(gp2v2neg_post) == 'v2_cond'] <- 'cond'
# Tibble needed for joining later
gp2v2neg_post <- as_tibble(gp2v2neg_post)
############## JOIN GROUPS ############
# Join the two tables representing Gp2 negative post scores
gp2_neg_post <- full_join(gp2v1neg_post, gp2v2neg_post)
# CLEAN UP: REMOVE OLD DATA
# rm(gp2v1neg_post, gp2v2neg_post)
#```
# JOINING
## By GROUPS
#```{r Gp1FullJoin, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
zgp1_neg <- full_join(gp1_neg_post, gp1_neg_pre)
zgp1_pos <- full_join(gp1_pos_post, gp1_pos_pre)
# zgp1_full <- full_join(zgp1_neg, zgp1_pos)
zgp2_neg <- full_join(gp2_neg_post, gp2_neg_pre)
zgp2_pos <- full_join(gp2_pos_post, gp2_pos_pre)
# zgp2_full <- full_join(zgp2_neg, zgp2_pos)
# zz_full <- full_join(zgp1_full, zgp2_full)
#
# rm(zgp1_full, zgp2_full, zgp1_neg, zgp1_pos)
# rm(zgp2_full, zgp2_full, zgp2_neg, zgp2_pos)
#```
## By POSITIVE
#```{r PosPre, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
#### Pos Pre ####
zzz_pos_pre <- full_join(gp1_pos_pre, gp2_pos_pre)
zzz_pos_pre <- rename(zzz_pos_pre, c(score="pospre"))
#### Pos Post ####
zzz_pos_post <- full_join(gp1_pos_post, gp2_pos_post)
zzz_pos_post <- rename(zzz_pos_post, c(score="pospost"))
colnames(zzz_pos_pre)
colnames(zzz_pos_post)
#### POSITIVE FULL ####
## CREATING A DATA FRAME POSITIVE PRE & POST BY GROUP
z <- zzz_pos_post %>% dplyr::select("ID", "pospost")
zzzz_pos_full <- full_join(zzz_pos_pre, z)
zzzz_pos_full <- zzzz_pos_full %>% dplyr::select( -c(cond, event))
colnames(zzzz_pos_full)
#```
## By NEGATIVE
#```{r NegPre, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
### NEG Pre ####
zzz_neg_pre <- full_join(gp1_neg_pre, gp2_neg_pre)
zzz_neg_pre <- rename(zzz_neg_pre, c(score="negpre"))
#### NEG POST ####
zzz_neg_post <- full_join(gp1_neg_post, gp2_neg_post)
zzz_neg_post <- rename(zzz_neg_post, c(score="negpost"))
#### NEG FULL ####
## CREATING A DATA FRAME NEGATIVE PRE & POST BY GROUP
z2 <- zzz_neg_post %>% dplyr::select("ID", "negpost")
zzzz_neg_full <- full_join(zzz_neg_pre, z2)
zzzz_neg_full <- zzzz_neg_full %>% dplyr::select( -c(cond, event))
#```
## FULL
#```{r FullMood, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
z_FULL <- full_join(zzzz_pos_full, zzzz_neg_full)
#```
# T-TESTS
#```{r ttests, eval=TRUE, echo=TRUE, message=FALSE, comment = ""}
#### T-TESTS: GROUP BY THE 4 CONDITIONS
attach(z_FULL)
t.test(pospre ~ group, mu=0, alt="two.sided", conf=0.95, var.eq=F, paired=F)
t.test(pospost ~ group, mu=0, alt="two.sided", conf=0.95, var.eq=F, paired=F)
t.test(negpre ~ group, mu=0, alt="two.sided", conf=0.95, var.eq=F, paired=F)
t.test(negpost ~ group, mu=0, alt="two.sided", conf=0.95, var.eq=F, paired=F)
#------END---------