How to Perform Basic Statistical Analysis in R

How to Perform Basic Statistical Analysis in R

Statistical analysis represents a methodology that encompasses both descriptive and inferential approaches to extract meaningful insights from quantitative information. Through the application of mathematical principles and computational techniques, statistical analysis provides researchers and practitioners with a framework for understanding data distributions, relationships, and underlying patterns that might otherwise remain obscured in raw datasets.

The R programming language, with its rich ecosystem of statistical packages and functions, serves as an invaluable tool for implementing these analytical methods, enabling practitioners to conduct analyses ranging from summary statistics to hypothesis testing while maintaining reproducibility.

As a reminder:

  1. Descriptive statistics summarize the data. They include the mean, median, and standard deviation.
  2. Inferential statistics help make predictions and conclusions. They involve testing hypotheses and finding relationships between variables.

Let’s take a look at performing statistical analysis in R.

Loading the Dataset

Before doing statistical analysis, we will need to load a dataset into R. We will use the built-in mtcars dataset for this example. The mtcars dataset includes data on car performance.

# Load the built-in mtcars dataset
data(mtcars)

# Display the first few rows of this dataset
head(mtcars)

dataset

Descriptive Statistics

Mean, Median, and Mode

The mean is the average. The median is the middle value. The mode is the most frequent number. These are basic ways to summarize data.

# Calculate mean of mpg
mean_mpg <- mean(mtcars$mpg)

# Calculate median of mpg
median_mpg <- median(mtcars$mpg)

# Custom function to calculate mode
get_mode <- function(v) {
  uniq_v <- unique(v)
  uniq_v[which.max(tabulate(match(v, uniq_v)))]
}

# Calculate mode of mpg
mode_mpg <- get_mode(mtcars$mpg)

central_tendency

Variance and Standard Deviation

Variance shows how much the values in a dataset spread out from the mean. Standard deviation is the square root of variance. These tell us how much the data varies from the average. Both help explain how spread out the data is.

# Calculate variance of mpg
variance_mpg <- var(mtcars$mpg)

# Calculate standard deviation of mpg
sd_mpg <- sd(mtcars$mpg)

variance_std

Range and Interquartile Range

The range is the difference between the highest and lowest values in a dataset. The interquartile range (IQR) measures the spread of the middle 50% of the data.

# Calculate range of mpg
range_mpg <- range(mtcars$mpg)

# Calculate interquartile range (IQR) of mpg
iqr_mpg <- IQR(mtcars$mpg)

range_iqr

Data Visualization

Visualizing data is an important part of descriptive statistics. It helps us understand the distribution and spread of our data. Two common visualization methods are histograms and boxplots.

Histogram

A histogram is a graphical representation that shows the frequency distribution of a dataset. It allows us to see how values are distributed across different ranges.

# Create a histogram of mpg
hist(mtcars$mpg, main="Histogram of Miles Per Gallon", xlab="Miles Per Gallon", col="lightblue", border="black")

histogram

Boxplot

A boxplot (box-and-whisker plot) summarizes the data’s central tendency and variability. It displays the median, quartiles, and potential outliers.

# Create a boxplot of mpg by transmission type
boxplot(mpg ~ am, data=mtcars, main="Boxplot of Miles Per Gallon by Transmission Type", xlab="Transmission (0 = Automatic, 1 = Manual)", ylab="Miles Per Gallon", col=c("lightgreen", "lightpink"))

boxplot

Inferential Statistics

T-test

A t-test is a statistical method used to compare the means of two groups. It helps determine if the differences between the groups are significant. The test produces a p-value. A low p-value suggests a significant difference. If this happens, we reject the null hypothesis. This means the difference between the groups is statistically significant.

# Perform a t-test to compare mpg between automatic and manual cars
t_test_result <- t.test(mpg ~ am, data=mtcars)

# Print the results of the t-test
print(t_test_result)

t-test

Chi-Squared Test

A Chi-squared test checks for relationships between two categorical variables. It compares how often we see certain outcomes versus what we expect to see. The test calculates a statistic based on these frequencies. A low p-value indicates a significant association.

# Create a contingency table for the number of cylinders (cyl) and transmission type (am)
cyl_am_table <- table(mtcars$cyl, mtcars$am)

# Perform the Chi-Squared test
chi_sq_test <- chisq.test(cyl_am_table)

# Print the results of the Chi-Squared test
print(chi_sq_test)

chi-sq_test

Conclusion

In this article, we saw how to load data and calculate simple statistics like the mean and median. We also created graphs like histograms and boxplots to see how the data is spread. Finally, we talked about inferential statistics, like t-tests and chi-squared tests. These help us make predictions about larger groups from smaller samples.

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *