-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheksamen22-mwasa.R
More file actions
394 lines (295 loc) · 10.5 KB
/
eksamen22-mwasa.R
File metadata and controls
394 lines (295 loc) · 10.5 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# -----------------------------------------------------------------------------
# Question 1
# -----------------------------------------------------------------------------
X_mean <- 2
Y_mean <- -1
# theorem 2.56
2*X_mean+Y_mean
# 3
# -----------------------------------------------------------------------------
# Question 2
# -----------------------------------------------------------------------------
y <- c(59, 52, 42, 59, 24, 24, 40, 32, 63, 55, 34, 24)
x <- c(298, 303, 270, 287, 236, 245, 265, 233, 286, 290, 264, 239)
fit <- lm(y ~ x)
summary(fit)
# 3 - 0.8334
# -----------------------------------------------------------------------------
# Question 3
# -----------------------------------------------------------------------------
y <- c(59, 52, 42, 59, 24, 24, 40, 32, 63, 55, 34, 24)
x <- c(298, 303, 270, 287, 236, 245, 265, 233, 286, 290, 264, 239)
fit <- lm(y ~ x)
qqnorm(fit$residuals)
qqline(fit$residuals)
# 2
# -----------------------------------------------------------------------------
# Question 4
# -----------------------------------------------------------------------------
df <- 10
n <- df+2
s <- 0.0763
p <- 1
conf_interval <- 95
alpha <- (100 - conf_interval) / 100
1-alpha/2
t <- qt(1 - alpha / 2, n - (p+1)) # T-value
t
# 3
# -----------------------------------------------------------------------------
# Question 5
# -----------------------------------------------------------------------------
# 4
# -----------------------------------------------------------------------------
# Question 6
# -----------------------------------------------------------------------------
df <- 34
# To do it from the standard deviation, sample mean and sample size
# n is sample size
n <- df+1
# x is sample mean
x <- 6.461533
# sample standard deviation is
s <- 0.845 # wtf where
# the confidence interval thingy
conf_interval <- 99
alpha <- (100 - conf_interval) / 100
# Calculate
x + c(-1, 1) * qt(1 - alpha / 2, n - 1) * s / sqrt(n)
# 3 - 6.07 6.85
# -----------------------------------------------------------------------------
# Question 7
# -----------------------------------------------------------------------------
# To do it from the standard deviation, sample mean and sample size
# n is sample size
n <- 34+1
# x is sample mean
x <- 1.857769
# variance
v <- 0.01643549
# sample standard deviation is
s <- sqrt(v)
t <- 45.117
# the confidence interval thingy
conf_interval <- 95
alpha <- (100 - conf_interval) / 100
# Calculate
x + c(-1, 1) * qt(1 - alpha / 2, n - 1) * s / sqrt(n)
x + t
# 4
# -----------------------------------------------------------------------------
# Question 8
# -----------------------------------------------------------------------------
t.test(log(range1), log(range2), paired = TRUE)
#4?
# -----------------------------------------------------------------------------
# Question 9
# -----------------------------------------------------------------------------
# method 3.63
ME <- 0.1
s <- 0.8
n <- ((1.96*s)/ME)^2
n
# 4
# -----------------------------------------------------------------------------
# Question 10
# -----------------------------------------------------------------------------
SST <- 14.163 + 20.305
# 4
# -----------------------------------------------------------------------------
# Question 11
# -----------------------------------------------------------------------------
# 4 - 0.02 < 0.05 (p)
# -----------------------------------------------------------------------------
# Question 12
# -----------------------------------------------------------------------------
# binomial
# How many "hits" you want / the value you want to hit
x <- 2
# size is how many times you try / draw
size <- 6
# the probability of getting a hit (percent - 3% -> 0.03)
prob <- 35 / 100
# probability of two or more (at least), so probability of 1 or fewer, other way around
1 - pbinom(x-1, size = size, prob = prob)
# 5 - 0.681
# -----------------------------------------------------------------------------
# Question 13
# -----------------------------------------------------------------------------
# simulation
# size is how many times you try / draw
size <- 6
# the probability of getting a hit (percent - 3% -> 0.03)
prob <- 35 / 100
# Number of values
k <- 1000000
x <- rbinom(n = k, size = size, prob = prob)
var(x)
# 4 - 1.37
# -----------------------------------------------------------------------------
# Question 14
# -----------------------------------------------------------------------------
# poisson
bus_per_minute <- 1/15
minutes <- 20
# In a Poisson distribution, the parameter lambda (λ) represents the average number of events that occur in a given
# interval of time or space. It is also known as the rate parameter or the expected value of the distribution.
lambda <- bus_per_minute*minutes
x <- 1
# dpois: This function calculates the probability density function (PDF) of the Poisson
# distribution at a given point. The PDF gives the probability of observing a value within
# a given range.
dpois(x, lambda = lambda)
# 3 - 35.1%
# -----------------------------------------------------------------------------
# Question 15
# -----------------------------------------------------------------------------
# # poisson
# bus_per_minute <- 1/15
# minutes <- 20
# # In a Poisson distribution, the parameter lambda (λ) represents the average number of events that occur in a given
# # interval of time or space. It is also known as the rate parameter or the expected value of the distribution.
# lambda <- 1/15*60
#
# x <- 90
#
# # ppois: This function calculates the cumulative distribution function (CDF) of the Poisson
# # distribution at a given point. The CDF gives the probability of observing a value less than
# # or equal to a given point.
# qpois(x, lambda = lambda)
# 1 - qpois(x, lambda = lambda)
#
# n <- 10
# # Simulate
# x <- rexp(n, rate = 1/15)
# # Plot the empirical pdf
# plot(table(x)/n)
# # Add the pdf to the plot
# lines(0:20, dpois(0:20,lambda), type="h", col="red")
#
#
# gen <- dnorm(1, 15)
# hist(gen)
# ------------------------
rate <- 1/15
x <- 0.9
# qexp is the inverse cumulative distribution function (ICDF) of the exponential distribution. It calculates the value
# at which a given probability occurs in the exponential distribution.
qexp(x, rate = rate)
# 3 - 34.53
# -----------------------------------------------------------------------------
# Question 16
# -----------------------------------------------------------------------------
runif(50, 0, 100)
# 1
# -----------------------------------------------------------------------------
# Question 17
# -----------------------------------------------------------------------------
df <- 10
q <- 0.371
2*(1 - pt(q=q, df=df))
# 4 - 0.7184
# -----------------------------------------------------------------------------
# Question 18
# -----------------------------------------------------------------------------
df <- 10
p <- 2
n <- df + (p+1)
n
# 5 - 13
# -----------------------------------------------------------------------------
# Question 19
# -----------------------------------------------------------------------------
# 1 - p < 0.5 (sign diff)
# -----------------------------------------------------------------------------
# Question 20
# -----------------------------------------------------------------------------
simsamples <- replicate(10000, sample(glutenA, replace = FALSE)) # FALSE?
simmeans <- apply(simsamples, 2, mean)
quantile(simmeans, c(0.025, 0.975))
# 4
# -----------------------------------------------------------------------------
# Question 21
# -----------------------------------------------------------------------------
simsamples <- replicate(10000, rnorm(10,mean(glutenA),sd(glutenA)))
simsds <- apply(simsamples, 2, sd)
quantile(simsds, c(0.025, 0.975))
# 2 (or 1 if mean)
# -----------------------------------------------------------------------------
# Question 22
# -----------------------------------------------------------------------------
# 1
# -----------------------------------------------------------------------------
# Question 23
# -----------------------------------------------------------------------------
# 5
# -----------------------------------------------------------------------------
# Question 24
# -----------------------------------------------------------------------------
n <- 1268
p <- (852)/n
conf_interval <- 95
alpha <- (100 - conf_interval) / 100
p + c(-1, 1) * sqrt(p * (1 - p) / n) * qnorm(1 - alpha / 2)
# 4 - 0.6460817 0.6977669
# -----------------------------------------------------------------------------
# Question 25
# -----------------------------------------------------------------------------
# p1 <- 746/1085
# p2 <- 339/1085
n1 <- 852
n2 <- 416
p1 <- 746/n1
p2 <- 339/n2
conf_interval <- 95
alpha <- (100 - conf_interval) / 100
p1-p2 + c(-1,1) * sqrt(p1*(1-p1)/n1+p2*(1-p2)/n2)*qnorm(1 - alpha / 2)
# 3 - 0.01727775 0.10408826
# -----------------------------------------------------------------------------
# Question 26
# -----------------------------------------------------------------------------
total_allgender <- 1268
total_agree <- 1085
total_male <- 416
male_agree <- total_agree/total_allgender*total_male
male_agree
# 5
# -----------------------------------------------------------------------------
# Question 27
# -----------------------------------------------------------------------------
# Insert values
n1 <- 3
n2 <- 2
alpha <- 0.05
# Calculate values
df2d <- (n1 - 1) * (n2 - 1)
# Calculate Result
qchisq(1 - alpha, df2d)
# 2 - 5.991
# -----------------------------------------------------------------------------
# Question 28
# -----------------------------------------------------------------------------
x <- c(-4.8, -0.1, -1.4, -3.3, 1.1, -2.2, -2.6, -2.8)
sd(x)
# 4 - 1.86
# -----------------------------------------------------------------------------
# Question 29
# -----------------------------------------------------------------------------
x <- c(-4.8, -0.1, -1.4, -3.3, 1.1, -2.2, -2.6, -2.8)
hist(x)
# 3 - C
# -----------------------------------------------------------------------------
# Question 30
# -----------------------------------------------------------------------------
# x <- c(-4.8, -0.1, -1.4, -3.3, 1.1, -2.2, -2.6, -2.8)
x <- 10 # Mean
s <- 5 # Standard Deviation
q <- 20 # Quantile to check for
# P( X <= x )
pnorm(10-1, x, s) # P(Y < 10)
1-pnorm(10-1, x, s) # P(Y > 10)
pnorm(10-1, x, s)-pnorm(0-1, x, s) # P(0 < Y < 10)
pnorm(20-1, x, s)-pnorm(0-1, x, s) # P(0 < Y < 20)
1-pnorm(20-1, x, s) # P(Y > 20)
pnorm(20, x, s)-pnorm(0, x, s) # P(0 < Y < 20)
# 4 - 0.954