0% found this document useful (0 votes)
2 views7 pages

Example Midterm

The Bio220 Lab MT Exam requires students to create an Rscript to analyze data related to measles vaccination and cat behavior. It includes questions on statistical tests, data visualization, and hypothesis testing, with specific tasks such as creating contingency tables, performing Fisher's Exact Test, and conducting Chi-squared tests. Students must also analyze cat preferences between a box and a bed, calculate Z-scores for blood pressure readings, and identify outliers.

Uploaded by

guzidebilgin1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Example Midterm

The Bio220 Lab MT Exam requires students to create an Rscript to analyze data related to measles vaccination and cat behavior. It includes questions on statistical tests, data visualization, and hypothesis testing, with specific tasks such as creating contingency tables, performing Fisher's Exact Test, and conducting Chi-squared tests. Students must also analyze cat preferences between a box and a bed, calculate Z-scores for blood pressure readings, and identify outliers.

Uploaded by

guzidebilgin1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bio220 Lab MT Exam

2025-04-25

• Create an Rscript file to solve the questions. Give titles for each question using comment symbol: #.
Your script should contain only codes and comments, do NOT paste console results to your script file!
• Do not write unclear or redundant codes in your script file which may cause you to lose points.
• Load data file [Link] to your R environment.
• Exam duration is 100 minutes.
• Remember to periodically save your scripts (you can use ctrl+S shortcut) in case of unexpected situ-
ations (freezing/crushing etc).

Question 1 (30 pts)


Researchers are investigating whether measles vaccination reduces the severity of infection. They recorded
data from infected children, including vaccination status, age group, and infection severity. Data is
stored in vaccine object.

head(vaccine)

## Age_Group Vaccination_Status Severity


## 1 <1 Vaccinated Mild
## 2 <1 Vaccinated Mild
## 3 <1 Vaccinated Mild
## 4 <1 Vaccinated Mild
## 5 <1 Vaccinated Mild
## 6 <1 Vaccinated Mild

a. (2 pts) What is the appropriate statistical test to the question “does vaccination decrease infection
severity?”?
Hint: Ignore age groups.

# Fisher's Exact Test

b. (6 pts) Create a contingency table for vaccination status and severity.


Hint: Instead of using whole data frame, use the mentioned columns.

vaccine_tab <- table(vaccine$Vaccination_Status, vaccine$Severity)


vaccine_tab

##
## Mild Severe
## Unvaccinated 24 38
## Vaccinated 51 25

c. (5 pts) Visualize the data using an appropriate plot. Color bars by severity. Give an appropriate title.

1
mosaicplot(vaccine_tab, col = c("green4", "darkred"),
main = "Vaccination Status vs. Infection Severity")

Vaccination Status vs. Infection Severity

Unvaccinated Vaccinated
Mild
Severe

d. (10 pts) Perform the test you chose in question 1.


- State the null and alternative hypotheses.
- What is your statistical and biological conclusions based on the test result?

# H0: Vaccination status and infection severity are independent (OR = 1)


# Ha: Vaccination reduces severity (OR "mild-vaccinated" > 1)

[Link](vaccine_tab[c(2, 1),], alternative = "greater")

##
## Fisher’s Exact Test for Count Data
##
## data: vaccine_tab[c(2, 1), ]
## p-value = 0.0007537
## alternative hypothesis: true odds ratio is greater than 1
## 95 percent confidence interval:
## 1.690127 Inf
## sample estimates:
## odds ratio
## 3.200918

2
# p-value < 0.05: reject H0. Vaccination reduces infection severity.

e. (7 pts) Which group (vaccinated or unvaccinated) is more likely to experience severe infection, and by
how much?

odds_ratio <- [Link](vaccine_tab[c(2, 1),], alternative = "greater")$estimate


odds_ratio

## odds ratio
## 3.200918

# The unvaccinated group is ~3.2 times more likely to experience severe infection.

Question 2 (30 pts)

We will continue using the same vaccine dataset from Question 1.


a. (2 pts) Which statistical test would you use to test whether infected children are uniformly distributed
across age groups?

# Chi-squared goodness-of-fit test

b. (4 pts) Create a vector containing the number of observed cases (infected children) for each age group.
Hint: Use the table() function to create a frequency table of age groups.

obs <- table(vaccine$Age_Group)


obs

##
## <1 >5 1–3 3–5
## 24 29 55 30

c. (4 pts) What is the expected probability for each age group under uniform distribution? Calculate
expected frequencies accordingly.

expected_freq <- rep(1/4, 4) * sum(obs)


expected_freq

## [1] 34.5 34.5 34.5 34.5

d. (6 pts) Visualize the observed frequencies. Add a horizontal line showing expected frequency. Add a
proper title and adjust y-axis limit. Use an arbitrary color.

