//Name: - Hemant Sharma
//Roll No: - 61
Practical No: 1
Study R Studio and basis of R programming.
# A simple R program
# Assign values to variables
a <- 10
b <- 5
# Perform arithmetic operations
sum_result <- a + b
diff_result <- a - b
prod_result <- a * b
quot_result <- a / b
# Print the results
print(paste("Sum:", sum_result))
print(paste("Difference:", diff_result))
print(paste("Product:", prod_result))
print(paste("Quotient:", quot_result))
# Use a built-in function
numbers <- c(10, 20, 30, 40, 50)
mean_value <- mean(numbers)
print(paste("Mean of the numbers:", mean_value))
OUTPUT:
> # A simple R program
>
> # Assign values to variables
> a <- 10
> b <- 5
>
> # Perform arithmetic operations
> sum_result <- a + b
> diff_result <- a - b
> prod_result <- a * b
> quot_result <- a / b
>
> # Print the results
> print(paste("Sum:", sum_result))
[1] "Sum: 15"
> print(paste("Difference:", diff_result))
[1] "Difference: 5"
> print(paste("Product:", prod_result))
[1] "Product: 50"
> print(paste("Quotient:", quot_result))
[1] "Quotient: 2"
>
> # Use a built-in function
> numbers <- c(10, 20, 30, 40, 50)
> mean_value <- mean(numbers)
> print(paste("Mean of the numbers:", mean_value))
[1] "Mean of the numbers: 30"
//Name: - Hemant Sharma
//Roll No: - 61
Practical No: 2
Linear Regression Analysis Regression Analysis and Plot Interpretations using R.
# Input vectors
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131) # Heights in cm
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48) # Weights in kg
# Create a data frame
data <- [Link](x, y)
# Fit a linear regression model
relation <- lm(x ~ y)
# Print the model and summary
print(relation)
print(summary(relation))
# Predict height for a person weighing 70 kg
a <- [Link](y = 70)
result <- predict(relation, a)
print(result)
# Save regression plot to PNG
png("[Link]")
plot(y, x,
col = "red",
main = "Height and Weight Regression",
cex = 1.3,
pch = 16,
xlab = "Weight in Kg",
ylab = "Height in cm")
abline(relation, col = "black", lwd = 2)
[Link]()
OUTPUT:
> # Input vectors
> x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131) # Heights in cm
> y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48) # Weights in kg
>
> # Create a data frame
> data <- [Link](x, y)
>
> # Fit a linear regression model
> relation <- lm(x ~ y)
>
> # Print the model and summary
> print(relation)
Call:
lm(formula = x ~ y)
Coefficients:
(Intercept) y
61.380 1.415
> print(summary(relation))
Call:
lm(formula = x ~ y)
Residuals:
Min 1Q Median 3Q Max
-6.0529 -2.4833 -0.0912 1.3774 10.0562
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 61.3803 7.2653 8.448 2.94e-05 ***
y 1.4153 0.1089 12.997 1.16e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 4.712 on 8 degrees of freedom
Multiple R-squared: 0.9548, Adjusted R-squared: 0.9491
F-statistic: 168.9 on 1 and 8 DF, p-value: 1.164e-06
>
> # Predict height for a person weighing 70 kg
> a <- [Link](y = 70)
> result <- predict(relation, a)
> print(result)
160.452
>
> # Save regression plot to PNG
> png("[Link]")
>
> plot(y, x,
+ col = "blue",
+ main = "Height and Weight Regression",
+ cex = 1.3,
+ pch = 16,
+ xlab = "Height in cm",
+ ylab = "Weight in kg")
>
> abline(relation, col = "black", lwd = 2)
>
> [Link]()
null device
GRAPH:
//Name: - Hemant Sharma
//Roll No: - 61
Practical No: 3
Logistic Regression Analysis in R.
# Load the mtcars dataset
input <- mtcars[, c("am", "cyl", "hp", "wt")]
# Print the first few rows of the selected data
print(head(input))
# Fit a logistic regression model (glm) predicting 'am' using 'cyl', 'hp', and 'wt'
[Link] <- glm(formula = am ~ cyl + hp + wt, data = input, family = binomial)
# Print the summary of the model
print(summary([Link]))
OUTPUT:
> # Load the mtcars dataset
> input <- mtcars[, c("am", "cyl", "hp", "wt")]
>
> # Print the first few rows of the selected data
> print(head(input))
am cyl hp wt
Mazda RX4 1 6 110 2.620
Mazda RX4 Wag 1 6 110 2.875
Datsun 710 1 4 93 2.320
Hornet 4 Drive 0 6 110 3.215
Hornet Sportabout 0 8 175 3.440
Valiant 0 6 105 3.460
>
> # Fit a logistic regression model (glm) predicting 'am' using 'cyl', 'hp', and 'wt'
> [Link] <- glm(formula = am ~ cyl + hp + wt, data = input, family = binomial)
>
> # Print the summary of the model
> print(summary([Link]))
Call:
glm(formula = am ~ cyl + hp + wt, family = binomial, data = input)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 19.70288 8.11637 2.428 0.0152 *
cyl 0.48760 1.07162 0.455 0.6491
hp 0.03259 0.01886 1.728 0.0840 .
wt -9.14947 4.15332 -2.203 0.0276 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 43.2297 on 31 degrees of freedom
Residual deviance: 9.8415 on 28 degrees of freedom
AIC: 17.841
Number of Fisher Scoring iterations: 8
//Name: - Hemant Sharma
//Roll No: - 61
Practical No: 4
Random Forest and Parameter Tuning in R
# Load iris dataset
data(iris)
# Structure of the dataset
str(iris)
# Install packages (only run once)
# [Link]("caTools")
# [Link]("randomForest")
# Load necessary libraries
library(caTools)
library(randomForest)
# Splitting data into training and testing sets
[Link](123) # For reproducibility
split <- [Link](iris$Species, SplitRatio = 0.7)
train <- subset(iris, split == TRUE)
test <- subset(iris, split == FALSE)
# Fitting Random Forest to the training set
[Link](120)
classifier_RF <- randomForest(x = train[-5],
y = train$Species,
ntree = 500)
# Print model summary
print(classifier_RF)
# Predicting the Test set results
Y_pred <- predict(classifier_RF, newdata = test[-5])
# Confusion Matrix
confusion_matrix <- table(Predicted = Y_pred,
Actual = test$Species)
print(confusion_matrix)
# Plotting the Random Forest model
plot(classifier_RF)
# Variable Importance Plot
importance(classifier_RF)
varImpPlot(classifier_RF)
png("RF_ErrorPlot.png")
plot(classifier_RF)
[Link]()
png("RF_VariableImportance.png")
varImpPlot(classifier_RF)
[Link]()
OUTPUT:
> # Load iris dataset
> data(iris)
>
> # Structure of the dataset
> str(iris)
'[Link]': 150 obs. of 5 variables:
$ [Link]: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ [Link] : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ [Link]: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ [Link] : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
>
> # Install packages (only run once)
> # [Link]("caTools")
> # [Link]("randomForest")
>
> # Load necessary libraries
> library(caTools)
> library(randomForest)
>
> # Splitting data into training and testing sets
> [Link](123) # For reproducibility
> split <- [Link](iris$Species, SplitRatio = 0.7)
>
> train <- subset(iris, split == TRUE)
> test <- subset(iris, split == FALSE)
>
> # Fitting Random Forest to the training set
> [Link](120)
> classifier_RF <- randomForest(x = train[-5],
+ y = train$Species,
+ ntree = 500)
>
> # Print model summary
> print(classifier_RF)
Call:
randomForest(x = train[-5], y = train$Species, ntree = 500)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 2
OOB estimate of error rate: 2.86%
Confusion matrix:
setosa versicolor virginica [Link]
setosa 35 0 0 0.00000000
versicolor 0 34 1 0.02857143
virginica 0 2 33 0.05714286
>
> # Predicting the Test set results
> Y_pred <- predict(classifier_RF, newdata = test[-5])
>
> # Confusion Matrix
> confusion_matrix <- table(Predicted = Y_pred,
+ Actual = test$Species)
>
> print(confusion_matrix)
Actual
Predicted setosa versicolor virginica
setosa 15 0 0
versicolor 0 12 1
virginica 0 3 14
>
> # Plotting the Random Forest model
> plot(classifier_RF)
>
> # Variable Importance Plot
> importance(classifier_RF)
MeanDecreaseGini
[Link] 7.055030
[Link] 1.170081
[Link] 33.522055
[Link] 27.537816
> varImpPlot(classifier_RF)
>
> png("RF_ErrorPlot.png")
> plot(classifier_RF)
> [Link]()
RStudioGD
2
>
> png("RF_VariableImportance.png")
> varImpPlot(classifier_RF)
> [Link]()
RStudioGD
GRAPH:
//Name: - Hemant Sharma
//Roll No: - 61
Practical No: 5
Clustering Algorithms and Evaluation in R
data("iris")
c <- head(iris)
print(c)
x <- iris[3:4]
print(x)
print(head(x))
model <- kmeans(x, 3)
library(cluster)
# Save clustering plot
png("KMeans_Clustering.png")
clusplot(x, model$cluster, color = TRUE, shade = TRUE)
[Link]()
OUTPUT:
> data("iris")
>
> c <- head(iris)
> print(c)
[Link] [Link] [Link] [Link] Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
>
> x <- iris[3:4]
> print(x)
[Link] [Link]
1 1.4 0.2
2 1.4 0.2
3 1.3 0.2
4 1.5 0.2
5 1.4 0.2
6 1.7 0.4
7 1.4 0.3
8 1.5 0.2
9 1.4 0.2
10 1.5 0.1
11 1.5 0.2
12 1.6 0.2
13 1.4 0.1
14 1.1 0.1
15 1.2 0.2
16 1.5 0.4
17 1.3 0.4
18 1.4 0.3
19 1.7 0.3
20 1.5 0.3
21 1.7 0.2
22 1.5 0.4
23 1.0 0.2
24 1.7 0.5
25 1.9 0.2
26 1.6 0.2
27 1.6 0.4
28 1.5 0.2
29 1.4 0.2
30 1.6 0.2
31 1.6 0.2
32 1.5 0.4
33 1.5 0.1
34 1.4 0.2
35 1.5 0.2
36 1.2 0.2
37 1.3 0.2
38 1.4 0.1
39 1.3 0.2
40 1.5 0.2
41 1.3 0.3
42 1.3 0.3
43 1.3 0.2
44 1.6 0.6
45 1.9 0.4
46 1.4 0.3
47 1.6 0.2
48 1.4 0.2
49 1.5 0.2
50 1.4 0.2
51 4.7 1.4
52 4.5 1.5
53 4.9 1.5
54 4.0 1.3
55 4.6 1.5
56 4.5 1.3
57 4.7 1.6
58 3.3 1.0
59 4.6 1.3
60 3.9 1.4
61 3.5 1.0
62 4.2 1.5
63 4.0 1.0
64 4.7 1.4
65 3.6 1.3
66 4.4 1.4
67 4.5 1.5
68 4.1 1.0
69 4.5 1.5
70 3.9 1.1
71 4.8 1.8
72 4.0 1.3
73 4.9 1.5
74 4.7 1.2
75 4.3 1.3
76 4.4 1.4
77 4.8 1.4
78 5.0 1.7
79 4.5 1.5
80 3.5 1.0
81 3.8 1.1
82 3.7 1.0
83 3.9 1.2
84 5.1 1.6
85 4.5 1.5
86 4.5 1.6
87 4.7 1.5
88 4.4 1.3
89 4.1 1.3
90 4.0 1.3
91 4.4 1.2
92 4.6 1.4
93 4.0 1.2
94 3.3 1.0
95 4.2 1.3
96 4.2 1.2
97 4.2 1.3
98 4.3 1.3
99 3.0 1.1
100 4.1 1.3
101 6.0 2.5
102 5.1 1.9
103 5.9 2.1
104 5.6 1.8
105 5.8 2.2
106 6.6 2.1
107 4.5 1.7
108 6.3 1.8
109 5.8 1.8
110 6.1 2.5
111 5.1 2.0
112 5.3 1.9
113 5.5 2.1
114 5.0 2.0
115 5.1 2.4
116 5.3 2.3
117 5.5 1.8
118 6.7 2.2
119 6.9 2.3
120 5.0 1.5
121 5.7 2.3
122 4.9 2.0
123 6.7 2.0
124 4.9 1.8
125 5.7 2.1
126 6.0 1.8
127 4.8 1.8
128 4.9 1.8
129 5.6 2.1
130 5.8 1.6
131 6.1 1.9
132 6.4 2.0
133 5.6 2.2
134 5.1 1.5
135 5.6 1.4
136 6.1 2.3
137 5.6 2.4
138 5.5 1.8
139 4.8 1.8
140 5.4 2.1
141 5.6 2.4
142 5.1 2.3
143 5.1 1.9
144 5.9 2.3
145 5.7 2.5
146 5.2 2.3
147 5.0 1.9
148 5.2 2.0
149 5.4 2.3
150 5.1 1.8
> print(head(x))
[Link] [Link]
1 1.4 0.2
2 1.4 0.2
3 1.3 0.2
4 1.5 0.2
5 1.4 0.2
6 1.7 0.4
>
> model <- kmeans(x, 3)
>
> library(cluster)
>
> # Save clustering plot
> png("KMeans_Clustering.png")
>
> clusplot(x, model$cluster, color = TRUE, shade = TRUE)
>
> [Link]()
RStudioGD
>
GRAPH: