Statistics with R
- Statistics with R
- R Objects, Numbers, Attributes, Vectors, Coercion
- Matrices, Lists, Factors
- Data Frames in R
- Control Structures in R
- Functions in R
- Data Basics: Compute Summary Statistics in R
- Central Tendency and Spread in R Programming
- Data Basics: Plotting – Charts and Graphs
- Normal Distribution in R
- Skewness of statistical data
- Bernoulli Distribution in R
- Binomial Distribution in R Programming
- Compute Randomly Drawn Negative Binomial Density in R Programming
- Poisson Functions in R Programming
- How to Use the Multinomial Distribution in R
- Beta Distribution in R
- Chi-Square Distribution in R
- Exponential Distribution in R Programming
- Log Normal Distribution in R
- Continuous Uniform Distribution in R
- Understanding the t-distribution in R
- Gamma Distribution in R Programming
- How to Calculate Conditional Probability in R?
- How to Plot a Weibull Distribution in R
- Hypothesis Testing in R Programming
- T-Test in R Programming
- Type I Error in R
- Type II Error in R
- Confidence Intervals in R
- Covariance and Correlation in R
- Covariance Matrix in R
- Pearson Correlation in R
- Normal Probability Plot in R
Two sample T-test in R
The two-sample t-test is a statistical test used to compare the means of two independent groups to determine if there’s a significant difference between them. In R, you can perform a two-sample t-test using the t.test()
function.
Here’s an example of how to perform a two-sample t-test in R:
1. First, create two vectors representing the data from the two groups
group1 <- c(12, 15, 17, 19, 22, 24, 28) group2 <- c(18, 20, 23, 25, 29, 31, 35)
2. Next, perform the two-sample t-test using the t.test()
function:
test_result <- t.test(group1, group2)
By default, the t.test()
function performs a two-sided test, assuming unequal variances between the two groups. You can also specify additional arguments if necessary:
var.equal = TRUE
to assume equal variances (performing a “pooled” t-test)alternative = "less"
oralternative = "greater"
for a one-sided test
3. Finally, display the test results:
print(test_result)
This will output the t-test results, including the t-value, degrees of freedom, and p-value. You can interpret the p-value to determine if there is a significant difference between the two groups’ means. If the p-value is less than your chosen significance level (e.g., 0.05), you can conclude that there is a statistically significant difference between the two group means.
Output
> test_result <- t.test(group1, group2) > print(test_result) Welch Two Sample t-test data: group1 and group2 t = -2.0203, df = 11.866, p-value = 0.06652 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -13.0731598 0.5017313 sample estimates: mean of x mean of y 19.57143 25.85714
Example – 2
Here’s another example:
First, let’s generate some synthetic data for two groups:
# Generate synthetic data set.seed(42) # Set seed for reproducibility group1 <- rnorm(n = 30, mean = 100, sd = 15) # 30 observations with mean=100, sd=15 group2 <- rnorm(n = 30, mean = 110, sd = 15) # 30 observations with mean=110, sd=15 # Perform two-sample t-test test_result <- t.test(group1, group2) # Print test result print(test_result) # Access specific values from the test result t_statistic <- test_result$statistic p_value <- test_result$p.value
To interpret the results, check the p-value. If the p-value is less than the chosen significance level (usually 0.05), you can reject the null hypothesis and conclude that there is a significant difference between the means of the two groups.