R Markdown
#Linear Regression:
# Linear regression analysis is used to predict the value of a variable based on
# the value of another variable. The variable you want to predict is called the
# dependent variable. The variable you are using to predict the other variable's
# value is called the independent variable.
data <- data.frame(
Years_Exp = c(1.1, 1.3, 1.5, 2.0, 2.2, 2.9, 3.0, 3.2, 3.2, 3.7),
Salary = c(39343.00, 46205.00, 37731.00, 43525.00,
39891.00, 56642.00, 60150.00, 54445.00, 64445.00, 57189.00)
)
plot(data$Years_Exp, data$Salary,
xlab = "Years Experienced",
ylab = "Salary",
main = "Scatter Plot of Years Experienced vs Salary")

library(caTools)
## Warning: package 'caTools' was built under R version 4.3.3
split = sample.split(data$Salary, SplitRatio = 0.7)
trainingset = subset(data, split == TRUE)
testset = subset(data, split == FALSE)
lm.r= lm(formula = Salary ~ Years_Exp,
data = trainingset)
summary(lm.r)
##
## Call:
## lm(formula = Salary ~ Years_Exp, data = trainingset)
##
## Residuals:
## 2 3 5 6 7 9 10
## 6174 -4186 -8626 1524 4089 6498 -5473
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27772 8077 3.438 0.0185 *
## Years_Exp 9430 3018 3.124 0.0261 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6652 on 5 degrees of freedom
## Multiple R-squared: 0.6612, Adjusted R-squared: 0.5935
## F-statistic: 9.759 on 1 and 5 DF, p-value: 0.02614
new_data <- data.frame(Years_Exp = c(4.0, 4.5, 5.0))
predicted_salaries <- predict(lm.r, newdata = new_data)
print(predicted_salaries)
## 1 2 3
## 65490.63 70205.40 74920.18
# Logistic Regression:
# Logistic regression estimates the probability of an event occurring, such as
# voted or didn't vote, based on a given data set of independent variables
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
library(caTools)
split <- sample.split(mtcars, SplitRatio = 0.8)
split
## [1] FALSE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
train_reg <- subset(mtcars, split == "TRUE")
test_reg <- subset(mtcars, split == "FALSE")
logistic_model <- glm(vs ~ wt + disp,
data = train_reg,
family = "binomial")
logistic_model
##
## Call: glm(formula = vs ~ wt + disp, family = "binomial", data = train_reg)
##
## Coefficients:
## (Intercept) wt disp
## 5.04943 0.68837 -0.03068
##
## Degrees of Freedom: 22 Total (i.e. Null); 20 Residual
## Null Deviance: 30.79
## Residual Deviance: 10.74 AIC: 16.74
summary(logistic_model)
##
## Call:
## glm(formula = vs ~ wt + disp, family = "binomial", data = train_reg)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 5.04943 5.15561 0.979 0.3274
## wt 0.68837 2.24602 0.306 0.7592
## disp -0.03068 0.01619 -1.895 0.0581 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 30.789 on 22 degrees of freedom
## Residual deviance: 10.744 on 20 degrees of freedom
## AIC: 16.744
##
## Number of Fisher Scoring iterations: 6
predict_reg <- predict(logistic_model,
test_reg, type = "response")
predict_reg
## Mazda RX4 Mazda RX4 Wag Hornet Sportabout Merc 450SE
## 0.874881602 0.892866506 0.025935649 0.352220848
## Merc 450SL Lincoln Continental AMC Javelin Camaro Z28
## 0.300832677 0.004831696 0.128816965 0.045487847
## Porsche 914-2
## 0.944393306