0% found this document useful (0 votes)
23 views22 pages

SML Practicals All

Uploaded by

dhruvck2007
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)
23 views22 pages

SML Practicals All

Uploaded by

dhruvck2007
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
You are on page 1/ 22

SML

Practical No 2
Write Program to-
i. Demonstrate use of the R-Number
>a = 12
> class(a)
[1] "numeric"
> b = 12L
> class(b)
[1] "integer"
> c = 2+3i
> class(c)
[1] "complex"

ii. Convert Number From One Type to Other using function


> x = 12L
> class(x)
[1] "integer"
> as.complex(x)
[1] 12+0i
> class(x)
[1] "integer"
>x
[1] 12
SML

Perform Following Operations:


i. Addition and Subtraction on numbers.
>a=2
>b=3
>c=a+b
>d=a-b
> print(c)
[1] 5
> print(d)
[1] -1

ii. Find Square root using of number using built-in function


> q = 12
> p = sqrt(q)
> print(p)
[1] 3.464102

Practical No 3

Write Program to-


a. Print any Built-in data set of R.

# Load the mtcars dataset


data(mtcars)

b. Get information about the data set.


SML
# Get information about the mtcars dataset
help(mtcars)

c. Find the dimensions of the data set and view the names of the variables.
Hint: Use dim( ) and names( ) function.
# Find the dimensions of the mtcars dataset
dim(mtcars)

# View the names of the variables in the mtcars dataset


names(mtcars)

d. Find the name of each row in the first column.


Hint: Use the rownames( ) function.

# Find the name of each row in the first column of the mtcars dataset
rownames(mtcars)

e. Print all values that belong to a variable.

# Print all values that belong to the mpg variable


mtcars$mpg

f. Sort the values of variable.

# Sort the values of the mpg variable in ascending order


sort(mtcars$mpg)

g. Get the statistical summary of the data.

# Get the statistical summary of the mtcars dataset


summary(mtcars)
SML

Practical No 4
Write a program to-
a. Find the lowest or highest value in a data set.
Hint: Use min( ) and max( ) functions.

# Load the mtcars dataset


data(mtcars)

# Find the lowest value in the mpg column


min(mtcars$mpg)

# Find the highest value in the mpg column


max(mtcars$mpg)

b. Find the index position of the max and min value in the table.
Hint: use which.max( ) and which.min( ) functions.

# Find the index position of the max value in the mpg column
which.max(mtcars$mpg)

# Find the index position of the min value in the mpg column
which.min(mtcars$mpg)

Practical No 5
Write programs to calculate Measures of Central tendency.
a. Import data into R.
SML
# Load the mtcars dataset
data(mtcars)

b. Calculate the Mean (Average value) of a variable from the given data
set.

# Calculate the mean of the mpg variable


mean(mtcars$mpg)

c. Find the Median (Mid-Point value) of the variable from the given data
set.

# Calculate the median of the mpg variable


median(mtcars$mpg)

d. Calculate the mode for the variable from the given data set.( by sorting
the column of the dataframe and by using the ‘modest’ package).

# Install and load the modest package


install.packages("modest")
library(modest)

# Calculate the mode of the mpg variable


mode(mtcars$mpg)

# Sort the mpg column in ascending order


sorted_mpg <- sort(mtcars$mpg)

# Find the mode of the mpg variable


mode_mpg <-
names(which(table(sorted_mpg)==max(table(sorted_mpg))))
mode_mpg
SML

e. Calculate the Percentile of the variable from the given data set.

# Calculate the 25th percentile of the mpg variable


quantile(mtcars$mpg, 0.25)

# Calculate the 50th percentile of the mpg variable


quantile(mtcars$mpg, 0.5)

# Calculate the 75th percentile of the mpg variable


quantile(mtcars$mpg, 0.75)

Practical No 6
Write programs to-
a. Print Original Data Frame, Modified Frequency Table, Cumulative
Frequency Table, Relative Frequency Table.

# Load the mtcars dataset


data(mtcars)

# Print the original data frame


print(mtcars)

# Create a frequency table for the gear variable


freq_table <- table(mtcars$gear)

# Print the frequency table


print(freq_table)
SML
# Create a modified frequency table
modified_freq_table <- data.frame(Frequency = freq_table,
Variable = names(freq_table))

# Print the modified frequency table


print(modified_freq_table)

# Create a cumulative frequency table


cumulative_freq_table <- data.frame(Frequency = cumsum(freq_table),
Variable = names(freq_table))

# Print the cumulative frequency table


print(cumulative_freq_table)

# Create a relative frequency table


relative_freq_table <- data.frame(Relative_Frequency = freq_table /
sum(freq_table),
Variable = names(freq_table))

# Print the relative frequency table


print(relative_freq_table)

b. Create the Frequency Table by using multiple arguments.

# Create a frequency table for the gear and cyl variables


freq_table_multiple_args <- table(mtcars$gear, mtcars$cyl)

# Print the frequency table


print(freq_table_multiple_args)

c. Plot the frequency table using ggplot function.


SML
# Install and load the ggplot2 package
install.packages("ggplot2")
library(ggplot2)

# Create a data frame for the frequency table


freq_table_df <- data.frame(Frequency = freq_table,
Variable = names(freq_table))

# Plot the frequency table using ggplot


ggplot(freq_table_df, aes(x = Variable, y = Frequency)) +
geom_bar(stat = "identity") + labs(x = "Gear", y = "Frequency") +
theme_classic()

Practical No 7
Write programs to calculate-Variance, Standard Deviation, Range, Mean
Deviation for the given data.
# Load the mtcars dataset
data(mtcars)

# Calculate the variance of the mpg variable


variance_mpg <- var(mtcars$mpg)
print(paste("Variance of mpg:", variance_mpg))

# Calculate the standard deviation of the mpg variable


std_dev_mpg <- sd(mtcars$mpg)
SML
print(paste("Standard Deviation of mpg:", std_dev_mpg))

# Calculate the range of the mpg variable


range_mpg <- max(mtcars$mpg) - min(mtcars$mpg)
print(paste("Range of mpg:", range_mpg))
# Calculate the mean deviation of the mpg variable
mean_mpg <- mean(mtcars$mpg)
mean_deviation_mpg <- sum(abs(mtcars$mpg - mean_mpg)) /
length(mtcars$mpg)
print(paste("Mean Deviation of mpg:", mean_deviation_mpg))

Practical No 8
Write Programs to graphically represent mode and median of the given data.
a. Draw Histogram for the given data.

# Load the mtcars dataset


data(mtcars)

# Draw a histogram for the mpg variable


hist(mtcars$mpg,
main = "Histogram of MPG",
xlab = "MPG",
ylab = "Frequency",
col = "lightblue",
border = "black")
SML

b. Draw Ogive Curve for the given data.

# Load the mtcars dataset


data(mtcars)

# Sort the mpg variable in ascending order


sorted_mpg <- sort(mtcars$mpg)

# Calculate the cumulative frequency


cumulative_frequency <- cumsum(table(sorted_mpg))

# Draw an ogive curve for the mpg variable


plot(c(0, sorted_mpg),
c(0, cumulative_frequency),
main = "Ogive Curve of MPG",
xlab = "MPG",
ylab = "Cumulative Frequency",
type = "s")

Practical No 9
Write a Program to calculate Skewness for the given data.
# Load the mtcars dataset
data(mtcars)
SML
# Calculate the skewness of the mpg variable
skewness_mpg <- (sum((mtcars$mpg - mean(mtcars$mpg))^3) /
length(mtcars$mpg)) / (sd(mtcars$mpg))^3
print(paste("Skewness of mpg:", skewness_mpg))

# Load the e1071 package


library(e1071)

# Calculate the skewness of the mpg variable


skewness_mpg <- skewness(mtcars$mpg)
print(paste("Skewness of mpg:", skewness_mpg))

Practical No 10
Write a Program to draw a scatterplot for two variables for the given dataset.
# Load the mtcars dataset
data(mtcars)

# Draw a scatterplot for the mpg and wt variables


plot(mtcars$mpg,
mtcars$wt,
main = "Scatterplot of MPG vs WT",
xlab = "MPG",
ylab = "WT",
SML
pch = 19,
col = "blue")
Practical No 11
Write Program to perform the correlation test to evaluate the association
between two or more variables.
a. Install and load required R packages.

# Install the required packages


install.packages("Hmisc")
install.packages("ggplot2")

# Load the required packages


library(Hmisc)
library(ggplot2)

b. Compute correlation in R.

# Load the mtcars dataset


data(mtcars)

# Compute the correlation between mpg and wt


correlation_mpg_wt <- rcorr(mtcars$mpg, mtcars$wt)
print(correlation_mpg_wt)

c. Visualize your data using scatter plots.

# Create a scatter plot of mpg vs wt


SML
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
labs(title = "Scatterplot of MPG vs WT", x = "WT", y = "MPG")

d. Perform Preliminary test to check the test assumptions.

# Perform the Shapiro-Wilk test for normality


