0% found this document useful (0 votes)
13 views5 pages

Statistical Analysis with R Techniques

The assignment by Vaibhav Keshari focuses on statistical analysis using R with built-in datasets like mtcars and AirPassengers. It covers various operations including data exploration, regression, correlation, time series analysis, and data visualization using ggplot2. The conclusion emphasizes the effectiveness of R programming for data manipulation and analysis.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Statistical Analysis with R Techniques

The assignment by Vaibhav Keshari focuses on statistical analysis using R with built-in datasets like mtcars and AirPassengers. It covers various operations including data exploration, regression, correlation, time series analysis, and data visualization using ggplot2. The conclusion emphasizes the effectiveness of R programming for data manipulation and analysis.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment: Statistical Analysis R

Submitted By:
 Name: Vaibhav Keshari
 Roll Number: 1021
 Subject: Roman with R
 Course: B.A. Program

Table of Contents
1. Introduction
2. Data Exploration
3. Data Frame Operations
4. Vector Operations
5. Matrix and Array Operations
6. Correlation Analysis
7. Regression Analysis
8. Time Series Analysis
9. Pie Chart Visualization
10. Data Visualization with ggplot2
11. Export and Load Operations
12. Conclusion
1. Introduction
This assignment demonstrates statistical concepts and R
programming techniques using the built-in mtcars and
AirPassengers datasets. It includes operations on vectors,
matrices, arrays, regression, correlation, time series, and data
visualization.

2. Data Exploration
# Load the dataset and explore its structure
data("mtcars")
head(mtcars) # Display the first few rows
summary(mtcars) # Summary statistics
str(mtcars) # Structure of the dataset

3. Data Frame Operations


# Subset and modify data
mtcars_subset <- mtcars[, c("mpg", "hp", "wt")]
mtcars_subset$power_to_weight <- mtcars_subset$hp /
mtcars_subset$wt
high_mpg_cars <- subset(mtcars, mpg > 20)

4. Vector Operations
# Perform vector operations
car_weights <- mtcars$wt
squared_weights <- car_weights^2
mean_weight <- mean(car_weights)
sd_weight <- sd(car_weights)

5. Matrix and Array Operations


# Create a matrix and perform operations
car_matrix <- [Link](mtcars[1:3, 1:3])
transposed_matrix <- t(car_matrix)
car_array <- array(1:27, dim = c(3, 3, 3))

6. Correlation Analysis
# Compute and visualize correlations
cor_matrix <- cor(mtcars[, c("mpg", "hp", "wt")])
library(corrplot)
corrplot(cor_matrix, method = "circle")

7. Regression Analysis
# Perform and plot regression
regression_model <- lm(mpg ~ hp + wt, data = mtcars)
summary(regression_model)
plot(mtcars$wt, mtcars$mpg, main = "Regression Plot: MPG
vs Weight")
abline(lm(mpg ~ wt, data = mtcars), col = "red")
8. Time Series Analysis
# Analyze the AirPassengers dataset
data("AirPassengers")
plot(AirPassengers, main = "AirPassengers Time Series", col =
"blue")
ts_decomposition <- decompose(AirPassengers)
plot(ts_decomposition)

9. Pie Chart Visualization


# Create a pie chart
cyl_counts <- table(mtcars$cyl)
pie(cyl_counts, labels = names(cyl_counts), main = "Cylinder
Distribution")

10. Data Visualization with ggplot2


# Scatter plot and boxplot using ggplot2
library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg, color = [Link](cyl))) +
geom_point(size = 3) +
theme_minimal() +
labs(title = "Horsepower vs MPG", x = "Horsepower", y =
"MPG", color = "Cylinders")
ggplot(mtcars, aes(x = [Link](cyl), y = mpg, fill =
[Link](cyl))) +
geom_boxplot() +
labs(title = "MPG by Cylinders", x = "Cylinders", y = "MPG") +
theme_classic()

11. Export and Load Operations


# Save and load dataset
[Link](mtcars_subset, "mtcars_subset.csv")
loaded_data <- [Link]("mtcars_subset.csv")
head(loaded_data)

12. Conclusion
This assignment demonstrates how R programming can be
used for statistical analysis, data manipulation, and
visualization. The built-in datasets, combined with powerful
libraries like ggplot2 and corrplot, provide a robust
environment for analyzing data efficiently.

Common questions

Powered by AI

The AirPassengers dataset is used in R to illustrate time series analysis by first plotting it to reveal seasonal patterns and trends over time. Decomposition of the series is performed using the decompose function, which separates the series into trend, seasonal, and random components. This reveals the underlying patterns such as consistent seasonal variations, and an overall increasing trend, essentials for understanding the dynamics influencing passenger numbers over time .

Data export and load operations in R, exemplified by saving mtcars_subset to a CSV file using write.csv and then loading it with read.csv, are crucial for managing data persistence outside the R environment. These operations enable data sharing, reproducibility of analysis, and integration with other applications. They ensure that processed or subsetted data can be easily reused or analyzed further without repeating previous steps .

Techniques for data visualization in R include creating scatter plots and boxplots using ggplot2, which is used here to visualize relationships and distributions within the mtcars dataset. For example, a scatter plot of horsepower vs. mpg shows how these variables interact visually, while a boxplot of mpg by cylinder count provides a clear picture of mpg variance across different cylinder configurations. These visualizations help in identifying patterns and outliers that might not be evident through raw data alone .

Regression analysis on the mtcars dataset involves fitting a linear model using the lm function, such as regression_model <- lm(mpg ~ hp + wt, data = mtcars), which models the relationship between miles per gallon (mpg) as the dependent variable and horsepower (hp) and weight (wt) as independent variables. The summary of this model reveals coefficients, significance levels, and diagnostic measures to understand how these variables impact mpg. Insights include how weight and horsepower collectively influence fuel efficiency .

Matrix operations in R, demonstrated by creating a car_matrix with the mtcars dataset, involve transforming data into a matrix for linear algebraic operations. Operations such as transposition, shown with t(car_matrix), enable restructuring data for analytical tasks like calculating row or column means, assessing linear dependencies, and more. These operations are significant for computational efficiency and certain statistical techniques that inherently rely on matrix algebra .

Correlation analysis in the mtcars dataset is used to assess the strength and direction of linear relationships between variables such as mpg, hp, and wt. The cor function computes a correlation matrix, and the corrplot library visualizes these correlations. This analysis identifies how strongly these variables are linearly related, which is vital for understanding variable interactions and guiding further modeling, such as in regression analyses .

R utilizes the ggplot2 library to enhance data visualization through its rich, flexible syntax for creating complex plots. In the mtcars dataset, ggplot2 is used to create a scatter plot of horsepower vs. mpg with color differentiation by cylinder count and a boxplot showing mpg distribution by cylinder. These visualizations employ aesthetic mappings and layering principles, making data patterns and differences visually discernable, facilitating in-depth data analysis .

Vector operations in R allow for straightforward computations on the mtcars dataset, such as calculating the mean and standard deviation. For instance, car_weights <- mtcars$wt creates a vector of car weights, from which mean_weight <- mean(car_weights) and sd_weight <- sd(car_weights) calculate the mean weight and standard deviation respectively. These operations simplify data summarization and provide insights into data distribution and variability .

The pie chart visualization of the mtcars dataset, representing cylinder distribution, visually illustrates the frequency proportion of cars with different cylinder counts. This assists in quickly assessing the predominance or rarity of particular configurations (e.g., 4, 6, 8 cylinder engines), aiding stakeholders in understanding market segmentation and informing decisions related to vehicle design or marketing strategies .

In the mtcars dataset, the power-to-weight ratio can be calculated by dividing the horsepower (hp) by the weight (wt) of each car, as shown by the operation mtcars_subset$power_to_weight <- mtcars_subset$hp / mtcars_subset$wt. This ratio is useful for evaluating vehicle performance, as it gives an indication of how efficiently a car uses its power relative to its weight .

You might also like