2024-02-04

Data

data <- data.frame(
  Years_Exp = c(0.5, 1.8, 1.9, 2.5, 2.6, 3.2, 4.0, 5.2, 5.5,5.7, 6.2,6,7,7.3,7.9,8,6.1,6.4,7.1,7.0),
  Salary = c(3900.00, 46225.00, 3731.00, 43525.00,
             31891.00, 56642.00, 60150.00, 54445.00,61445.00, 64445.00, 57189.00,70000.00,70889.00,73000.09,75000,.00,59000.0, 68831.9,43001.1,43033.2)
)
summary(data)
   Years_Exp         Salary     
 Min.   :0.500   Min.   :    0  
 1st Qu.:3.050   1st Qu.:43025  
 Median :5.850   Median :56916  
 Mean   :5.095   Mean   :49317  
 3rd Qu.:7.000   3rd Qu.:65542  
 Max.   :8.000   Max.   :75000  

RStudio Plot

Subset For Regression Model

#Create a subset of the dataframe pertaining to Salary
split = sample.split(data$Salary, SplitRatio = 0.7)
trainingset = subset(data, split == TRUE)
testset = subset(data, split == FALSE)
# using lm function to reate the linear regression model 
lm.r= lm(formula = Salary ~ Years_Exp,
         data = trainingset)
#Show the value that the moddel outputs like the residuals of fit to the data
summary(lm.r)
Call:
lm(formula = Salary ~ Years_Exp, data = trainingset)

Residuals:
   Min     1Q Median     3Q    Max 
-53342  -6621   7323  13214  20013 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)    39919      14959   2.669   0.0205 *
Years_Exp       1678       2787   0.602   0.5583  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 22170 on 12 degrees of freedom
Multiple R-squared:  0.02932,   Adjusted R-squared:  -0.05157 
F-statistic: 0.3625 on 1 and 12 DF,  p-value: 0.5583

Regression formula

Linear Regression: \(\begin{equation} \hat{Y}_i = \hat{\beta}_0 + \hat{\beta}_1 X_i + \hat{\epsilon}_i \end{equation})\)

Plot Training Set

# GGPLOT for the training set
ggplot() + geom_point(aes(x = trainingset$Years_Ex, 
                          y = trainingset$Salary), colour = 'red') +
  geom_line(aes(x = trainingset$Years_Ex,
                y = predict(lm.r, newdata = trainingset)), colour = 'blue') +
   
  ggtitle('SALARY vs YEARS OF EXPERIENCE[YoE] FOR TRAINING SET ') +
  xlab('[YoE]') +
  ylab('Salary')

Create New Data to test model

# create new data to see how well the model  can create predicted values for salary
new_data <- data.frame(Years_Exp = c(2.0, 3.5, 4.0,4.5,5.0,5.5,6.0,6.5,7,0))
 
# Create the predicted values for salaries from the model to new data
predicted_salaries <- predict(lm.r, newdata = new_data)
 
# Display the predicted salaries from the model 
#This allows us to see how the model can output predicted salaries with different years of experience
print(predicted_salaries)
       1        2        3        4        5        6        7        8 
43275.01 45791.83 46630.77 47469.71 48308.65 49147.59 49986.53 50825.47 
       9       10 
51664.41 39919.25 

Plot_ly Stats

fig <- plot_ly(new_data, x = ~predicted_salaries, color = "blue", type = "box")
fig
Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels