0% found this document useful (0 votes)
64 views46 pages

Descriptive Statistics with R Tools

The document provides a comprehensive guide on performing descriptive statistics using R, including importing data, creating visualizations, and calculating statistical measures. It covers various types of plots such as histograms, bar plots, box plots, and scatter plots, along with functions for calculating mean, median, mode, variance, and correlation. Additionally, it explains the coefficient of determination (R²) in the context of linear regression analysis.

Uploaded by

Shivani (Sky)
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views46 pages

Descriptive Statistics with R Tools

The document provides a comprehensive guide on performing descriptive statistics using R, including importing data, creating visualizations, and calculating statistical measures. It covers various types of plots such as histograms, bar plots, box plots, and scatter plots, along with functions for calculating mean, median, mode, variance, and correlation. Additionally, it explains the coefficient of determination (R²) in the context of linear regression analysis.

Uploaded by

Shivani (Sky)
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DESCRIPTIVE STATISTICS USING R

UNIT 4
IMPORT → VISUALIZE → DESCRIBE
- IMPORT CSV/EXCEL FILES.
- USE R TO CREATE BASIC CHARTS.
- UNDERSTAND DESCRIPTIVE STATISTICS.
INSTALLING PACKAGES AND LOADING THEM

 # Install if not already installed


 [Link]("tidyverse") # For data manipulation and visualization
 [Link]("readxl") # For Excel file import
 [Link]("dplyr") # For advanced data manipulation

 # Load libraries
 library(tidyverse)
 library(readxl)
 library(dplyr)
IMPORTING FROM EXCEL AND EXPLORING THE FILE
 library(readxl)
 CreditLimit <- read_excel(“path of the file") #make sure to use forward slashes “/”
 View(CreditLimit)
 #check the first 6 rows
 head(CreditLimit) # by default, the first 6 rows are checked
 #check the last 6 rows
 tail(CreditLimit)
 #check the summary
 summary(CreditLimit)
IMPORTING FROM EXCEL AND EXPLORING THE FILE
 #check the summary of a variable
 summary(CreditLimit$Income)
 #check dimensions
 dim(CreditLimit)
 #check the number of variables
 length(CreditLimit)
 #check the list of variables
 attributes(CreditLimit)
 #check the data type of variables
 class(CreditLimit)
IMPORTING FROM EXCEL AND EXPLORING THE FILE
 #check the internal storage of the class
 typeof(CreditLimit)
 #check number of rows
 nrow(CreditLimit)
 #check number of columns
 ncol(CreditLimit)
 # check column names
 colnames(CreditLimit)
 glimpse(data) # Tidyverse alternative to str()
 #check for missing values
 sum([Link](CreditLimit)) #counts total missing values
 colSums([Link](CreditLimit)) # missing values per column
DATA VISUALIZATION WITH R
HISTOGRAM

• A histogram is a graphical representation of the distribution of numerical data.


• It uses continuous bars (bins) to show the frequency of data within specific ranges.
• Ideal for visualizing the distribution of a continuous variable (e.g., income).
• Helps identify skewness, peaks, and gaps in the data.
HISTOGRAM

 # Base R Histogram of Income distribution


 hist(CreditLimit$Income,
 main = "Income Distribution",
 xlab = "Income ('000)",
 col = "lightblue",
 border = "black",
 breaks = 10)
 Histogram shows the frequency distribution of Income
 breaks defines the number of bins
BAR PLOT

• A bar plot (or bar chart) uses rectangular bars to represent data values.
• The height or length of each bar is proportional to the magnitude of the variable it
represents.
• Best suited for categorical data (e.g., regions, marital status).
• Helps compare counts or averages across different groups.
BAR PLOT
 # suppose we want to show the number of customers by Marital Status
 # let’s first create a frequency table counting the occurrences of each category in the Married column
 marital_count <- table(CreditLimit$Married)
 # the result is a named vector with counts for Yes and No
 # now let’s construct a bar plot
 barplot(marital_count,
 main = "Customer Count by Marital Status",
 xlab = "Marital Status",
 ylab = "Count",
 col = "skyblue",
 border = "black")
BAR PLOT – ANOTHER EXAMPLE
 # Base R Bar plot showing average Credit Limit by Region
 avg_limit <- tapply(CreditLimit$’credit Limit’, CreditLimit$Region, mean)
 # tapply() calculates the mean by region.
 barplot(avg_limit,
 main = "Average Credit Limit by Region",
 xlab = "Region",
 ylab = "Average Credit Limit",
 col = "skyblue",
 border = "black")
 Bar plots display aggregated values by category.
BOX PLOT OR BOX-AND-WHISKER PLOT

• A box plot displays the five-number summary of a dataset:


• Minimum,
• 1st quartile (Q1),
• Median (Q2),
• 3rd quartile (Q3),
• Maximum.

• It also highlights outliers and the spread of the data.


• Useful for comparing distributions between groups.
• Helps detect outliers and skewness.
BOX PLOT

 # Simple box plot for Income distribution


 boxplot(CreditLimit$Income,
 main = "Box Plot of Income",
 ylab = "Income",
 col = "lightblue",
 border = "darkblue")
BOX PLOT – ANOTHER EXAMPLE

 # suppose we want to construct a Box plot comparing credit limit by Marital Status
 boxplot(CreditLimit$’credit Limit’ ~ CreditLimit$Married, # ~ groups by Married
status
 main = "Credit Limit by Marital Status",
 xlab = "Marital Status",
 ylab = "Credit Limit",
 col = c("pink", "lightgreen"))
LINE PLOT WITH SINGLE SERIES OF DATA
 # create a vector l1<- c(7,12,28,3,41)
 l1
 # plot l1 using points plot(l1, type = “p”)
 # plot l1 using lines plot(l1, type = “l”)
 # plot l1 using both points and lines plot(l1, type = “o”)
 # plot a vector using both points and lines, with colour
 l2<- c(1,2,8,13,40)
 plot(l2, type = “o”, col=“red”)
 # plot a vector using both points and lines with colour, heading, label of the x & y axis
 l3<- c(5,2,11,7,20,15,22,17,25)
 plot(l3, type = “o”, col=“green”, main=“Line Plot”, xlab=“points”, ylab=“Frequency”)
LINE PLOT WITH MULTIPLE SERIES OF DATA
 # let variable 't' represent time
 t<-0:10
 #variable 'z' showing quantity that is decreasing in time
 z<-exp(-t/2)
 #variable 'w' that is increasing with time
 w<-0.1*exp(t/3)
 #plot t and z
 plot(t, z, type =“l”)
 #plot t and z with colour red, line width 3, label of x & y axes time and concentration
 plot(t, z, type ="l", col="red", lwd="3", xlab="Time", ylab="Concentration")
LINE PLOT WITH MULTIPLE SERIES OF DATA
 #plot t and w
 plot(t, w, type =“o”)
 #plot t and w with colour green, line width 4, label of x & y axes time and
concentration
 plot(t, w, type =“o”, col=“green”, lwd=“4”, xlab=“Time”, ylab=“Concentration”)
 # we can add a line to an existing plot using lines() function (superimposes)
 lines(t, z, col=“red”, lwd=“3”)
 #add title 'Exponential Growth and decay’
 title("Exponential Growth and decay")
LINE CHART

• A line graph uses points connected by lines to represent data trends


over time or sequential values.
• The x-axis typically represents the time or order variable, while the y-
axis shows the values.
• Ideal for visualizing trends or changes over time.
• Shows continuous data progression (e.g., customer ID vs. credit limit).
LINE CHART

 # Base R Line plot of Bank Balance vs Age


 plot(CreditLimit$Age, CreditLimit$`Bank Balance (‘000)`,
 col = “blue”,
 main = “Bank Balance vs Age”,
 xlab = “Age”,
 ylab = “Bank Balance ('000)”
 type=“o”)
SCATTER PLOT

• A scatter plot displays individual data points on a two-dimensional plane.


• It shows the relationship between two continuous variables, where:
• X-axis → Independent variable.
• Y-axis → Dependent variable.

• Used to identify correlations or patterns between two variables.


• Helps detect linear or non-linear relationships.
SCATTER PLOT
 # Base R Scatter plot of Income vs Credit Limit
 plot(CreditLimit$Income, CreditLimit$`credit Limit`,
 main = "Income vs Credit Limit",
 xlab = "Income ('000)",
 ylab = "Credit Limit",
 col = "purple",
 pch = 19) # pch defines point shape
 # Create a plot displaying all pch types
 plot(1:25, rep(1, 25), pch = 1:25, cex = 2) # Increase point size
pch Value Symbol Description
0 □ Square outline
1 ○ Circle outline
2 △ Triangle outline (up)
3 + Plus
4 × Cross
5 ◇ Diamond outline
6 △ (filled) Triangle (up, filled)
7 ▽ Triangle outline (down)
8 Asterisk
9 ◇ (filled) Diamond (filled)
10 ⬥ Solid circle with cross
11 ⬥ Star-like shape
12-25 ⬤ Varied filled and unfilled shapes
DESCRIPTIVE STATISTICS
SIMPLE EXAMPLE FIRST..

 #create a vector
 x<-c(3,7,5,13,20,23,39,23,40,23,14,12,56,23)
 #calculate mean of x
 mean(x)
 #calculate median of x
 median(x)
 #calculate mode of x
 #create a frequency table and store in a variable t1
 t1<-table(x)
 t1
 max(t1) # finds maximum value
 t1==max(t1) # finds position of the maximum value
 names(t1[t1==max(t1)]) # extracts the names corresponding to the maximum
value
 mode_x <- [Link](names(t1[t1==max(t1)]))
 mode_x
FURTHER PRACTICE..

 #create a vector
 y<-c(3,7,5,13,20,20,39,23,40,23,14,12,56,23,20)
 #calculate mean, median and mode of y
 #create a vector
 z<-c(3,7,5,13,20,20,39,23,25,25,40,23,14,12,56,23,20,25)
 #calculate mean, median and mode of z
 # similarly find mean and median for credit Limit column in CreditLimit data
MULTIMODAL DATA
 In case of multimodal data, better way is as below (and this is standardized for all
datasets)
 find_mode <- function(x) {
freq_table <- table(x)
mode <- [Link](names(freq_table[freq_table == max(freq_table)]))
return(mode)
}
 find_mode(CreditLimit$`credit Limit`)
 find_mode(x)
 # Find mode for data frame
 # first create a data frame
 df <- [Link](
x = c(1, 4, 4, 5, 6, 7, 10, 12),
y = c(2, 2, 3, 3, 4, 5, 11, 11),
z = c(8, 9, 9, 9, 10, 13, 15, 17)
)
 print(df)
 # now apply the function to calculate mode for each column of the data frame above (hint: apply family
of functions)
MEASURES OF DISPERSION
 # Range, variance, and standard deviation of Income
 [Link]<-range(CreditLimit$Income) # Min and Max
 [Link]
 [Link]<-[Link][2] – [Link][1]
 Or
 [Link] <- range(CreditLimit$Income)[2] - range(CreditLimit$Income)[1]
 Or max(CreditLimit$Income) – min(CreditLimit$Income)
 Or we could create a function for the same
 var(CreditLimit$Income) # Variance
 sd(CreditLimit$Income) # Standard deviation
 IQR(CreditLimit$Income) # Inter quartile range
COVARIANCE AND CORRELATION
 # simple example first
 u<-c(1,2,3,4,5,6,7,8,9,10)
 v<-c(10,9,8,7,6,5,4,3,2,1)
 w<-c(2,4,6,8,10,12,14,16,18,20)
 # find covariance and correlation between u and v
 cov(u,v) cor(u,v)
 # find covariance and correlation between u and w
 cov(u,w) cor(u,w)
 # calculate covariance and correlation between Income and Credit Limit
 cov(CreditLimit$Income, CreditLimit$`credit Limit`) # Covariance
 cor(CreditLimit$Income, CreditLimit$`credit Limit`) # Correlation
 Calculate covariance using Pearson method
 Calculate covariance using Spearman method
 Calculate covariance using Kendall method
COEFFICIENT OF DETERMINATION
 The Coefficient of Determination (R²) is the Square of the Coefficient of Correlation (r).
 For simple linear regression (with one independent variable), the relationship between the two
is:
 R2 = (r)2
 r is the Pearson correlation coefficient, which measures the strength and direction of the
linear relationship between two variables.
 R2 is the coefficient of determination, which indicates the proportion of variance in the
dependent variable explained by the independent variable.
 # example
 x<-c(1,3,5,10)
 y<-c(2,4,6,20)
 r_squared<-cor(x,y)^2
 r_squared
COEFFICIENT OF DETERMINATION
 # calculating R2 using linear regression model
 # same sample data
 x <- c(1, 3, 5, 10)
 y <- c(2, 4, 6, 20)
 # Create a linear model
 model <- lm(y ~ x)
 # Extract R-squared
 r_squared <- summary(model)$[Link]
 # Print R-squared
 print(r_squared)
 cat("Coefficient of Determination (R²):", round(r_squared, 4))
COEFFICIENT OF DETERMINATION (R²)
• R² measures how well the model explains the variability of the data.
• Values range from 0 to 1 (higher values indicate a better fit).
 # applying the linear regression model method to credit Limit and Income
 model <- lm(CreditLimit$`credit Limit` ~ CreditLimit$Income)
 # R-squared value
 r_squared<-summary(model)$[Link]
 cat("Coefficient of Determination (R²):", round(r_squared, 4))
 Explanation:
 R² ≈ 0.6274 means that 62.74% of the variance in the Credit Limit is explained by Income.
 The remaining 37.26% is due to other factors not included in the model.
PRACTICING USING ANOTHER FILE…
IRIS DATASET IN R
EXPORTING FROM R

 Let’s first export the dataset to a .csv file and then import it
 # write the dataset into a data object
 data<-iris
 [Link](data, "C:/Users/payal/Documents/[Link]", [Link]=FALSE)
 # check if the file has been saved at the specified location
 [Link]("C:/Users/payal/Documents/[Link]“)
IMPORTING INTO R
 Write an R script to import the [Link] file into a data frame.
 Iris <- [Link](“path to the file”)
 How do you check if the file was imported correctly?
 Check the first few rows.
 What function can you use to view the first 10 rows of the dataset?
 head(Iris,10)
 How do you check the structure of the dataset in R?
 str(Iris) OR glimpse(Iris)
 How do you verify if there are missing values in the dataset?
 sum([Link](Iris))
EXPLORING THE DATASET
 How do you find the number of rows and columns in the dataset?
 dim(Iris)
 How do you extract only the first and last 5 rows of the dataset?
 head(Iris, 5)
 tail(Iris,5)
 Write R code to display the column names of the dataset.
 colnames(Iris)
 How do you determine the data type of each column?
 sapply(Iris, class)
 Write an R command to get a summary of the dataset.
 summary(Iris)
VISUALIZATIONS USING BASE R
 Create a histogram for the [Link] column, adding colour, labels, and title.
 hist(Iris$[Link]……..)
 Generate a boxplot for [Link], adding all other elements.
 boxplot(Iris$[Link], ……)
 Create a scatter plot between [Link] and [Link], fully detailed.
 plot(Iris$[Link], Iris$[Link], ….)
 Differentiate species using different colors in the scatter plot created above.
 plot(Iris$[Link], Iris$[Link], col = Iris$Species, pch = 19)
 Create a bar plot showing the count of each species in the dataset.
 barplot(table(Iris$Species),….)
MEASURES OF CENTRAL TENDENCY
 Write R code to calculate the mean, median, and mode of [Link].
 mean(Iris$[Link])
 median(Iris$[Link])
 find_mode(Iris$[Link]) # user-defined function
 How do you handle missing values when computing the mean?
 mean(Iris$[Link], [Link]=TRUE)
 Calculate the median for the [Link] column.
 median(Iris$[Link])
 Write a function to compute the mode of any given numeric column.
 Write the find_mode function.
 How do you find the most frequently occurring value in [Link]?
 find_mode(Iris$[Link])
MEASURES OF DISPERSION

 Compute the range of the [Link] column.


 Use the range function
 Write R code to calculate the variance of [Link].
 var(Iris$[Link])
 How do you find the standard deviation of [Link]?
 sd(Iris$[Link])
 What does a higher standard deviation indicate in terms of data spread?
 Create a boxplot to visualize the spread of [Link].
 boxplot(Iris$[Link]……)
COVARIANCE AND CORRELATION
 Compute the covariance between [Link] and [Link].
 cov(Iris$[Link], Iris$[Link])
 Compute the correlation coefficient between [Link] and [Link].
 cor(Iris$[Link], Iris$[Link])
 How do you interpret a correlation coefficient close to 1, 0, and -1?
 Perfect positive, no correlation, perfect negative
 Write an R function that takes two numerical columns and returns their correlation.
 correlation_fn <- function(x, y) {
 cor(x, y)
 }
 correlation_fn(iris$[Link], iris$[Link])
COEFFICIENT OF DETERMINATION (R²)
 Write R code to perform a linear regression between [Link] (dependent variable) and
[Link] (independent variable).
 model<-lm(Iris$[Link] ~ Iris$[Link])
 Extract the R-squared value from the linear regression model.
 r_squared<-summary(model)$[Link]
 Interpret an R² value of 0.85 in terms of model fit.
 How do you check the summary statistics of the linear regression model in R?
 summary(model)
 Create a scatter plot with a regression line for [Link] vs [Link].
 plot(data$[Link], data$[Link], main = "Petal Width vs Petal Length", xlab = "Petal
Width", ylab = "Petal Length", col = "blue", pch = 19)
 abline(model, col = "red", lwd = 2) # to add reference lines to an existing plot
ASSIGNMENT

 Import ‘Alpha Advertising Ltd.’ dataset into R.


 Draw a histogram of the total sales. Give a suitable title to the histogram, label both its axes, and colour it also.
 Draw a box plot of sales from newspaper. Give a suitable title to the plot, label the axes, and color the box plot,
with border.
 Draw a line plot with sales from radio and sales from T.V. add a legend also.
 Calculate the measures of centrality of each type of advertising mode.
 Calculate the measures of dispersion of each type of advertising mode.
 Calculate correlation between sales from TV and sales from newspaper.

You might also like