library(ggplot2)
library(devtools)
library(responsesR)
library(truncnorm)
library(rlang)
library(effects)
library(effsize)

INTRODUCTION

Standardized Mean Difference

ggplot()+
  scale_x_continuous(limits=c(-3,4),breaks=seq(-4,4,.50))+
  ylab("Density")+xlab("X1 (z-score)")+
  geom_vline(xintercept=c(0,1.5),linetype=2,size=1)+
  stat_function(fun=dnorm,n=200,color="coral1",size=2,args = list(mean = 1.50))+
  stat_function(fun=dnorm,n=200,color="cornflowerblue",size=2)

DATA SIMULATION 1

cohen’s d is calculated by doing the difference between the means of two distributions, divided for the sd. y is just one distribution, so it does not make sense to calculate the d.

rm(list=ls())
set.seed(123)  
n <- 1000
x1 <- rnorm(n, mean = 0.5, sd = 1) 
x2 <- rnorm(n, mean = 0.0, sd = 1) 
x <- x1 - x2
y <- 0.8*x + rnorm(n,mean = 0 , sd = 1) # variable Y with 80% correlation 


# COMPARING DISTRIBUTIONS OF X AND Y
x_m <-  round(mean(x),3)
x_sd <- round(sd(x),3)
cohen.d(x1,x2)
## 
## Cohen's d
## 
## d estimate: 0.4733195 (small)
## 95 percent confidence interval:
##     lower     upper 
## 0.3843945 0.5622444
y_m <-  round(mean(y),3)
y_sd <- round(sd(y),3)
y_d <- round((y_m/y_sd),3)

## HISTOGRAM
dfx <- data.frame(
  man=x,
  frequency=frequency(x))
head(dfx)
##         man frequency
## 1 0.9353231         1
## 2 1.3097776         1
## 3 2.0766886         1
## 4 0.7026835         1
## 5 3.1786305         1
## 6 1.1744915         1
dfy <- data.frame(
  dv=y,
  frequency=frequency(y))
head(dfy)
##          dv frequency
## 1 0.2366547         1
## 2 1.2847599         1
## 3 1.1197617         1
## 4 1.7813745         1
## 5 2.7170403         1
## 6 0.3243249         1
library(patchwork)
#plot1
px<-ggplot(dfx, aes(x=x)) + 
  geom_histogram(color="black", fill="cornflowerblue") +
  ylab("Frequency") +
  xlab("Manipulation check")
#plot2
py<-ggplot(dfy, aes(x=dv)) + 
  geom_histogram(color="black", fill="cornflowerblue") +
  ylab("Frequency") +
  xlab("Dependent variable")
(px | py)

# SCATTER PLOT WITH REGRESSION LINE
data <- data.frame(X = x, Y = y)

ggplot(data, aes(x = X, y = Y)) +
  geom_point(color = "cornflowerblue") +  # scatter plot points
  geom_smooth(method = "lm", se = FALSE, linetype=1, linewidth=2, color="black") +  # regression line
  labs(title = " ",
       x = "Manipulation check",
       y = "Dependent variable")+
  coord_cartesian(xlim = c(0, 5), ylim = c(0, 5))  # only positive values

# SPAGHETTI PLOT
sogg <- rep(1:n, times = 2)
var <- rep(c("X","Y"), each = n)
data <- data.frame(N = sogg, X = x, Y = y, variable = var, value = c(x,y))

plot <- ggplot(data = data, aes(x = variable, y = value)) + 
  geom_point(shape = 21, size = 3, colour = "black", fill = "coral1", alpha = 0.3) + 
  geom_line(aes(group = N, color = "coral1", alpha = 0.00003)) + #spaghetti plot
  ylab("Effect size") +
  xlab("") +
  theme_bw(base_size = 18) +
  theme(legend.position="none") +
  scale_x_discrete(labels=c("X" = "Man. check", "Y" = "Dependent variable")) +
  theme(axis.title.x=element_blank(),
        strip.background =element_rect(fill="white"),
        panel.grid.major.x = element_blank())
plot

DATA SIMULATION 2

rm(list=ls())
set.seed(123)  
n <- 1000
x1 <- rnorm(n, mean = 0.5, sd = 1) 
x2 <- rnorm(n, mean = 0.0, sd = 1) 
x <- x1 - x2
y <- x^2 + rnorm(n,mean = 0 , sd = 1) 


# COMPARING DISTRIBUTIONS OF X AND Y
x_m <-  round(mean(x),3)
x_sd <- round(sd(x),3)
library(effsize)
cohen.d(x1,x2)
## 
## Cohen's d
## 
## d estimate: 0.4733195 (small)
## 95 percent confidence interval:
##     lower     upper 
## 0.3843945 0.5622444
y_m <-  round(mean(y),3)
y_sd <- round(sd(y),3)
y_d <- round((y_m/y_sd),3)

## HISTOGRAM
dfx <- data.frame(
  man=x,
  frequency=frequency(x))
head(dfx)
##         man frequency
## 1 0.9353231         1
## 2 1.3097776         1
## 3 2.0766886         1
## 4 0.7026835         1
## 5 3.1786305         1
## 6 1.1744915         1
dfy <- data.frame(
  dv=y,
  frequency=frequency(y))
head(dfy)
##           dv frequency
## 1  0.3632255         1
## 2  1.9524551         1
## 3  3.7710462         1
## 4  1.7129918         1
## 5 10.2778278         1
## 6  0.7641620         1
library(patchwork)
#plot1
px<-ggplot(dfx, aes(x=x)) + 
  geom_histogram(color="black", fill="cornflowerblue") +
  ylab("Frequency") +
  xlab("Manipulation check")
#plot2
py<-ggplot(dfy, aes(x=dv)) + 
  geom_histogram(color="black", fill="cornflowerblue") +
  ylab("Frequency") +
  xlab("Dependent variable")
(px | py)

# SCATTER PLOT WITH REGRESSION LINE
data <- data.frame(x, y)
d <- subset(data, y > 0)
fitP = glm(y~x,data=d,family="poisson")
effP = data.frame(allEffects(fitP,xlevels=list(x=seq(min(x),max(x),.05)))$"x")

ggplot(d, aes(x=x,y=y)) +
  geom_point(color = "cornflowerblue") +  # scatter plot points
  geom_line(data=effP,aes(y=fit),linetype=1, linewidth=2,color="black") +
  labs(title = " ",
       x = "Manipulation check",
       y = "Dependent variable")+
  coord_cartesian(xlim = c(0, 5), ylim = c(0, 5))  # only positive values

# SPAGHETTI PLOT
sogg <- rep(1:n, times = 2)
var <- rep(c("X","Y"), each = n)
data <- data.frame(N = sogg, X = x, Y = y, variable = var, value = c(x,y))

plot <- ggplot(data = data, aes(x = variable, y = value)) + 
  geom_point(shape = 21, size = 3, colour = "black", fill = "coral1", alpha = 0.3) + 
  geom_line(aes(group = N, color = "coral1", alpha = 0.00003)) + #spaghetti plot
  ylab("Effect size") +
  xlab("") +
  theme_bw(base_size = 18) +
  theme(legend.position="none") +
  scale_x_discrete(labels=c("X" = "Man. check", "Y" = "Dependent variable")) +
  theme(axis.title.x=element_blank(),
        strip.background =element_rect(fill="white"),
        panel.grid.major.x = element_blank())
plot