forked from vasishth/ESSLLI2015Vasishth_Week1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture1.R
More file actions
274 lines (230 loc) · 7.69 KB
/
lecture1.R
File metadata and controls
274 lines (230 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
## ----setup,include=FALSE,cache=FALSE-------------------------------------
library(knitr)
library(coda)
# set global chunk options, put figures into folder
options(replace.assign=TRUE,show.signif.stars=FALSE)
opts_chunk$set(fig.path='figures/figure-', fig.align='center', fig.show='hold')
options(replace.assign=TRUE,width=75)
opts_chunk$set(dev='postscript')
#library(rstan)
#set.seed(9991)
# save workspace image, if you want
#the.date <- format(Sys.time(), "%b%d%Y")
#save.image(file=paste0("homework01-",the.date,".RData")
## ----forloops,cache=TRUE-------------------------------------------------
## number of iterations:
nsim<-10
## vector for storing results:
results<-rep(NA,10)
for(i in 1:nsim){
results[i]<-1+2*i
}
results
## ----calculatecdfbinomial,include=TRUE,echo=TRUE,cache=TRUE--------------
## sample size
n<-10
## prob of success
p<-0.5
probs<-rep(NA,11)
for(x in 0:10){
## Cumulative Distribution Function:
probs[x+1]<-round(pbinom(x,size=n,prob=p),digits=2)
}
## ----echo=FALSE----------------------------------------------------------
xtabl.probs<-data.frame(x=0:10,prob=probs)
## ----cdfbinomial,include=TRUE,echo=TRUE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'----
## Plot the CDF:
plot(1:11,probs,xaxt="n",xlab="x",
ylab=expression(P(X<=x)),main="CDF")
axis(1,at=1:11,labels=0:10)
## ------------------------------------------------------------------------
## using cdf:
pbinom(1,size=10,prob=0.5)-pbinom(0,size=10,prob=0.5)
## using pmf:
choose(10,1) * 0.5 * (1-0.5)^9
## ----binomcomputePequalszero---------------------------------------------
## P(X=1)
choose(10,1) * 0.5 * (1-0.5)^9
## using the built-in function:
dbinom(1,size=10,prob=0.5)
## ----pdfbinomial,include=TRUE,echo=TRUE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'----
## Plot the pmf:
plot(1:11,dbinom(0:10,size=10,prob=0.5),main="PMF",
xaxt="n",ylab="P(X=x)",xlab="x")
axis(1,at=1:11,labels=0:10)
## ----pdfnormal,include=TRUE,echo=TRUE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'----
plot(function(x) dnorm(x), -3, 3,
main = "Normal density",ylim=c(0,.4),
ylab="density",xlab="X")
## ----shadenormal,echo=FALSE----------------------------------------------
## function for plotting CDF:
plot.prob<-function(x,
x.min,
x.max,
prob,
mean,
sd,
gray.level,main){
plot(x,dnorm(x,mean,sd),
type = "l",xlab="",
ylab="",main=main)
abline(h = 0)
## shade X<x
x1 = seq(x.min, qnorm(prob), abs(prob)/5)
y1 = dnorm(x1, mean, sd)
polygon(c(x1, rev(x1)),
c(rep(0, length(x1)), rev(y1)),
col = gray.level)
}
shadenormal<-
function (prob=0.5,
gray1=gray(0.3),
x.min=-6,
x.max=abs(x.min),
x = seq(x.min, x.max, 0.01),
mean=0,
sd=1,main="P(X<0)")
{
plot.prob(x=x,x.min=x.min,x.max=x.max,
prob=prob,
mean=mean,sd=sd,
gray.level=gray1,main=main)
}
## ----plotshadenormal,include=TRUE,echo=FALSE,cache=TRUE,fig.width=4,fig.height=3,out.width='0.75\\textwidth'----
shadenormal(prob=0.975,main="P(X<1.96)")
## ----computingprobsnormal------------------------------------------------
## The area under curve between +infty and -infty:
pnorm(Inf)-pnorm(-Inf)
## The area under curve between 2 and -2:
pnorm(2)-pnorm(-2)
## The area under curve between 1 and -1:
pnorm(1)-pnorm(-1)
## ------------------------------------------------------------------------
qnorm(0.975,mean=500,sd=100)
## ----pnormtwo------------------------------------------------------------
pnorm(2)
## ----negativexpnorm------------------------------------------------------
1-pnorm(2)
## alternatively:
pnorm(2,lower.tail=F)
## ------------------------------------------------------------------------
(x<-rbinom(5,size=10,prob=0.5))
## ----likfun0,echo=TRUE,fig.width=6---------------------------------------
## probability parameter fixed at 0.5
theta<-0.5
prod(dbinom(x,size=10,prob=theta))
## probability parameter fixed at 0.1
theta<-0.1
prod(dbinom(x,size=10,prob=theta))
## ----likfun,echo=TRUE----------------------------------------------------
theta<-seq(0,1,by=0.01)
store<-rep(NA,length(theta))
for(i in 1:length(theta)){
store[i]<-prod(dbinom(x,size=10,prob=theta[i]))
}
## ----likfunplot,echo=FALSE,fig.height=4----------------------------------
plot(1:length(store),store,xaxt="n",xlab="theta",
ylab="f(x1,...,xn|theta")
axis(1,at=1:length(theta),labels=theta)
## ------------------------------------------------------------------------
x<-rnorm(100,mean=500,sd=50)
mean(x)
x<-rnorm(100,mean=500,sd=50)
mean(x)
## ------------------------------------------------------------------------
nsim<-1000
n<-100
mu<-500
sigma<-100
samp_distrn_means<-rep(NA,nsim)
samp_distrn_sd<-rep(NA,nsim)
for(i in 1:nsim){
x<-rnorm(n,mean=mu,sd=sigma)
samp_distrn_means[i]<-mean(x)
samp_distrn_sd[i]<-sd(x)
}
## ----samplingdistrnmeans,echo=FALSE,fig.height=4-------------------------
hist(samp_distrn_means,main="Samp. distrn. means",
freq=F,xlab=expression(hat(mu)),ylab="density")
## ------------------------------------------------------------------------
for(i in 1:nsim){
x<-rexp(n)
samp_distrn_means[i]<-mean(x)
samp_distrn_sd[i]<-sd(x)
}
## ----samplingdistrnmeansexp,echo=FALSE,fig.height=4----------------------
hist(samp_distrn_means,main="Samp. distrn. means",
freq=F,xlab=expression(hat(mu)),ylab="density")
## ------------------------------------------------------------------------
## estimate from simulation:
sd(samp_distrn_means)
## ------------------------------------------------------------------------
x<-rnorm(100,mean=500,sd=100)
hat_sigma<-sd(x)
hat_sigma/sqrt(n)
## ----confint1------------------------------------------------------------
## lower bound:
mu-(2*hat_sigma/sqrt(n))
## upper bound:
mu+(2*hat_sigma/sqrt(n))
## ----confint2------------------------------------------------------------
lower<-rep(NA,nsim)
upper<-rep(NA,nsim)
for(i in 1:nsim){
x<-rnorm(n,mean=mu,sd=sigma)
lower[i]<-mean(x) - 2 * sd(x)/sqrt(n)
upper[i]<-mean(x) + 2 * sd(x)/sqrt(n)
}
## ----confint3------------------------------------------------------------
## check how many CIs contain mu:
CIs<-ifelse(lower<mu & upper>mu,1,0)
table(CIs)
## approx. 95% of the CIs contain true mean:
table(CIs)[2]/sum(table(CIs))
## ----repeatedCIsplot,echo=FALSE,fig.height=4-----------------------------
se <- function(x)
{
y <- x[!is.na(x)] # remove the missing values, if any
sqrt(var(as.vector(y))/length(y))
}
ci <- function (scores){
m <- mean(scores,na.rm=TRUE)
stderr <- se(scores)
len <- length(scores)
upper <- m + qt(.975, df=len-1) * stderr
lower <- m + qt(.025, df=len-1) * stderr
return(data.frame(lower=lower,upper=upper))
}
lower <- rep(NA,100)
upper <- rep(NA,100)
for(i in 1:100){
sample <- rnorm(100,mean=60,sd=4)
lower[i] <- ci(sample)$lower
upper[i] <- ci(sample)$upper
}
cis <- cbind(lower,upper)
store <- rep(NA,100)
pop.mean<-60
pop.sd<-4
for(i in 1:100){
sample <- rnorm(100,mean=pop.mean,sd=pop.sd)
lower[i] <- ci(sample)$lower
upper[i] <- ci(sample)$upper
if(lower[i]<pop.mean & upper[i]>pop.mean){
store[i] <- TRUE} else {
store[i] <- FALSE}
}
## need this for the plot below:
cis <- cbind(lower,upper)
main.title<-"95% CIs in 100 repeated samples"
line.width<-ifelse(store==FALSE,2,1)
cis<-cbind(cis,line.width)
x<-0:100
y<-seq(55,65,by=1/10)
plot(x,y,type="n",xlab="i-th repeated sample",
ylab="Scores",main=main.title)
abline(60,0,lwd=2)
x0<-x
x1<-x
arrows(x0,y0=cis[,1],
x1,y1=cis[,2],length=0,lwd=cis[,3])