library(ggplot2)

Exercise 1

seq(0, 19, 2)
##  [1]  0  2  4  6  8 10 12 14 16 18
seq(101, 203, 2)
##  [1] 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133
## [18] 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167
## [35] 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 201
## [52] 203
rep(c(1,3),4)
## [1] 1 3 1 3 1 3 1 3
c(rep(1,4), rep(3,4))
## [1] 1 1 1 1 3 3 3 3

Exercise 2

mean(0:99)
## [1] 49.5
var(0:99)
## [1] 841.6667
x = rnorm(50, 30, 5)
mean(x)
## [1] 30.06677
var(x)
## [1] 25.3428

Exercise 3

die = 1:6
B = 10000
means = rep(0, B)
for (i in 1:B) {
  x = sample(die, 10, replace=TRUE)
  means[i] = mean(x)
}
a = data.frame(means)
ggplot(a, aes(x=means)) + geom_density()

Exercise 4

B = 10000
pvals = rep(0, B)
for (i in 1:B) {
  x = rnorm(25, 0.5, 1)
  pvals[i] = t.test(x)$p.value  
}
mean(pvals < 0.05)
## [1] 0.6662

Exercise 5

plot(dnorm(seq(20,80,1), 50, sqrt(10)), lwd=1, type='l', xlab="x")

Exercise 6

signtest = function(x) {
  numplus = sum(x > 0)
  numminus = sum(x < 0)
  return(1 - pbinom(numplus-1, numplus + numminus, 0.5))
}

Exercise 7

school<-c(82,69,73,43,58,56,76,65)
home<-c(63,42,74,37,51,43,80,62)
response <- school - home
signtest(response)
## [1] 0.1445312