library(LM2GLMM)
knitr::opts_chunk$set(cache = FALSE,
                      fig.align = "center",
                      fig.width = 4,
                      fig.height = 4,
                      cache.path = "./cache_knitr/LM_intro_ex/",
                      fig.path = "./fig_knitr/LM_intro_ex/")
options(width = 200)
set.seed(1L)

Computing with R

Compute the following equation:


$\sqrt{\frac{2^{3+1}}{\frac{4}{5\times{6}}}-20}$


[Back to main menu](./Title.html#2)

Data exploration

Using the dataset TitanicSurvival, figure out:

dim(TitanicSurvival)
str(TitanicSurvival)
apply(TitanicSurvival, 2, function(x) any(is.na(x)))
focus <- with(TitanicSurvival, survived[sex == "female" & !is.na(age) & age < 20 & passengerClass == "2nd"])
table(focus, useNA = "always")

An example of programming

The following function can be used to investigate the statistical power of a t-test (i.e. the probability to detect a significant effect when there really is one = rate of true positive):

compare_heights <- function(n_group = 10, height_difference = 5) {
  male   <- rnorm(n = n_group, mean = 180, sd = 6)
  female <- rnorm(n = n_group, mean = 180 - height_difference, sd = 6)
  t_test_res <- t.test(male, female)
  t_test_res$p.value
}

N <- seq(from = 10, to = 60, by = 2)
power <- sapply(N, function(n) mean(replicate(100, compare_heights(n_group = n)) <= 0.05))
plot(power ~ N)


Can you run, read and understand this code?

Plots

Using the dataset TitanicSurvival, find a good way to show in one plot what influenced the survival of passengers.

[Back to main menu](./Title.html#2)


courtiol/LM2GLMM documentation built on July 3, 2022, 7:42 a.m.