Skip to content

Commit fd79dde

Browse files
author
emu
committed
Code of all the analysis
0 parents  commit fd79dde

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

data_visualization.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library(ggplot2)
2+
3+
ggplot(data = mtcars, aes(x = am)) +
4+
geom_bar() +
5+
ggtitle("Car count based on Transmission Type [0 = manual, 1 = auto]") +
6+
xlab("Transmission Type") +
7+
ylab("Total cars")
8+
9+
10+
ggplot(data = mtcars, aes(x = qsec)) +
11+
geom_histogram(bins = 10) +
12+
ggtitle("Distribution of Fuel") +
13+
xlab("Fuel per Milage") +
14+
ylab("Total cars")
15+
16+
ggplot(data = mtcars, aes(x = qsec)) +
17+
geom_density() +
18+
ggtitle("Distribution of Fuel") +
19+
xlab("Fuel per Milage") +
20+
ylab("Total cars")
21+
22+
ggplot(data = mtcars, aes(x = cyl, y = qsec)) +
23+
geom_point() +
24+
ggtitle("Distribution of Fuel") +
25+
xlab("cylinders") +
26+
ylab("Total milage")
27+
28+
29+

descriptive_statistics.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
mtcars
2+
3+
# find number of cars based on Automatic/Manual gear
4+
table(mtcars$am)
5+
6+
# minimum qsec value
7+
min(mtcars$qsec)
8+
9+
# maximum qsec value
10+
max(mtcars$qsec)
11+
12+
# mean qsec value
13+
mean(mtcars$qsec)
14+
15+
# median qsec value
16+
median(mtcars$qsec)
17+
18+
# quartile qsec value
19+
quantile(mtcars$qsec)
20+
21+
# sum of qsec value
22+
sum(mtcars$qsec)
23+
24+
# correclation coefficients between cyl and qsec
25+
cor(
26+
x = mtcars$cyl,
27+
y = mtcars$qsec
28+
)
29+
30+
# summary(mtcars)

intro_demo.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
gender <- c('Male', 'Female', 'Female', 'Male', 'Female', 'Male')
2+
gender <- factor(gender)
3+
unclass(gender)
4+
5+
genders <- c('Male', 'Female')
6+
people <- c('Kabir', 'Laif', 'Moin')
7+
df <- data.frame(colnames(genders), people)
8+
df

predict_with_ml.R

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
library(tree)
2+
library(ggplot2)
3+
library(RColorBrewer)
4+
library(caret)
5+
6+
data("iris")
7+
8+
# set a seed to make randomness reproducable
9+
set.seed(42)
10+
11+
# get 100 random indexes between 1 to 150
12+
indexes <- sample(
13+
x = 1:150,
14+
size = 100
15+
)
16+
17+
print(indexes)
18+
19+
# based on the indexes split iris data to TRAIN & TEST
20+
train <- iris[indexes, ]
21+
test <- iris[-indexes, ]
22+
23+
# create a decision trre model based on train data
24+
model <- tree(
25+
data = train,
26+
formula = Species ~ .
27+
)
28+
29+
# get summary of the created model
30+
summary(model)
31+
32+
# plot tree with text
33+
plot(model)
34+
text(model)
35+
36+
# create a color palatte
37+
palatte <- brewer.pal(3, "Set2")
38+
39+
# create a scatterplot with colored species
40+
plot(
41+
x = iris$Petal.Length,
42+
y = iris$Petal.Width,
43+
pch = 19,
44+
col = palatte[as.numeric(iris$Species)],
45+
main = "IRIS petal Length vs Width",
46+
xlab = "Petal Length",
47+
ylab = "Petal Width"
48+
)
49+
50+
# partition based on species
51+
partition.tree(
52+
tree = model,
53+
label = "Species",
54+
add = TRUE
55+
)
56+
57+
# Predict test data
58+
predictions <- predict(
59+
object = model,
60+
newdata = test,
61+
type = "class"
62+
)
63+
64+
# create a confussion matrix
65+
table(
66+
x = predictions,
67+
y = test$Species
68+
)
69+
70+
# evaluate the predicting result
71+
confusionMatrix(
72+
data = predictions,
73+
reference = test$Species
74+
)

statistical_models.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
library(ggplot2)
2+
3+
data("iris")
4+
5+
head(iris)
6+
7+
# draw a scatterplot based on petal width
8+
petal_plot <- ggplot(data = iris, aes(x = Petal.Width, y = Petal.Length)) +
9+
geom_point() +
10+
ggtitle("Scatterplot of petal width & length") +
11+
xlab("petal width") +
12+
ylab("petal length")
13+
14+
# create a linear regression model
15+
linear_model <- lm(
16+
formula = Petal.Width ~ Petal.Length,
17+
data = iris
18+
)
19+
20+
# summary(linear_model)
21+
22+
# draw linear regression line
23+
petal_plot + stat_smooth(method = "lm", col = "red")
24+
25+
# getting correlation coefficients
26+
cor(
27+
x = iris$Petal.Width,
28+
y = iris$Petal.Length
29+
)
30+
31+
# predict new values from the model
32+
predict(
33+
object = linear_model,
34+
newdata = data.frame(Petal.Length = c(2,5,7))
35+
)

0 commit comments

Comments
 (0)