barplot(obs, ylim = c(0, 70),


col = "pink",
main = "Number of Cases Among Age Groups")
abline(h = expected_freq[1])

3
Number of Cases Among Age Groups
70
60
50
40
30
20
10
0

<1 >5 1−3 3−5

e. (4 pts) List the assumptions of the chi-squared test. Do these assumptions hold?

# Assumptions:
# - All expected frequencies > 1
# - No more than 20% of expected frequencies < 5

# All expected frequencies > 5, so assumptions are satisfied.

f. (10 pts) If assumptions are satisfied, perform the test.


- State the null and alternative hypotheses.
- What is your statistical and biological conclusions based on the test result?

# H0: Cases are uniformly distributed across age groups.


# Ha: Distribution of cases differs among age groups.

[Link](obs, p = rep(1/4, 4))

##
## Chi-squared test for given probabilities
##
## data: obs
## X-squared = 16.841, df = 3, p-value = 0.0007621

# p-value < 0.05, reject H0. Cases are not distributed uniformly across age groups.

4
Question 3 (20 pts)

A cat behaviorist is running an experiment to determine whether the common belief is true: “If you give a
cat two choices — a luxury bed and a cardboard box — the cat will always choose the box.” To test this,
the scientist places 79 cats in a room, each given the choice between a plush velvet pet bed and a plain
cardboard box. It records the results in a csv file named “[Link]”.

head(cat)

## Type Choice
## 1 British Shorthair Box
## 2 British Shorthair Box
## 3 British Shorthair Box
## 4 British Shorthair Box
## 5 British Shorthair Box
## 6 British Shorthair Box

Answer the below questions based on the given data:


a. (5 pts) What is the most abundant cat type involved in the experiment?

table(cat$Type)

##
## British Shorthair Calico Tabby Tuxedo
## 19 9 29 22

#OR
sum(cat$Type == "British Shorthair")

## [1] 19

sum(cat$Type == "Calico")

## [1] 9

sum(cat$Type == "Tabby")

## [1] 29

sum(cat$Type == "Tuxedo")

## [1] 22

b. (5 pts) Subset the data frame into 2: box and bed. Store them in 2 separate data frames. How many
cats preferred bed and how many preferred the box?

5
bed = cat[cat$Choice == "Bed",]
box = cat[cat$Choice == "Box",]
nrow(bed)

## [1] 21

nrow(box)

## [1] 58

c. (10 pts) Based on given data, can we say that cats prefer boxes over beds? Make a hypothesis test (using
a test function of R). State the hypotheses, make a decision and a conclusion.

#H0: cats have no preference for box or bed, choice is random, p = 0.5
#HA: cats have preference for boxes, p > 0.5

[Link](nrow(box), (nrow(box) + nrow(bed)), alternative ="greater")

##
## Exact binomial test
##
## data: nrow(box) and (nrow(box) + nrow(bed))
## number of successes = 58, number of trials = 79, p-value = 1.881e-05
## alternative hypothesis: true probability of success is greater than 0.5
## 95 percent confidence interval:
## 0.6402316 1.0000000
## sample estimates:
## probability of success
## 0.7341772

#conclusion: Since p < 0.05, we reject the null hypothesis and we can say there is evidence that cat hav

Question 4 (20 pts)

The vector bp contains systolic blood pressure readings (in mmHg) of 20 patients. We want to see the
number of outliers in the data. For this purpose we want to calculate something called Z-score for each
patient. Z-score is helpful when identifying outliers or standardizing values across di!erent scales. It is
calculated as follows: Z-score = (value - mean) / standard deviation. We call patients with a Z-score less
than -2 or greater than 2 as outlier.

head(bp)

## [1] 126 128 120 107 114 111

a. (2 pts) Calculate the mean of the sample and store it in an object.

bp_mean = mean(bp)

b. (2 pts) Calculate the standard deviation of the sample and store it in an object.

6
bp_sd = sd(bp)

c. (2 pts) Create an empty vector called z.

z = c()

d. (10 pts) Write a for loop to calculate Z-score of each patient and store them in the vector z.
Hint 1: You have to use the formula given above. value should change at each iteration of the loop, it
corresponds to bp reading of each patient. mean and standard deviation is constant, you should use the
objects calculated in parts 4a and 4b.
Hint 2: You should use the loop index i two times: one for retrieving blood pressure reading of patients, the
other for storing calculated Z-score.

for(i in 1:20){
z[i] = (bp[i]-bp_mean)/bp_sd
}

e. (4 pts) How many of the patients are outliers (have a Z-score less than -2 or greater than 2) ?

sum((z < -2)|(z > 2))

## [1] 2

You might also like