-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbcwr01.R
143 lines (92 loc) · 3.76 KB
/
bcwr01.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
# Bayesian Computation with R (Second Edition)
# by Jim Albert
# 1 An Introduction to R
# 1.1 Overview
# 1.2 Exploring a Student Dataset
# 1.2.1 Introduction to the Dataset
# 1.2.2 Reading the Data into R
# studentdata <- read.table("studentdata.txt", sep = "\t", header = TRUE)
# This dataset is also available as part of the LearnBayes package.
library(LearnBayes)
data(studentdata)
studentdata[1, ]
# attach(studentdata)
# 1.2.3 R Commands to Summarize and Graph a Single Batch
table(studentdata$Drink)
barplot(table(studentdata$Drink), xlab = "Drink", ylab = "Count")
# Fig. 1.1. Barplot of the drinking preference of the statistics students.
studentdata$hours.of.sleep <- with(studentdata, WakeUp - ToSleep)
summary(studentdata$hours.of.sleep)
hist(studentdata$hours.of.sleep, main = "")
# Fig. 1.2. Histogram of the hours of sleep of the statistics students.
# 1.2.4 R Commands to Compare Batches
boxplot(hours.of.sleep ~ Gender, data = studentdata, ylab = "Hours of Sleep")
# Fig. 1.3. Parallel boxplots of the hours of sleep of the male and female students.
str(studentdata)
female.Haircut <- with(studentdata, Haircut[Gender == "female"])
male.Haircut <- with(studentdata, Haircut[Gender == "male"])
summary(female.Haircut)
summary(male.Haircut)
# 1.2.5 R Commands for Studying Relationships
par(pty = "s")
with(studentdata, plot(jitter(ToSleep), jitter(hours.of.sleep)))
# Fig. 1.4. Scatterplot of wake-up time and hours of sleep for students.
with(studentdata, plot(jitter(ToSleep), jitter(hours.of.sleep)))
fit <- lm(hours.of.sleep ~ ToSleep, data = studentdata)
fit
abline(fit)
# Fig. 1.5. Scatterplot of wake-up time and hours of sleep for students with least- squares line plotted on top.
# 1.3 Exploring the Robustness of the t Statistic
# 1.3.1 Introduction
# 1.3.2 Writing a Function to Compute the t Statistic
x <- rnorm(10, mean = 50, sd = 10)
y <- rnorm(10, mean = 50, sd = 10)
m <- length(x)
n <- length(y)
sp <- sqrt(((m - 1)*sd(x)^2+ (n - 1)*sd(y)^2)/(m + n - 2))
t.stat <- (mean(x) - mean(y))/(sp*sqrt(1/m+ 1/n))
t.stat
tstatistic <- function(x, y) {
m <- length(x)
n <- length(y)
sp <- sqrt(((m - 1)*sd(x)^2 + (n - 1)*sd(y)^2)/(m + n - 2))
t.stat <- (mean(x) - mean(y))/(sp*sqrt(1/m + 1/n))
return(t.stat)
}
#source("tstatistic.R")
data.x <- c(1, 4, 3, 6, 5)
data.y <- c(5, 4, 7, 6, 10)
tstatistic(data.x, data.y)
# 1.3.3 Programming a Monte Carlo Simulation
# simulation algorithm for normal populations
alpha <- 0.1 # sets alpha
m <- 10 # sets m
n <- 10 # sets n
N <- 10000 # sets the number of simulations
n.reject <- 0 # counter of num. of rejections
for (i in 1:N) {
x <- rnorm(m, mean = 0, sd = 1) # simulates xs from population 1
y <- rnorm(n, mean = 0, sd = 1) # simulates ys from population 2
t.stat <- tstatistic(x, y) # computes the t statistic
if (abs(t.stat) > qt(1 - alpha/2, n + m - 2))
n.reject <- n.reject + 1 # reject if |T| exceeds critical pt
}
true.sig.level <- n.reject/N # est. is proportion of rejections
true.sig.level
# 1.3.4 The Behavior of the True Significance Level Under Different Assumptions
# simulation algorithm for normal and exponential populations
# storing the values of the t statistic in vector tstat
m <- 10
n <- 10
my.tsimulation <- function() {
tstatistic(rnorm(m, mean = 10, sd = 2), rexp(n, rate = 1/10))
}
tstat.vector <- replicate(10000, my.tsimulation())
plot(density(tstat.vector), xlim = c(-5, 8), ylim = c(0, 0.4), lwd = 3)
curve(dt(x, df = 18), add = TRUE)
legend(4, 0.3, c("exact", "t(18)"), lwd = c(3,1))
# Fig. 1.6. Exact sampling density of the t statistic when sampling from normal and exponential distributions.
# The t sampling density assuming normal populations is also displayed.
# 1.4 Further Reading
# 1.5 Summary of R Functions
# 1.6 Exercises