0
0
R Programmingprogramming~10 mins

t-test in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - t-test
Start
Prepare data samples
Calculate sample means and variances
Compute t-statistic
Calculate degrees of freedom
Find p-value from t-distribution
Compare p-value with significance level
Decide to reject or not reject null hypothesis
End
The t-test compares the means of two groups by calculating a t-statistic and p-value to decide if the difference is significant.
Execution Sample
R Programming
x <- c(5, 7, 8, 6, 9)
y <- c(10, 12, 11, 14, 13)
t.test(x, y)
This code runs a t-test comparing two small samples x and y to check if their means differ significantly.
Execution Table
StepActionEvaluationResult
1Prepare sample xx = c(5,7,8,6,9)x = [5,7,8,6,9]
2Prepare sample yy = c(10,12,11,14,13)y = [10,12,11,14,13]
3Calculate mean of xmean(x)7
4Calculate mean of ymean(y)12
5Calculate variance of xvar(x)2.5
6Calculate variance of yvar(y)2.5
7Calculate sample sizeslength(x), length(y)5, 5
8Compute t-statistict = (7 - 12) / sqrt(2.5/5 + 2.5/5)-5
9Calculate degrees of freedomdf approx8
10Calculate p-value2 * pt(-abs(t), df)0.0015
11Compare p-value with 0.050.0015 < 0.05Reject null hypothesis
12ConclusionMeans differ significantlyEnd
💡 p-value is less than 0.05, so we reject the null hypothesis that means are equal.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10Final
xundefined[5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9]
yundefinedundefined[10,12,11,14,13][10,12,11,14,13][10,12,11,14,13][10,12,11,14,13][10,12,11,14,13][10,12,11,14,13][10,12,11,14,13][10,12,11,14,13][10,12,11,14,13][10,12,11,14,13]
mean_xundefinedundefinedundefined777777777
mean_yundefinedundefinedundefinedundefined1212121212121212
var_xundefinedundefinedundefinedundefinedundefined2.52.52.52.52.52.52.5
var_yundefinedundefinedundefinedundefinedundefinedundefined2.52.52.52.52.52.5
n_xundefinedundefinedundefinedundefinedundefinedundefinedundefined55555
n_yundefinedundefinedundefinedundefinedundefinedundefinedundefined55555
t_statundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined-5-5-5-5
dfundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined888
p_valueundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined0.00150.0015
decisionundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedReject null hypothesis
Key Moments - 3 Insights
Why is the t-statistic negative in the execution table?
Because the mean of x (7) is less than the mean of y (12), the numerator (mean_x - mean_y) is negative, resulting in a negative t-statistic as shown in step 8.
Why do we compare the p-value to 0.05 in step 11?
0.05 is a common significance level threshold; if p-value is less than 0.05, it means the observed difference is unlikely by chance, so we reject the null hypothesis as shown in step 11.
What does degrees of freedom (df) represent in the t-test?
Degrees of freedom approximate the amount of independent information in the data; it affects the shape of the t-distribution used to find the p-value, calculated in step 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 8. What is the value of the t-statistic?
A0
B5
C-5
D12
💡 Hint
Check the 'Result' column in step 8 of the execution_table.
At which step does the p-value get calculated?
AStep 7
BStep 10
CStep 5
DStep 12
💡 Hint
Look for the step labeled 'Calculate p-value' in the execution_table.
If the p-value was 0.1 instead of 0.0015, what would be the decision at step 11?
AAccept null hypothesis
BReject null hypothesis
CCannot decide
DRun test again
💡 Hint
Compare p-value to 0.05 threshold in step 11 of the execution_table.
Concept Snapshot
t.test(x, y) compares means of two samples.
Calculates t-statistic = difference of means / standard error.
Degrees of freedom affect p-value calculation.
If p-value < 0.05, reject null hypothesis.
Used to check if two groups differ significantly.
Full Transcript
This visual execution traces an R t-test comparing two samples x and y. First, the samples are prepared. Then their means and variances are calculated. Using these, the t-statistic is computed as the difference of means divided by the combined standard error. Degrees of freedom are approximated to determine the shape of the t-distribution. The p-value is found from this distribution. Finally, the p-value is compared to 0.05 to decide if the difference in means is statistically significant. Here, the p-value is very small (0.0015), so the null hypothesis that the means are equal is rejected.