forked from lcomm/Summer-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethodsClass5.R
224 lines (170 loc) · 5.72 KB
/
MethodsClass5.R
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
#
# Leah Comment
#
# August 21, 2014
#
# Methods summer prep class session 5
#
#
# setwd("C:/Users/leahcomment/Desktop/vc_practice")
# getwd()
#now I've made a change!
#Libraries
library(lattice)
library(RColorBrewer)
#General settings
blues = brewer.pal(n=9, name="PuBu")
med.blue = blues[5]
# Modeling gamma-generated data as exponential ----------------------------
#yet another change
#Requested function to generate samples from the gamma distribution
PlotGamma <- function(n, alpha, beta, R) {
#Generate the samples
my.samples = matrix(data=rgamma(n=n*R, shape=alpha, rate=beta)
,nrow=n
,ncol=R)
#Calculate sample stats
sample.rate.ests = 1/colMeans(my.samples)
overall.rate.est = 1/mean(my.samples)
print("overall.rate.est")
print(overall.rate.est)
#Histogram of rate parameter estimates
print(histogram(sample.rate.ests
,main=paste("Estimated exponential parameters for",R,"replicate samples of size",n)
,sub=paste0("True gamma distribution alpha=",alpha, ", beta=",beta)
,col=med.blue
,xlim=c(0
,max(1.1*sample.rate.ests, 1.5*overall.rate.est))
,freq=FALSE
,xlab="Estimated rate parameter"))
}
#Run the function with different inputs
PlotGamma(n=1000, alpha=2, beta=2, R=10000)
PlotGamma(n=1000, alpha=1, beta=2, R=10000)
# Law of large numbers example --------------------------------------------
#Function to generate R size n samples from binomial, exponential, or chisq
#Saves mean of each replicate and plots histogram of sample means
ShowLLN <- function(distribution, R, n) {
#Sample
if (toupper(distribution) == "BINOMIAL") {
#Generate the samples
my.samples = matrix(data=rbinom(n=n*R, size=6, prob=0.3)
,nrow=n
,ncol=R)
#Set histogram color
hist.color = brewer.pal(n=5,name="Set3")[3]
} else if (toupper(distribution) == "EXPONENTIAL") {
#Generate the samples
my.samples = matrix(data=rexp(n=n*R)
,nrow=n
,ncol=R)
#Set histogram color
hist.color = brewer.pal(n=5,name="Set3")[4]
} else if (toupper(distribution) == "CHISQ") {
#Generate the samples
my.samples = matrix(data=rchisq(n=n*R,df=1)
,nrow=n
,ncol=R)
#Set histogram color
hist.color = brewer.pal(n=5,name="Set3")[5]
} else {
warning(paste("FUNCTION showlln CANNOT UNDERSTAND DISTRIBUTION INPUT",distribution))
}
#Save the means
sample.means = colMeans(my.samples)
#Histogram of means
hist(sample.means
,main=paste0(toupper(distribution))
,sub=paste0("R=",R," n=",n)
,col=hist.color
,freq=FALSE
,xlab="Sample mean"
,xlim=c(0,3) )
}
#Use the function
sample.sizes = c(10, 100, 1000)
distributions = c("binomial", "exponential", "chisq")
#Plot for the different sample sizes
#Loop over distributions
par(mfrow=c(3,3))
for (distribution in distributions) {
#Loop over sample sizes
for (n in sample.sizes) {
#Plot for each of the sample sizes
ShowLLN(distribution=distribution,R=100,n=n)
}
}
# Central Limit Theorem example -------------------------------------------
#Function to generate R size n samples from binomial, exponential, or chisq
#Saves mean of each replicate and plots histogram of sample means
#Standardizes and plots to show the CLT
ShowCLT <- function(distribution, R, n) {
#Means for the different distributions
dist.means = c(0.3, 1, 1)
dist.vars = c(dist.means[1]^2, dist.means[2]^-1, 2*dist.means[3])
#Sample
if (toupper(distribution) == "BINOMIAL") {
#Generate the samples
my.samples = matrix(data=rbinom(n=n*R, size=6, prob=dist.means[1])
,nrow=n
,ncol=R)
#Set histogram color
hist.color = brewer.pal(n=5,name="Set3")[3]
#Set mean
true.mean = dist.means[1]*6
#Set variance
true.variance = dist.vars[1]
} else if (toupper(distribution) == "EXPONENTIAL") {
#Generate the samples
my.samples = matrix(data=rexp(n=n*R, rate=1/dist.means[2])
,nrow=n
,ncol=R)
#Set histogram color
hist.color = brewer.pal(n=5,name="Set3")[4]
#Set mean
true.mean = dist.means[2]
#Set variance
true.variance = dist.vars[2]
} else if (toupper(distribution) == "CHISQ") {
#Generate the samples
my.samples = matrix(data=rchisq(n=n*R,df=dist.means[3])
,nrow=n
,ncol=R)
#Set histogram color
hist.color = brewer.pal(n=5,name="Set3")[5]
#Set mean
true.mean = dist.means[3]
#Set variance
true.variance = dist.vars[3]
} else {
warning(paste("FUNCTION showlln CANNOT UNDERSTAND DISTRIBUTION INPUT",distribution))
}
#Save the means
sample.means = colMeans(my.samples)
sample.means.standardized = (sample.means - true.mean)*sqrt(n)
#Histogram of means
hist(sample.means.standardized
,main=paste0(toupper(distribution))
,sub=paste0("R=",R,", n=",n)
,col=hist.color
,freq=FALSE
,xlab="Sample 'standardized' mean"
,ylim=c(0,0.5)
,xlim=c(-5,5)
)
#Add normal overlay
lines(x=seq(-5,5,0.1),y=dnorm(seq(-5,5,0.1)))
}
#Use the function
sample.sizes = c(10, 100, 1000)
distributions = c("binomial", "exponential", "chisq")
#Plot for the different sample sizes
#Loop over distributions
par(mfrow=c(length(sample.sizes),length(distributions)))
for (distribution in distributions) {
#Loop over sample sizes
for (n in sample.sizes) {
#Plot for each of the sample sizes
ShowCLT(distribution=distribution,R=10000,n=n)
}
}