M=1000
n=c(10,30,100)
normal_est=function(n){
data=rnorm(n,0,1)
output=c(mean(data),median(data),mean(max(data),min(data)),IQR(data))
return(output)
}
unif_est=function(n){
data=runif(n,0,1)
output=c(mean(data),median(data),mean(max(data),min(data)),IQR(data))
return(output)
}
exp_est=function(n){
data=rexp(n,1)
output=c(mean(data),median(data),mean(max(data),min(data)),IQR(data))
return(output)
}
norm_stat1=data.frame()
unif_stat1=data.frame()
exp_stat1=data.frame()
norm_stat2=data.frame()
unif_stat2=data.frame()
exp_stat2=data.frame()
norm_stat3=data.frame()
unif_stat3=data.frame()
exp_stat3=data.frame()
for(i in 1:M){
norm_stat1=rbind(norm_stat1,normal_est(n[1]))
unif_stat1=rbind(unif_stat1,unif_est(n[1]))
exp_stat1=rbind(exp_stat1,exp_est(n[1]))
norm_stat2=rbind(norm_stat2,normal_est(n[2]))
unif_stat2=rbind(unif_stat2,unif_est(n[2]))
exp_stat2=rbind(exp_stat2,exp_est(n[2]))
norm_stat3=rbind(norm_stat3,normal_est(n[3]))
unif_stat3=rbind(unif_stat3,unif_est(n[3]))
exp_stat3=rbind(exp_stat3,exp_est(n[3]))
}
colnames(norm_stat1)=c("mean","median","midrange","midquartile")
colnames(norm_stat2)=c("mean","median","midrange","midquartile")
colnames(norm_stat3)=c("mean","median","midrange","midquartile")
colnames(unif_stat1)=c("mean","median","midrange","midquartile")
colnames(unif_stat2)=c("mean","median","midrange","midquartile")
colnames(unif_stat3)=c("mean","median","midrange","midquartile")
colnames(exp_stat1)=c("mean","median","midrange","midquartile")
colnames(exp_stat2)=c("mean","median","midrange","midquartile")
colnames(exp_stat3)=c("mean","median","midrange","midquartile")
#normal
##10
plot(density(norm_stat1$mean),xlim=c(-1.5,4),ylim=c(0,1.2))
lines(density(norm_stat1$median),col='red')
lines(density(norm_stat1$midrange),col='blue')
lines(density(norm_stat1$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(norm_stat1$mean,main="mean")
boxplot(norm_stat1$median,main="median")
boxplot(norm_stat1$midrange,main="midrange")
boxplot(norm_stat1$midquartile,main="midquartile")
pairs(norm_stat1)
##30
plot(density(norm_stat2$mean),xlim=c(-1.5,4),ylim=c(0,2))
lines(density(norm_stat2$median),col='red')
lines(density(norm_stat2$midrange),col='blue')
lines(density(norm_stat2$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(norm_stat2$mean,main="mean")
boxplot(norm_stat2$median,main="median")
boxplot(norm_stat2$midrange,main="midrange")
boxplot(norm_stat2$midquartile,main="midquartile")
pairs(norm_stat2)
##100
plot(density(norm_stat3$mean),xlim=c(-1.5,4),ylim=c(0,4))
lines(density(norm_stat3$median),col='red')
lines(density(norm_stat3$midrange),col='blue')
lines(density(norm_stat3$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(norm_stat3$mean,main="mean")
boxplot(norm_stat3$median,main="median")
boxplot(norm_stat3$midrange,main="midrange")
boxplot(norm_stat3$midquartile,main="midquartile")
pairs(norm_stat3)
#unif
##10
plot(density(unif_stat1$mean),xlim=c(-0.1,1.5),ylim=c(0,7))
lines(density(unif_stat1$median),col='red')
lines(density(unif_stat1$midrange),col='blue')
lines(density(unif_stat1$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(unif_stat1$mean,main="mean")
boxplot(unif_stat1$median,main="median")
boxplot(unif_stat1$midrange,main="midrange")
boxplot(unif_stat1$midquartile,main="midquartile")
pairs(unif_stat1)
##30
plot(density(unif_stat2$mean),xlim=c(-0.1,1.5),ylim=c(0,21))
lines(density(unif_stat2$median),col='red')
lines(density(unif_stat2$midrange),col='blue')
lines(density(unif_stat2$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(unif_stat2$mean,main="mean")
boxplot(unif_stat2$median,main="median")
boxplot(unif_stat2$midrange,main="midrange")
boxplot(unif_stat2$midquartile,main="midquartile")
pairs(unif_stat2)
##100
plot(density(unif_stat3$mean),xlim=c(-0.1,1.5),ylim=c(0,40))
lines(density(unif_stat3$median),col='red')
lines(density(unif_stat3$midrange),col='blue')
lines(density(unif_stat3$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(unif_stat3$mean,main="mean")
boxplot(unif_stat3$median,main="median")
boxplot(unif_stat3$midrange,main="midrange")
boxplot(unif_stat3$midquartile,main="midquartile")
pairs(unif_stat3)
#exp
##10
plot(density(exp_stat1$mean),xlim=c(-0.1,5),ylim=c(0,2))
lines(density(exp_stat1$median),col='red')
lines(density(exp_stat1$midrange),col='blue')
lines(density(exp_stat1$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(exp_stat1$mean,main="mean")
boxplot(exp_stat1$median,main="median")
boxplot(exp_stat1$midrange,main="midrange")
boxplot(exp_stat1$midquartile,main="midquartile")
pairs(exp_stat1)
##30
plot(density(exp_stat2$mean),xlim=c(-0.1,5),ylim=c(0,2))
lines(density(exp_stat2$median),col='red')
lines(density(exp_stat2$midrange),col='blue')
lines(density(exp_stat2$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(exp_stat2$mean,main="mean")
boxplot(exp_stat2$median,main="median")
boxplot(exp_stat2$midrange,main="midrange")
boxplot(exp_stat2$midquartile,main="midquartile")
pairs(exp_stat2)
##100
plot(density(exp_stat3$mean),xlim=c(-0.1,10),ylim=c(0,4))
lines(density(exp_stat3$median),col='red')
lines(density(exp_stat3$midrange),col='blue')
lines(density(exp_stat3$midquartile),col='green')
par(mfrow=c(1,4))
boxplot(exp_stat3$mean,main="mean")
boxplot(exp_stat3$median,main="median")
boxplot(exp_stat3$midrange,main="midrange")
boxplot(exp_stat3$midquartile,main="midquartile")
pairs(exp_stat3)
mean(norm_stat1$mean)
mean(norm_stat1$median)
mean(norm_stat1$midrange)
mean(norm_stat1$midquartile)
sd(norm_stat1$mean)
sd(norm_stat1$median)
sd(norm_stat1$midrange)
sd(norm_stat1$midquartile)
mean(norm_stat2$mean)
mean(norm_stat2$median)
mean(norm_stat2$midrange)
mean(norm_stat2$midquartile)
sd(norm_stat2$mean)
sd(norm_stat2$median)
sd(norm_stat2$midrange)
sd(norm_stat2$midquartile)
mean(norm_stat3$mean)
mean(norm_stat3$median)
mean(norm_stat3$midrange)
mean(norm_stat3$midquartile)
sd(norm_stat3$mean)
sd(norm_stat3$median)
sd(norm_stat3$midrange)
sd(norm_stat3$midquartile)
mean(unif_stat1$mean)
mean(unif_stat1$median)
mean(unif_stat1$midrange)
mean(unif_stat1$midquartile)
sd(unif_stat1$mean)
sd(unif_stat1$median)
sd(unif_stat1$midrange)
sd(unif_stat1$midquartile)
mean(unif_stat2$mean)
mean(unif_stat2$median)
mean(unif_stat2$midrange)
mean(unif_stat2$midquartile)
sd(unif_stat2$mean)
sd(unif_stat2$median)
sd(unif_stat2$midrange)
sd(unif_stat2$midquartile)
mean(unif_stat3$mean)
mean(unif_stat3$median)
mean(unif_stat3$midrange)
mean(unif_stat3$midquartile)
sd(unif_stat3$mean)
sd(unif_stat3$median)
sd(unif_stat3$midrange)
sd(unif_stat3$midquartile)
mean(exp_stat1$mean)
mean(exp_stat1$median)
mean(exp_stat1$midrange)
mean(exp_stat1$midquartile)
sd(exp_stat1$mean)
sd(exp_stat1$median)
sd(exp_stat1$midrange)
sd(exp_stat1$midquartile)
mean(exp_stat2$mean)
mean(exp_stat2$median)
mean(exp_stat2$midrange)
mean(exp_stat2$midquartile)
sd(exp_stat2$mean)
sd(exp_stat2$median)
sd(exp_stat2$midrange)
sd(exp_stat2$midquartile)
mean(exp_stat3$mean)
mean(exp_stat3$median)
mean(exp_stat3$midrange)
mean(exp_stat3$midquartile)
sd(exp_stat3$mean)
sd(exp_stat3$median)
sd(exp_stat3$midrange)
sd(exp_stat3$midquartile)
##question 3
library(MASS)
library(boot)
data=Cars93
cv=function(x,n){sd(x[n])/mean(x[n])}
B=boot(data$EngineSize,cv,1000)
boot.ci(B)
#t -ci
upper2=mean(B$t)+sd(B$t)*qt(0.95,999)
lower2=mean(B$t)+sd(B$t)*qt(0.05,999)
#permutation test
library(coin)
independence_test(EngineSize~Origin,data=data)