#1.1 Data

Source:https: //www.kaggle.com/code/codebreaker619/predicting-profits-with-multiple-linear-regression

**A study was made on how R&D spending, marketing spending and administrative spendind affect annual profit of U.S. startups. For this purpose, data was collected for 50 startups from three states: New York, California, and Florida.

How do these variables affect annual profit of U.S. startups?**

Strtup <- read.table("/cloud/project/Quiz avg vs Final/Strtup.csv", header = TRUE, sep = ",", dec = ".", encoding = "UTF-8")

Strtup$StateF <- factor(Strtup$State, levels = c(1, 2, 3), labels = c("New York", "California", "Florida"))

head(Strtup)
##   ID Profit  RandD Administration Marketing State     StateF
## 1  1 192.26 165.35         136.90    471.78     1   New York
## 2  2 191.79 162.60         151.38    443.90     2 California
## 3  3 191.05 153.44         101.15    407.93     3    Florida
## 4  4 182.90 144.37         118.67    383.20     1   New York
## 5  5 166.19 142.11          91.39    366.17     3    Florida
## 6  6 156.99 131.88          99.81    362.86     1   New York

Description:

Expectations: increase in R&D, Administration and Marketing spending should have a positive effect on the profit, while we are not expecting a major differences in profit among the states.

library(pastecs)
round(stat.desc(Strtup[-1]), 2)
##               Profit   RandD Administration Marketing State StateF
## nbr.val        50.00   50.00          50.00     50.00 50.00     NA
## nbr.null        0.00    2.00           0.00      3.00  0.00     NA
## nbr.na          0.00    0.00           0.00      0.00  0.00     NA
## min            14.68    0.00          51.28      0.00  1.00     NA
## max           192.26  165.35         182.65    471.78  3.00     NA
## range         177.58  165.35         131.37    471.78  2.00     NA
## sum          5600.61 3686.05        6067.24  10551.23 99.00     NA
## median        107.97   73.05         122.70    212.72  2.00     NA
## mean          112.01   73.72         121.34    211.02  1.98     NA
## SE.mean         5.70    6.49           3.96     17.29  0.12     NA
## CI.mean.0.95   11.45   13.05           7.96     34.75  0.23     NA
## var          1624.57 2107.01         785.03  14954.87  0.67     NA
## std.dev        40.31   45.90          28.02    122.29  0.82     NA
## coef.var        0.36    0.62           0.23      0.58  0.41     NA

#1.2 Analysis

library(car)
## Loading required package: carData
scatterplotMatrix(Strtup[ , c(-1, -6, -7, -8, -9, -10)], smooth = FALSE)

library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
rcorr(as.matrix(Strtup[ , c(-1, -6, -7)]))
##                Profit RandD Administration Marketing
## Profit           1.00  0.97           0.20      0.75
## RandD            0.97  1.00           0.24      0.72
## Administration   0.20  0.24           1.00     -0.03
## Marketing        0.75  0.72          -0.03      1.00
## 
## n= 50 
## 
## 
## P
##                Profit RandD  Administration Marketing
## Profit                0.0000 0.1622         0.0000   
## RandD          0.0000        0.0905         0.0000   
## Administration 0.1622 0.0905                0.8247   
## Marketing      0.0000 0.0000 0.8247

#We observe that the highest varience is between Profit and RandD (0.97), while we also observe that all p values for Administration are statistically insignificant (>0.05).

fit1 <- lm (Profit ~ RandD + Administration + Marketing + StateF, data = Strtup)

vif(fit1)
##                    GVIF Df GVIF^(1/(2*Df))
## RandD          2.495540  1        1.579728
## Administration 1.177781  1        1.085256
## Marketing      2.416797  1        1.554605
## StateF         1.062682  2        1.015315

#We observe that VIF statistics is too high for RandD and Marketing, implicating too storng multicolinearity. Since we introduced State through 2 dummy variables, VIF statistics needs to be lower than 2.25. We need to remove one of the two and we decide to remove variable RandD.

#We run fit again but without variable RandD.

fit2 <- lm (Profit ~ Administration + Marketing + StateF, data = Strtup)

vif(fit2)
##                    GVIF Df GVIF^(1/(2*Df))
## Administration 1.001556  1        1.000778
## Marketing      1.052166  1        1.025751
## StateF         1.051343  2        1.012596

#This time, we observe that VIF statistics for all of the variables is low enough, so we can proceed with the analysis.

#Next we need to check, whether errors are normally distributed.

Strtup$StdResid <- round(rstandard(fit2), 3)

hist(Strtup$StdResid, xlab = "Standardized Residuals", ylab = "Frequency")

