Open Rstudio to do the practicals. Note that tasks with * are optional.
R version 4.2.1 (2022-06-23 ucrt)
Use the function seq(…) to generate a sequence. Use the function dnorm(…) for the normal density function.
<- seq(from = -5, to = 5, by = 0.1)
x <- dnorm(x)
y plot(x, y, type = "l", col = "red", lwd = 2,
main = "Standard normal density function")
Use the function dnorm(…) for the normal density function. Check the arguments mean and sd.
<- seq(from = -10, to = 10, by = 0.2)
x <- dnorm(x, mean = 2, sd = 1)
y plot(x, y, type = "l", col = "red", lwd = 2,
main = "Standard normal density function")
Use the function pnorm(…) for the normal cumulative distribution function (cdf). Use the function qnorm(…) for the inverse of the cdf.
<- seq(from = -10, to = 10, by = 0.2)
x pnorm(q = 3, mean = 2, sd = 1)
## [1] 0.8413447
qnorm(0.5)
## [1] 0
qnorm(0.95)
## [1] 1.644854
Use the function pnorm(…) for the normal cumulative distribution function (cdf).
<- seq(from = -10, to = 10, by = 0.2)
x pnorm(q = 3, mean = 2, sd = 1, lower.tail = FALSE)
## [1] 0.1586553
Use the function pnorm(…) for the normal cumulative distribution function (cdf). Figure C = Figure A - Figure B:
<- seq(from = -10, to = 10, by = 0.2)
x pnorm(q = 4, mean = 2, sd = 1) - pnorm(2, mean = 2, sd = 1)
## [1] 0.4772499
Use the function rt(…) to generate random values from the t-distribution.
<- rt(n = 50, df = 50 - 1)
x hist(x)
Use the function dt(…) for the probability density function from the t-distribution. Use the function pt(…) for the cumulative distribution function.
<- c(6, -4, -2, 0, 2, 4, 6)
x dt(x, df = 6)
## [1] 0.0004217475 0.0040545779 0.0640361226 0.3827327723 0.0640361226 0.0040545779 0.0004217475
pt(q = -2, df = 6)
## [1] 0.04621316
pt(q = 2, df = 6, lower.tail = FALSE)
## [1] 0.04621316
Use the function qt(…) for the inverse of the cumulative distribution function.
<- rt(n = 60, df = 60 - 1)
x hist(x)
qt(p = 0.90, df = 60 - 1)
## [1] 1.296066
qt(p = 0.95, df = 60 - 1)
## [1] 1.671093
<- rt(n = 150, df = 150 - 1)
x hist(x)
qt(p = 0.90, df = 150 - 1)
## [1] 1.287259
qt(p = 0.95, df = 150 - 1)
## [1] 1.655145
Use the function rchisq(…) to generate random values from the chi-square distribution. Use the function dchisq(…) for the density function.
<- rchisq(n = 10000, df = 7)
x hist(x, freq = FALSE)
curve(expr = dchisq(x, df = 7), col = "red", lwd = 2, add = T)
Use the function qchisq(…) for the chi-square quantile function.
<- seq(from = 0, to = 1, by = 0.1)
x plot(qchisq(x, df = 2))
Use the function df(…) for the density function. Use the function pf(…) to calculate the area under the F-curve.
<- seq(from = 1, to = 20)
x <- df(x, df1 = 3, df2 = 1)
y1 <- df(x, df1 = 3, df2 = 3)
y2 <- df(x, df1 = 3, df2 = 6)
y3 <- df(x, df1 = 6, df2 = 6)
y4
plot(x, y1, type = "l")
lines(x, y2, col = "red")
lines(x, y3, col = "blue")
lines(x, y4, col = "purple")
pf(q = 1.5, df = 10, df2 = 20, lower.tail = TRUE)
## [1] 0.7890535
We have 10 multiple choice questions in the exam. Each question has 6 possible answers, but only one of them is correct.
Use the function dbinom(…) for the density function. Use the function pbinom(…) for the cumulative distribution function.
# Since only one out of 6 possible answers is correct,
# the probability of answering a question correctly is 1/6 = 0.17
dbinom(x = 3, size = 10, prob = 0.17)
## [1] 0.1599833
# For the probability of having 3 or less correct answers we
# use dbinom for x = 0,..., 3.
<- dbinom(x = 0, size = 10, prob = 0.17)
p1 <- dbinom(x = 1, size = 10, prob = 0.17)
p2 <- dbinom(x = 2, size = 10, prob = 0.17)
p3 <- dbinom(x = 3, size = 10, prob = 0.17)
p4
+ p2 + p3 + p4 p1
## [1] 0.9258528
# Alternatively
pbinom(q = 3, size = 10, prob = 0.17)
## [1] 0.9258528
We have 10 multiple choice questions in the exam. Each question has 4 possible answers, but only one of them is correct.
Use the function dbinom(…) for the density function. Use the function pbinom(…) for the cumulative distribution function.
# Since only one out of 6 possible answers is correct,
# the probability of answering a question correctly is 1/4 = 0.25
dbinom(x = 4, size = 10, prob = 0.25)
## [1] 0.145998
# For the probability of having four or more correct answers
# we use dbinom for x = 4,..., 10.
<- dbinom(x = 4, size = 10, prob = 0.25)
p1 <- dbinom(x = 5, size = 10, prob = 0.25)
p2 <- dbinom(x = 6, size = 10, prob = 0.25)
p3 <- dbinom(x = 7, size = 10, prob = 0.25)
p4 <- dbinom(x = 8, size = 10, prob = 0.25)
p5 <- dbinom(x = 9, size = 10, prob = 0.25)
p6 <- dbinom(x = 10, size = 10, prob = 0.25)
p7
+ p2 + p3 + p4 + p5 + p6 + p7 p1
## [1] 0.2241249
# Alternatively
# Note: if lower.tail = TRUE (default), probabilities are Pr(X ≤ x), otherwise, Pr(X > x).
pbinom(q = 3, size = 10, prob = 0.25, lower.tail = FALSE)
## [1] 0.2241249
# Pr(X > x) does not include x therefore we use x-1 (4-1).
© Eleni-Rosalina Andrinopoulou