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 4 6 7 8 10
## 4889 -5153 -3282 2775 5499 -1775 -2953
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 31118 5576 5.581 0.00255 **
## Years_Exp 7844 2102 3.733 0.01353 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4713 on 5 degrees of freedom
## Multiple R-squared: 0.7359, Adjusted R-squared: 0.6831
## F-statistic: 13.93 on 1 and 5 DF, p-value: 0.01353
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
## 62495.65 66417.88 70340.12
# 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)
##
## 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] TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE 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
## 2.03395 2.54026 -0.05441
##
## Degrees of Freedom: 22 Total (i.e. Null); 20 Residual
## Null Deviance: 31.49
## Residual Deviance: 12.69 AIC: 18.69
summary(logistic_model)
##
## Call:
## glm(formula = vs ~ wt + disp, family = "binomial", data = train_reg)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 2.03395 3.02219 0.673 0.5009
## wt 2.54026 2.10430 1.207 0.2274
## disp -0.05441 0.02883 -1.887 0.0591 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 31.492 on 22 degrees of freedom
## Residual deviance: 12.692 on 20 degrees of freedom
## AIC: 18.692
##
## Number of Fisher Scoring iterations: 7
predict_reg <- predict(logistic_model,
test_reg, type = "response")
predict_reg
## Hornet 4 Drive Hornet Sportabout Merc 230 Cadillac Fleetwood
## 2.108528e-02 1.482411e-04 9.148477e-01 3.319776e-05
## Lincoln Continental Toyota Corolla Fiat X1-9 Porsche 914-2
## 9.922485e-05 9.440919e-01 9.340524e-01 7.158836e-01
## Maserati Bora
## 5.087486e-03