shapiro_test_mpg <- shapiro.test(mtcars$mpg)
print(shapiro_test_mpg)

shapiro_test_wt <- shapiro.test(mtcars$wt)


print(shapiro_test_wt)

Practical No 12

Write Program to perform the correlation test to evaluate the association


between two or more variables.
a. Pearson correlation test.

# Load the mtcars dataset


data(mtcars)

# Perform the Pearson correlation test


correlation_mpg_wt <- cor.test(mtcars$mpg, mtcars$wt)
print(correlation_mpg_wt)

c. Use Spearman rank correlation coefficient to estimate a rank-based


measure of association.
SML

# Perform the Spearman rank correlation test


correlation_mpg_wt_spearman <- cor.test(mtcars$mpg, mtcars$wt,
method = "spearman")
print(correlation_mpg_wt_spearman)
Practical No 13
Write a Program based on Line of Regression using two variables.
# Load the mtcars dataset
data(mtcars)

# Perform the linear regression analysis


linear_regression_mpg_wt <- lm(mpg ~ wt, data = mtcars)
print(summary(linear_regression_mpg_wt))

# Create a scatter plot of mpg vs wt with a regression line


plot(mtcars$wt, mtcars$mpg,
main = "Scatterplot of MPG vs WT with Regression Line",
xlab = "WT",
ylab = "MPG")

abline(linear_regression_mpg_wt)
SML
Practical No 14

Write Programs to-


a. Calculate the probability of getting heads when flipping a fair coin.

# The probability of getting heads is 1/2, since the coin is fair


probability_heads <- 1/2

# Print the result


cat("The probability of getting heads is:", probability_heads)

b. Calculate the probability of drawing a spade from a standard deck of 52


cards.

# The number of spades in a standard deck of 52 cards is 13

num_spades <- 13

# The total number of cards in the deck is 52

total_cards <- 52

# Calculate the probability of drawing a spade

probability_spade <- num_spades / total_cards

# Print the result


cat("The probability of drawing a spade is:", probability_spade)

Practical No 18

Write a program to use Bayes’ Theorem in R-Programming.

bayesTheorem <- function(pA, pB, pBA) { pAB <- pA * pBA / pB return(pAB )}

# Define probabilities
pRain <- 0.20
SML
pCloudy <- 0.40
pCloudyRain <- 0.85

# Use function to calculate conditional probability


bayesTheorem(pRain, pCloudy, pCloudyRain)

Practical No 19

Write a Program to interpolate using newton forward interpolation.

# Define the function for Newton's forward interpolation


newton_forward_interpolation <- function(x, y, x_new) {
n <- length(x)
delta_y <- matrix(nrow = n, ncol = n)
delta_y[, 1] <- y

# Calculate the differences


for (i in 2:n) {
for (j in 1:(n - i + 1)) {
delta_y[j, i] <- delta_y[j + 1, i - 1] - delta_y[j, i - 1]
}
}

# Calculate the interpolated value


y_new <- y[1]
for (i in 1:(n - 1)) {
product <- 1
for (j in 1:i) {
product <- product * (x_new - x[j])
}
y_new <- y_new + (delta_y[1, i + 1] / factorial(i)) * product
}

return(y_new)
}
SML
# Define the x and y values
x <- c(0, 1, 2, 3, 4)
y <- c(1, 2, 5, 10, 17)

# Define the new x value for interpolation


x_new <- 2.5

# Perform the interpolation


y_new <- newton_forward_interpolation(x, y, x_new)

# Print the result


cat("The interpolated value at x =", x_new, "is:", y_new)

Practical No 20

Write a Program to interpolate using newton backward interpolation.

# Define the function for Newton's backward interpolation


newton_backward_interpolation <- function(x, y, x_new) {n <- length(x)
delta_y <- matrix(nrow = n, ncol = n)
delta_y[, 1] <- y

# Calculate the differences


for (i in 2:n) {
for (j in 1:(n - i + 1)) {
delta_y[j, i] <- delta_y[j + 1, i - 1] - delta_y[j, i - 1]
}
}

# Calculate the interpolated value


y_new <- y[n]
h <- x[2] - x[1]
p <- (x_new - x[n]) / h

for (i in 1:(n - 1)) {


product <- 1
for (j in 1:i) {
SML
product <- product * (p + j)}
y_new <- y_new + ((-1)^i) * (delta_y[n - i, i + 1] / factorial(i)) * product
}

return(y_new)
}
# Define the x and y values
x <- c(0, 1, 2, 3, 4)
y <- c(1, 2, 5, 10, 17)

# Define the new x value for interpolation


x_new <- 2.5

# Perform the interpolation


y_new <- newton_backward_interpolation(x, y, x_new)

# Print the result


cat("The interpolated value at x =", x_new, "is:", y_new)

Practical No 21

Write a program for the implementation of extrapolation.

# Define the function for Newton's forward interpolation


newton_forward_interpolation <- function(x, y, x_new) {
n <- length(x)
delta_y <- matrix(nrow = n, ncol = n)
delta_y[, 1] <- y

# Calculate the differences


for (i in 2:n) {
for (j in 1:(n - i + 1)) {
delta_y[j, i] <- delta_y[j + 1, i - 1] - delta_y[j, i - 1]
}
}
SML
# Calculate the extrapolated value
y_new <- y[1]
for (i in 1:(n - 1)) {
product <- 1
for (j in 1:i) {
product <- product * (x_new - x[j])
}
y_new <- y_new + (delta_y[1, i + 1] / factorial(i)) * product
}

return(y_new)
}

# Define the x and y values


x <- c(0, 1, 2, 3, 4)
y <- c(1, 2, 5, 10, 17)

# Define the new x value for extrapolation


x_new <- 5.5

# Perform the extrapolation


y_new <- newton_forward_interpolation(x, y, x_new)

# Print the result


cat("The extrapolated value at x =", x_new, "is:", y_new)

Practical No 24

Write a program to generate a Sampling Distribution proportion.

# Set the seed for reproducibility


set.seed(123)

# Define the population proportion


p <- 0.4
SML
# Define the sample size
n <- 20

# Define the number of samples


num_samples <- 1000

# Create a vector to store the sample proportions


sample_proportions <- rep(NA, num_samples)

# Generate the sampling distribution of proportions


for (i in 1:num_samples) {
# Generate a random sample from the population
sample <- rbinom(n, 1, p)

# Calculate the sample proportion


sample_proportion <- sum(sample) / n

# Store the sample proportion


sample_proportions[i] <- sample_proportion
}

# Calculate the mean and standard deviation of the sampling distribution


mean_sampling_distribution <- mean(sample_proportions)
sd_sampling_distribution <- sd(sample_proportions)

# Print the results


cat("Mean of the sampling distribution:", mean_sampling_distribution,
"\n")
cat("Standard deviation of the sampling distribution:",
sd_sampling_distribution, "\n")

# Plot the sampling distribution


hist(sample_proportions,
main = "Sampling Distribution of Proportions",
xlab = "Sample Proportion",
ylab = "Frequency",
col = "skyblue",
SML
border = "black")

Practical No 25

Write a program based on t-Distribution using dt, pt, qt & rt functions.

# Define the degrees of freedom


df <- 10

# Define the x values for the density and distribution functions


x <- seq(-3, 3, by = 0.1)

# Calculate the density values using dt()


density_values <- dt(x, df)
plot(x, density_values, type = "l", main = "t-Distribution Density", xlab =
"x", ylab = "Density")

# Calculate the cumulative distribution values using pt()


cumulative_distribution_values <- pt(x, df)
plot(x, cumulative_distribution_values, type = "l", main = "t-Distribution
Cumulative Distribution", xlab = "x", ylab = "Cumulative Probability")

# Calculate the quantile values using qt()


quantile_values <- qt(c(0.25, 0.5, 0.75), df)
cat("Quantile values:", quantile_values, "\n")

# Generate random values from the t-distribution using rt()


random_values <- rt(10, df)
cat("Random values from the t-distribution:", random_values, "\n")

Practical No 26

Write a program based on Chi-Square Distribution using dchisq, pchisq,


qchisq & rchisq functions.

# Define the degrees of freedom


df <- 5
SML
# Define the x values for the density and distribution functions
x <- seq(0, 20, by = 0.1)

# Calculate the density values using dchisq()


density_values <- dchisq(x, df)
plot(x, density_values, type = "l", main = "Chi-Square Distribution Density",
xlab = "x", ylab = "Density")

# Calculate the cumulative distribution values using pchisq()


cumulative_distribution_values <- pchisq(x, df)
plot(x, cumulative_distribution_values, type = "l", main = "Chi-Square
Distribution Cumulative Distribution", xlab = "x", ylab = "Cumulative
Probability")

# Calculate the quantile values using qchisq()


quantile_values <- qchisq(c(0.25, 0.5, 0.75), df)
cat("Quantile values:", quantile_values, "\n")

# Generate random values from the chi-square distribution using rchisq()


random_values <- rchisq(10, df)
cat("Random values from the chi-square distribution:", random_values,
"\n")

You might also like