Review: Quick R Tips

Review: Vectors

In R, vectors are lists that can hold multiple pieces of data of the same type. For example, a vector could be used to store a list of multiple numbers (e.g. 1, 2, and 3).

In R, you can create a vector using the syntax c(). You place the pieces of data you want included in the vector separated by a comma within the parentheses:

my_first_vector <- c(1, 2, 3)

#recall the variable to print the vector values
my_first_vector 
## [1] 1 2 3

Applying a Chi Square Test

R is a great tool for doing statistical tests. Today we will use R to perform chi square tests to compare the outcomes of experimental Drosophila crosses with the expected outcomes.

For example, if we perform a cross between two heterozygous pea plants, perhaps we observe 416 yellow peas and 138 green peas. Based on a Punnett square, the expected outcome is 3:1 or 0.75 to 0.25. To run a chi square test, we need to put these values into vectors, then we can run the chi square test. For example:

# define variables with observed and expected values
observed_peas <- c(416, 138)
expected_peas <- c(0.75, 0.25)

# run chi square test
chisq.test(x=observed_peas, p=expected_peas, correct = FALSE)
## 
##  Chi-squared test for given probabilities
## 
## data:  observed_peas
## X-squared = 0.0024067, df = 1, p-value = 0.9609

Note that the expected values are given as a decimal value, while the observed data should be given as counts of number of individuals.

In this example, our p-value is 0.9609, which is very high. Therefore, we fail to reject the null hypothesis, meaning the variation we see between the observed and expected values is likely due to random chance.

You can also use fractions instead of decimals to indicate the expected values. Each fraction must be placed in parenthesis like is shown below:

# define variables with observed and expected values
observed_peas_2 <- c(416, 138)
expected_peas_2 <- c((3/4), (1/4))

# run chi square test
chisq.test(x=observed_peas_2, p=expected_peas_2, correct = FALSE)
## 
##  Chi-squared test for given probabilities
## 
## data:  observed_peas_2
## X-squared = 0.0024067, df = 1, p-value = 0.9609

Practice Exercise

To perform your own Chi square tests as part of the Drosophila simulation activity, follow these steps:

  1. Create a code chuck, give it an appropriate (and unique) name, then paste the appropriate code from above as a starting point.
  1. Change the name of the variables (e.g., observed_peas_2 and expected_peas_2) to a name relevant to your test. You should also change the values within the vectors to match the data from your crosses.

  2. Finally, you need to modify the code to run the chi square test. x = and p = should be changed to run using your new variable names. Do not modify correct = FALSE.

E1. Chi Square Test Monohybrid Cross
Apply a chi square test to the monohybrid cross in Part 1 of the Drosophila simulation.

observed_fruit_flys_1 <- c(745, 264)

#The total amount of fruit flys was 1009 the 745 fruit flys were all wildtype, and the 264 were all wild type with sepia eye color.

expected_fruit_flys_1 <- c((3/4), (1/4))

chisq.test(x=observed_fruit_flys_1, p=expected_fruit_flys_1, correct = FALSE)
## 
##  Chi-squared test for given probabilities
## 
## data:  observed_fruit_flys_1
## X-squared = 0.72977, df = 1, p-value = 0.393

E2. Chi Square Test Dihybrid Cross
Apply a chi square test to the dihybrid cross in Part 2 of the Drosophila simulation.

observed_fruit_flys_2 <- c(550, 195, 184, 49)
#The total population of fruit flies is 978. 550 of the fruit flies were fully wild type, 195 of the fruit flies were vestigal wings with wild type body color. 184 of the fruit flies were ebony body color and wild type wings. 49 of the fruit flies were both ebony body color and vestigal wing.
expected_fruit_flys_2 <- c((9/16), (3/16), (3/16), (1/16))

chisq.test(x=observed_fruit_flys_2, p=expected_fruit_flys_2, correct = FALSE)
## 
##  Chi-squared test for given probabilities
## 
## data:  observed_fruit_flys_2
## X-squared = 3.1443, df = 3, p-value = 0.3699

When you finish, click Knit to see your code, outputs, and formatted text in the final document.