0
0
R Programmingprogramming~20 mins

Chi-squared test in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Chi-squared Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a basic Chi-squared test in R
What is the output of this R code performing a Chi-squared test on a 2x2 table?
R Programming
data <- matrix(c(10, 20, 20, 40), nrow = 2)
result <- chisq.test(data)
result$p.value
A1.0000000
B0.9876543
C0.0012345
D0.0455003
Attempts:
2 left
💡 Hint
Check the p-value returned by chisq.test for the given matrix.
🧠 Conceptual
intermediate
1:30remaining
Understanding Chi-squared test assumptions
Which of the following is NOT an assumption of the Chi-squared test in R?
AObservations must be independent
BData must be normally distributed
CData should be categorical
DExpected frequencies should be at least 5 in each cell
Attempts:
2 left
💡 Hint
Think about the type of data Chi-squared test is designed for.
🔧 Debug
advanced
2:00remaining
Identify the error in Chi-squared test code
What error will this R code produce?
R Programming
data <- matrix(c(5, 3, 2), nrow = 3)
chisq.test(data)
AError: 'data' must have at least 2 columns and 2 rows
BError: 'data' must be a square matrix
CError: 'x' must be a matrix or a table
DNo error, test runs successfully
Attempts:
2 left
💡 Hint
Check the shape of the matrix and requirements for chisq.test.
🚀 Application
advanced
3:00remaining
Calculate Chi-squared statistic manually
Given the observed counts matrix in R: obs <- matrix(c(30, 10, 20, 40), nrow=2) Which code correctly calculates the Chi-squared test statistic manually (without using chisq.test)?
A
expected &lt;- outer(rowSums(obs), colSums(obs)) / sum(obs)
chisq &lt;- sum((obs - expected)^2 / expected)
chisq
B
expected &lt;- rowSums(obs) * colSums(obs) / sum(obs)
chisq &lt;- sum((obs - expected)^2 / expected)
chisq
C
expected &lt;- outer(rowSums(obs), colSums(obs)) * sum(obs)
chisq &lt;- sum((obs - expected)^2 / expected)
chisq
D
expected &lt;- outer(rowSums(obs), colSums(obs)) / sum(obs)
chisq &lt;- sum((expected - obs)^2 / obs)
chisq
Attempts:
2 left
💡 Hint
Expected counts are calculated by multiplying row and column sums divided by total sum.
Predict Output
expert
2:30remaining
Output of Chi-squared test with simulated data
What is the output of this R code snippet?
R Programming
set.seed(123)
data <- matrix(sample(1:100, 9), nrow=3)
result <- chisq.test(data)
round(result$statistic, 2)
A4.67
B7.89
C5.12
D2.34
Attempts:
2 left
💡 Hint
Run the code with the given seed to get the Chi-squared statistic.