install.packages(c("gplots", "plotrix"))
install.packages("readr")
install.packages("rmarkdown")
source('~/Dropbox/courses/5210-2020/web-5210/psy5210/Projects/Chapter7/Chapter7Walkthrough.R')
setwd("~/Dropbox/courses/5210-2020/web-5210/psy5210/Projects/Chapter7")
install.packages(c("dplyr", "rmdformats"))
install.packages(c("BayesFactor", "BSDA", "GGally", "vioplot"))
par(mfrow=c(2,1))
hist(rnorm(1000,mean=0,sd=1),col="gold",xlim=c(-3,3),
main="Histogram of data")
numexps <- 100000
means <- rep(0,numexps)
sds <- rep(0,numexps)
expsize <- 15
for(i in 1:numexps)
{
#Generate one experiment:
data <- rnorm(expsize,mean=0,sd=1)
#estimate its mean
means[i] <- mean(data)
sds[i] <- sd(data)
}
#pdf("c8-montecarlo.pdf",width=9,height=5)
par(mfrow=c(1,2))
x <- hist(means,col="gold",breaks=-20:20/10,main="Histogram of obtained means\n under null hypothesis")
abline(v=0.5,lwd=3,col="cadetblue3")
plot(x$mids,cumsum(x$density)/10,type="o",ylab="Cumulative density",xlab="Bin midpoint",pch=16,cex=.5)
abline(v=0.5,lwd=3,col="cadetblue3")
#dev.off()
set.seed(1000)
x0 <- rnorm(30,0)
x1 <- rnorm(20,.2)
x1a <- rnorm(200,.2)
x2 <- rnorm(20,.5)
x3 <- rnorm(20,mean=.2,sd=.2)
x4 <- exp(rnorm(100))-1
t.test(x0,alternative="greater")
t.test(x1,alternative="greater")
t.test(x1,alternative="two.sided")
t.test(x1a,alternative="greater")
t.test(x2,alternative="greater")
t.test(x3,alternative="greater")
t.test(x4)
##############################################################
###non-parametric one-sample test: sign test/binomial test
binom.test(sum(x0>0),length(x0),p=.5)
binom.test(sum(x1>0),length(x1),p=.5)
SIGN.test(x0)
##BSDA library has a simpler to use version:
#install.packages("BSDA")
library(BSDA)
SIGN.test(x0)
binom.test(sum(x1>0),length(x1),p=.5)
SIGN.test(x1)
SIGN.test(x1a)
SIGN.test(x2)
SIGN.test(x3)
SIGN.test(x4)
###########################
### Bayesian one-sample
library(BayesFactor)
x <- ttestBF(x1)
print(x)
ttestBF(x0)
print(x)
ttestBF(x0)
## Extract samples to get posterior distributions
samples <- ttestBF(x1,iterations=10000,posterior=TRUE)
hist(samples[,1],breaks=50,main="Posterior distribution of mu",xlab="Sampled values",col="gold")
hist(samples[,2],breaks=50,main="Posterior distribution of sigma2",xlab="Sampled Values",col="gold")
##Error bars with 95% confidence on the sampled means:
quantile(samples[,2],c(.05,.95))
ttestBF(x2)
ttestBF(x0)
set.seed(1000)
x0 <- rnorm(30,0)
x1 <- rnorm(20,.2)
x1a <- rnorm(200,.2)
x2 <- rnorm(20,.5)
x3 <- rnorm(20,mean=.2,sd=.2)
x4 <- exp(rnorm(100))-1
library(vioplot)
#pdf("c8-vioplots.pdf",width=9,height=6)
vioplot(x0,x1,x1a,x2,x3,x4,col="gold",
names=c("x0","x1","x1a","x2","x3","x4"))
abline(0,0)
#dev.off()
qqnorm(x0)
mu <- mean(x1)
mu
?sd
length(x1)
mu <- mean(x1)
sd <- sd(x1)
se <- sd/sqrt(length(x1))
t <- mu/se
t
1-pt(t,19)
(1- pt(t,19))*2
mean(x2)
sd(x2)
sd(x2)/sqrt(length(x2))
mean(x)/(sd(x2)/sqrt(length(x2)))
mean(x2)/(sd(x2)/sqrt(length(x2)))
t <- mean(x2)/(sd(x2)/sqrt(length(x2)))
1-pt(t,19)
t.test(x0,alternative="greater")
t.test(x1,alternative="greater")
t.test(x2,alternative="greater")
t.test(x4)
binom.test(x4>0,length(x4),p=.5)
binom.test(sum(x4>0),length(x4),p=.5)
library(BSDA)
SIGN.test(x0)
SIGN.test(x4)