shapiro.test(Strtup$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  Strtup$StdResid
## W = 0.97163, p-value = 0.2692

#Based on the Shapiro-Wilk test of normality, we observe that the distribution of errors is normal, however we observe that one unit is outside of the desired interval (between -3 and 3), so we will have to remove that unit.

#Before removing the outlier, we will perform the Cooks distance test to check for units with high impact.

Strtup$CooksD <- round(cooks.distance(fit2), 3)

hist(Strtup$CooksD, xlab = "Cooks Distance", ylab = "Frequency")

#We observe a gap betweeen 2 units and the rest of them, so we will have to remove them, because they are units with high impact.

head(Strtup[order(Strtup$StdResid),], 6)
##    ID Profit RandD Administration Marketing State     StateF StdResid CooksD
## 47 47  49.49  1.32         115.82    297.11     3    Florida   -3.159  0.143
## 50 50  14.68  0.00         116.98     45.17     2 California   -2.142  0.087
## 28 28 105.01 72.11         127.86    353.18     1   New York   -1.919  0.074
## 22 22 111.31 78.39         153.77    299.74     1   New York   -1.473  0.048
## 35 35  96.71 46.43         157.69    210.80     2 California   -1.055  0.024
## 23 23 110.35 73.99         122.78    303.32     3    Florida   -0.902  0.012

#Based on standardized residuals we remove ID=47

head(Strtup[order(-Strtup$CooksD),], 6)
##    ID Profit  RandD Administration Marketing State     StateF StdResid CooksD
## 20 20 122.78  86.42         153.51      0.00     1   New York    2.071  0.143
## 47 47  49.49   1.32         115.82    297.11     3    Florida   -3.159  0.143
## 7   7 156.12 134.62         147.20    127.72     2 California    2.288  0.092
## 50 50  14.68   0.00         116.98     45.17     2 California   -2.142  0.087
## 28 28 105.01  72.11         127.86    353.18     1   New York   -1.919  0.074
## 3   3 191.05 153.44         101.15    407.93     3    Florida    1.567  0.060

#Based on cooks distances we remove ID=20 and ID=47. ID=20 is a unit with high impact, while ID=47 is an outlier and also a unit with high impact. We remove both.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
## 
##     src, summarize
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:pastecs':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
Strtup <- Strtup %>% filter(!ID == "20")
Strtup <- Strtup %>% filter(!ID == "47")

round(stat.desc(Strtup[-1]), 2)
##               Profit   RandD Administration Marketing State StateF StdResid
## nbr.val        48.00   48.00          48.00     48.00 48.00     NA    48.00
## nbr.null        0.00    2.00           0.00      2.00  0.00     NA     0.00
## nbr.na          0.00    0.00           0.00      0.00  0.00     NA     0.00
## min            14.68    0.00          51.28      0.00  1.00     NA    -2.14
## max           192.26  165.35         182.65    471.78  3.00     NA     2.29
## range         177.58  165.35         131.37    471.78  2.00     NA     4.43
## sum          5428.34 3598.31        5797.91  10254.12 95.00     NA     1.21
## median        107.97   73.05         122.70    212.72  2.00     NA     0.06
## mean          113.09   74.96         120.79    213.63  1.98     NA     0.03
## SE.mean         5.79    6.58           4.07     17.37  0.12     NA     0.12
## CI.mean.0.95   11.64   13.24           8.19     34.94  0.24     NA     0.25
## var          1606.87 2080.13         795.45  14479.18  0.66     NA     0.75
## std.dev        40.09   45.61          28.20    120.33  0.81     NA     0.86
## coef.var        0.35    0.61           0.23      0.56  0.41     NA    34.23
##              CooksD
## nbr.val       48.00
## nbr.null       9.00
## nbr.na         0.00
## min            0.00
## max            0.09
## range          0.09
## sum            0.69
## median         0.01
## mean           0.01
## SE.mean        0.00
## CI.mean.0.95   0.01
## var            0.00
## std.dev        0.02
## coef.var       1.55

#As we can see from descriptive statistics, now we only have 48 units in the sample. We will perform fit again.

fit2 <- lm (Profit ~ Administration + Marketing + StateF, data = Strtup)

#We will again configure the Standardized residuals and Cooks distances histograms to make sure that this time everything is okay.

Strtup$StdResid <- round(rstandard(fit2), 3)

hist(Strtup$StdResid, xlab = "Standardized Residuals", ylab = "Frequency")

#This time all units lie within the (-3,3) interval.

shapiro.test(Strtup$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  Strtup$StdResid
## W = 0.98261, p-value = 0.6901

#P value higher than 0.05 so it is okay.

Strtup$CooksD <- round(cooks.distance(fit2), 3)

hist(Strtup$CooksD, xlab = "Cooks Distance", ylab = "Frequency")

#On Cooks Distances histogram we still observe some gaps, so we will have to remove a few more units.

head(Strtup[order(-Strtup$CooksD),], 6)
##    ID Profit  RandD Administration Marketing State     StateF StdResid CooksD
## 7   7 156.12 134.62         147.20    127.72     2 California    2.858  0.147
## 48 50  14.68   0.00         116.98     45.17     2 California   -2.405  0.112
## 27 28 105.01  72.11         127.86    353.18     1   New York   -2.260  0.104
## 21 22 111.31  78.39         153.77    299.74     1   New York   -1.597  0.059
## 3   3 191.05 153.44         101.15    407.93     3    Florida    1.368  0.051
## 12 12 144.26 100.67          91.79    249.74     2 California    1.407  0.038
Strtup <- Strtup %>% filter(!ID == "7")
Strtup <- Strtup %>% filter(!ID == "50")
Strtup <- Strtup %>% filter(!ID == "28")

#We repeat the procedure, this time with only 45 units.

fit2 <- lm (Profit ~ Administration + Marketing + StateF, data = Strtup)
Strtup$StdResid <- round(rstandard(fit2), 3)

hist(Strtup$StdResid, xlab = "Standardized Residuals", ylab = "Frequency")

#All values within (-3,3) interval, so it is okay.

shapiro.test(Strtup$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  Strtup$StdResid
## W = 0.98078, p-value = 0.6514

#P value higher than 0.05, so it is okay.

Strtup$CooksD <- round(cooks.distance(fit2), 3)

hist(Strtup$CooksD, xlab = "Cooks Distance", ylab = "Frequency")

#This time we observe no gaps on the Cooks distances histogram, while all the values are low enough. We can proceed with the analysis.

#Now we will configure a scatterplot to check for linearity and homoscedasticity.

Strtup$StdFitted <- scale(fit2$fitted.values)

library(car)
scatterplot(y = Strtup$StdResid, x = Strtup$StdFitted, ylab = "Standardized residuals", xlab = "Standardized fitted values", boxplots = FALSE, regLine = FALSE, smooth = FALSE)

#We do not observe any curves in particular so linearity should be met, from the eye test homoscedasticity seems fine as well, but we will also perform the Breusch Pagan Test.

library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
ols_test_breusch_pagan(fit2)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##                Data                
##  ----------------------------------
##  Response : Profit 
##  Variables: fitted values of Profit 
## 
##         Test Summary         
##  ----------------------------
##  DF            =    1 
##  Chi2          =    0.4659576 
##  Prob > Chi2   =    0.4948528

#The last row represents the p value (0.49), so we cannot reject the H0 that the varience is constant. Homoscedasticity is met.

summary(fit2)
## 
## Call:
## lm(formula = Profit ~ Administration + Marketing + StateF, data = Strtup)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -35.614 -13.842   1.021   9.967  30.557 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      28.35459   12.62402   2.246   0.0303 *  
## Administration    0.22580    0.09131   2.473   0.0178 *  
## Marketing         0.27973    0.02241  12.482 2.24e-15 ***
## StateFCalifornia -3.55300    6.37844  -0.557   0.5806    
## StateFFlorida    -0.80357    6.42470  -0.125   0.9011    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.45 on 40 degrees of freedom
## Multiple R-squared:  0.8092, Adjusted R-squared:  0.7902 
## F-statistic: 42.42 on 4 and 40 DF,  p-value: 6.994e-14
sqrt(summary(fit2)$r.squared)
## [1] 0.8995799
library(lm.beta)
lm.beta(fit2)
## 
## Call:
## lm(formula = Profit ~ Administration + Marketing + StateF, data = Strtup)
## 
## Standardized Coefficients::
##      (Intercept)   Administration        Marketing StateFCalifornia 
##               NA       0.17099445       0.87531114      -0.04447085 
##    StateFFlorida 
##      -0.01005783

#Results interpretation of fit2:

fit3 <- lm (Profit ~ Administration + Marketing, data = Strtup)

summary(fit3)
## 
## Call:
## lm(formula = Profit ~ Administration + Marketing, data = Strtup)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -34.283 -13.432   2.147  10.481  30.131 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    26.56965   11.91238   2.230   0.0311 *  
## Administration  0.22635    0.08938   2.532   0.0152 *  
## Marketing       0.28097    0.02163  12.989 2.66e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.1 on 42 degrees of freedom
## Multiple R-squared:  0.8076, Adjusted R-squared:  0.7985 
## F-statistic: 88.16 on 2 and 42 DF,  p-value: 9.268e-16
sqrt(summary(fit3)$r.squared)
## [1] 0.8986811
lm.beta(fit3)
## 
## Call:
## lm(formula = Profit ~ Administration + Marketing, data = Strtup)
## 
## Standardized Coefficients::
##    (Intercept) Administration      Marketing 
##             NA      0.1714064      0.8791815

#We observe that the results of fit3 are very similar to those of fit2. Now we will compare the two models with ANOVA analysis of variance.

anova(fit3,fit2)
## Analysis of Variance Table
## 
## Model 1: Profit ~ Administration + Marketing
## Model 2: Profit ~ Administration + Marketing + StateF
##   Res.Df   RSS Df Sum of Sq      F Pr(>F)
## 1     42 12280                           
## 2     40 12176  2    103.17 0.1695 0.8447

#In this case the H0 is that there is no differences between models, so it is better to use the simplier model (Model 1, which is without the dummy variable). H1 is that the second model (Model 2, which includes the dummy variable) explains more. Based on the p value (=0.845) we cannot reject the H0, therefore it is better to use the simplier model (Model 1, which is without the dummy variable).

#1.3 Conclusion

How do these variables affect annual profit of U.S. startups?

Based on the analysis we found that the more appropriate model to use is the model without the dummy variable.

#Results interpretation of fit3: