This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.

setwd("~/Desktop/VCU/Spring 2021 Classes/BIOS635 - SEM/Homework1 - Regression Review")
StressData <- read.csv("Stress.csv")
MultReg <- lm(Stress ~ FirmSize + YrsInPosition + Sallaryx1000 + Age, data = StressData)
summary(MultReg)
## 
## Call:
## lm(formula = Stress ~ FirmSize + YrsInPosition + Sallaryx1000 + 
##     Age, data = StressData)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -30.462 -17.109   2.376  12.886  39.515 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   -126.50532   32.28107  -3.919  0.00287 **
## FirmSize         0.17629    0.04009   4.397  0.00134 **
## YrsInPosition   -1.56295    2.01205  -0.777  0.45526   
## Sallaryx1000     1.57454    0.44567   3.533  0.00542 **
## Age              1.62929    0.62872   2.591  0.02688 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 24.03 on 10 degrees of freedom
## Multiple R-squared:  0.8424, Adjusted R-squared:  0.7794 
## F-statistic: 13.37 on 4 and 10 DF,  p-value: 0.0005064
## 7a - Least-squares regression equation##
# y = B1(x1) + B2(x2) + B3(x3) + B4(x4) + B0
# y = .18(x1) + -1.56(x2) + 1.57(x3) + 1.63(x4) - 126.51

## 7b - test of the overall model (test of R-squared) ## 
# F(4,10) = 13.37, p=.001, therefore suggesting that R-square is not equal to zero. 

## 7c - test if each slope in reg model is equal to zero 
# Firm size - There is sufficient evidence that firm size is statistically associated with stress (and the slope is thus not equal to zero), b=0.18, t=4.40, p=.001. 
# Years in position - Years in position is not a significant predictor of stress in this model (and so the slope is equal to zero), b=-1.56, t=-0.78, p=.455
# Salary - Salary is a statistically significant predictor of stress in this model (slope is not equal to zero), b=1.57, t=3.53, p=.005
# Age - Age is also a statistically significant predictor of stress in this model (slope is not equal to zero), b=1.63, t=2.59, p=.027

## 7d - Coefficient of determination = R^2 
# R^2 = .84 
HW7e <- ((.18*362) + (-1.56*12) + (1.57*62) + (1.63*50) - (126.51))
# = 98.77 
names(StressData)
## [1] "ID"            "Stress"        "FirmSize"      "YrsInPosition"
## [5] "Sallaryx1000"  "Age"
#standardize (conver to z scores) 
StressSTD <- scale(StressData$Stress)
FirmSizeSTD <- scale(StressData$FirmSize)
YrsInPositionSTD <- scale(StressData$YrsInPosition)
SalarySTD <- scale(StressData$Sallaryx1000)
AgeSTD <- scale(StressData$Age)
mean(StressSTD)
## [1] -8.510897e-17
sd(StressSTD)
## [1] 1
mean(FirmSizeSTD)
## [1] -7.586343e-17
sd(FirmSizeSTD)
## [1] 1
mean(YrsInPositionSTD)
## [1] -1.795154e-16
sd(YrsInPositionSTD)
## [1] 1
mean(SalarySTD)
## [1] -7.584175e-17
sd(SalarySTD)
## [1] 1
mean(AgeSTD)
## [1] 1.618858e-16
sd(AgeSTD)
## [1] 1
#Add standardized variables to dataset
StressData$StressSTD <- StressSTD
StressData$FirmSizeSTD <- FirmSizeSTD
StressData$YrsInPositionSTD <- YrsInPositionSTD
StressData$SalarySTD <- SalarySTD
StressData$AgeSTD <- AgeSTD
names(StressData)
##  [1] "ID"               "Stress"           "FirmSize"         "YrsInPosition"   
##  [5] "Sallaryx1000"     "Age"              "StressSTD"        "FirmSizeSTD"     
##  [9] "YrsInPositionSTD" "SalarySTD"        "AgeSTD"
#standardized multiple regression
MultRegSTD <- lm(StressSTD ~ FirmSizeSTD + YrsInPositionSTD + SalarySTD + AgeSTD, data=StressData)
summary(MultRegSTD)
## 
## Call:
## lm(formula = StressSTD ~ FirmSizeSTD + YrsInPositionSTD + SalarySTD + 
##     AgeSTD, data = StressData)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.59539 -0.33440  0.04644  0.25186  0.77233 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)   
## (Intercept)       3.999e-17  1.213e-01   0.000  1.00000   
## FirmSizeSTD       6.461e-01  1.469e-01   4.397  0.00134 **
## YrsInPositionSTD -1.267e-01  1.631e-01  -0.777  0.45526   
## SalarySTD         5.153e-01  1.459e-01   3.533  0.00542 **
## AgeSTD            3.486e-01  1.345e-01   2.591  0.02688 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4697 on 10 degrees of freedom
## Multiple R-squared:  0.8424, Adjusted R-squared:  0.7794 
## F-statistic: 13.37 on 4 and 10 DF,  p-value: 0.0005064
## 7f - strongest effect on stress? 
# FirmSize - coefficient = .65, which is the largest (vs. -.13, .52, .35)
## 7g - model assumptions met? 

MultReg$residuals
##          1          2          3          4          5          6          7 
##  -1.349647  23.913331   2.376005 -27.961400   9.523829   6.233118  -1.723162 
##          8          9         10         11         12         13         14 
##  39.515340 -12.343217  16.612778 -21.874690   5.578898 -24.287246 -30.462244 
##         15 
##  16.248309
qqnorm(MultReg$residuals)
qqline(MultReg$residuals)

plot(MultReg$fitted.values, MultReg$residuals)

plot(MultReg)

# Linearity - linearity met - based on residuals v. fitted plot & qq plot 
# Normality - approximately normal (based on qqplot); 
# Homoscedasticity (via residuals v. fitted values plot) - potential heteroscedasticity due to increase as Y increased
# Independence of Residuals - assumption met based on residuals plots 
# Potential outliers were examined via residuals v. leverage plot.  No values were beyond 3-SDs; no high leverage points (< .67)

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.