Follow the lab test instructions published on Learn.
Run the code chunk below without editing it. This will create data for your report that are linked to your exam number. It will not run if you do not have an exam number inserted where you were asked to insert it.
# Run this code chunk WITHOUT editing it!
source("https://uoepsy.github.io/data/get_data_lt1_dapr1_2122.R")
For example, if we were to ask you to compute 2 to the power of 3, store it to an object x, and then print the value, your code chunk should look like this:
x <- 2^3
# The next line prints the result
x
## [1] 8
In your R environment you should see a data set called water. This contains data for 100 individuals taking part in a survey. They were asked to report their age group (non-adult/adult) and which type of water they usually drink.
Create a new object called tbl which contains the two-way contingency table of counts for each combination of age group and preferred type of water.
Answer below:
tbl <- water %>%
table()
# The next line prints the result
tbl
## UsuallyDrink
## AgeGroup Bottled Filtered Tap
## Non-adult 14 8 5
## Adult 27 18 28
Using ggplot, create an object called plot_tbl that displays the different water preferences faceted by age group.
Answer below:
plot_tbl <- ggplot(data = water, aes(x = UsuallyDrink)) +
geom_histogram(stat="count") +
facet_wrap("AgeGroup")
## Warning: Ignoring unknown parameters: binwidth, bins, pad
# The next line prints the result
plot_tbl
Create an object called p_age containing 2 numbers: the first is the probability of being a non-adult, and the second the probability of being an adult.
Answer below:
p_age <- c((27/100), (73/100))
# The next line prints the result
p_age
## [1] 0.27 0.73
Create an object called p_cond, containing the probability that, given the preference is filtered water, the participant is not an adult. The object p_cond should only be one number.
Answer below:
p_cond <- 8/26
# The next line prints the result
p_cond
## [1] 0.3076923
Create an object called p_adult_water, containing two probabilities: the first is the probability that an adult prefers bottled water, and the second the probability that an adult prefers filtered or tap water.
Answer below:
p_adult_water <- c(27/73, (18+28)/73)
# The next line prints the result
p_adult_water
## [1] 0.369863 0.630137