# Clear the workspace
  rm(list = ls())  # Clear environment
  gc()             # Clear unused memory
##          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 531014 28.4    1182544 63.2         NA   669277 35.8
## Vcells 973384  7.5    8388608 64.0      16384  1840364 14.1
  cat("\f")        # Clear the console

1 Data

1.1 Export Data to Excel

?attitude
df <- attitude
getwd()
## [1] "/Users/arvindsharma/Library/CloudStorage/Dropbox/WCAS/Data Analysis/Data Analysis - Spring II 2024/Data Analysis - Spring II 2024 (shared files)/W6/Week 14"
write.csv(x = df, file = "attitude_data")

2 Simple Linear Regression

\[ rating_i = \beta_0 + \beta_1 \ complaints_i + \epsilon_i \]

model1 <- lm(formula = df$rating ~ df$complaints)

model1 <- 
lm(data = df,
   formula = rating ~ complaints)

summary(model1)
## 
## Call:
## lm(formula = rating ~ complaints, data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.8799  -5.9905   0.1783   6.2978   9.6294 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 14.37632    6.61999   2.172   0.0385 *  
## complaints   0.75461    0.09753   7.737 1.99e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.993 on 28 degrees of freedom
## Multiple R-squared:  0.6813, Adjusted R-squared:  0.6699 
## F-statistic: 59.86 on 1 and 28 DF,  p-value: 1.988e-08

2.1 Slope

?var 

cov(x = df$complaints, y = df$rating)
## [1] 133.7793
var(x = df$complaints)
## [1] 177.2828
beta_1 <- cov(x = df$complaints, y = df$rating)/var(x = df$complaints)
beta_1
## [1] 0.7546098

2.2 Intercept

beta_0 <- 
mean(df$rating) - mean(df$complaints) * beta_1

beta_0
## [1] 14.37632

3 Multivariate Linear Regression

model2 <- 
lm(data = df,
   formula = rating ~ .)

summary(model2)
## 
## Call:
## lm(formula = rating ~ ., data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.9418  -4.3555   0.3158   5.5425  11.5990 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.78708   11.58926   0.931 0.361634    
## complaints   0.61319    0.16098   3.809 0.000903 ***
## privileges  -0.07305    0.13572  -0.538 0.595594    
## learning     0.32033    0.16852   1.901 0.069925 .  
## raises       0.08173    0.22148   0.369 0.715480    
## critical     0.03838    0.14700   0.261 0.796334    
## advance     -0.21706    0.17821  -1.218 0.235577    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.068 on 23 degrees of freedom
## Multiple R-squared:  0.7326, Adjusted R-squared:  0.6628 
## F-statistic:  10.5 on 6 and 23 DF,  p-value: 1.24e-05
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
stargazer(model1, model2, type="text")
## 
## =================================================================
##                                  Dependent variable:             
##                     ---------------------------------------------
##                                        rating                    
##                              (1)                    (2)          
## -----------------------------------------------------------------
## complaints                 0.755***               0.613***       
##                            (0.098)                (0.161)        
##                                                                  
## privileges                                         -0.073        
##                                                   (0.136)        
##                                                                  
## learning                                           0.320*        
##                                                   (0.169)        
##                                                                  
## raises                                             0.082         
##                                                   (0.221)        
##                                                                  
## critical                                           0.038         
##                                                   (0.147)        
##                                                                  
## advance                                            -0.217        
##                                                   (0.178)        
##                                                                  
## Constant                   14.376**                10.787        
##                            (6.620)                (11.589)       
##                                                                  
## -----------------------------------------------------------------
## Observations                  30                     30          
## R2                          0.681                  0.733         
## Adjusted R2                 0.670                  0.663         
## Residual Std. Error    6.993 (df = 28)        7.068 (df = 23)    
## F Statistic         59.861*** (df = 1; 28) 10.502*** (df = 6; 23)
## =================================================================
## Note:                                 *p<0.1; **p<0.05; ***p<0.01
library(readxl)
attitude <- read_excel("~/Library/CloudStorage/Dropbox/WCAS/Data Analysis/Data Analysis - Fall 2023/Data Analysis - Fall 2023 (shared files)/Week 15/attitude_2.xls", 
    sheet = "Sheet4", range = "A1:I31")

model3 <- 
lm(data = attitude,
   formula = rating ~ .)

summary(model3)
## 
## Call:
## lm(formula = rating ~ ., data = attitude)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.5940  -4.8315   0.3552   5.5677  10.9790 
## 
## Coefficients: (1 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 10.06433   12.02017   0.837  0.41144   
## complaints   0.59792    0.17054   3.506  0.00199 **
## privileges  -0.05822    0.14549  -0.400  0.69289   
## learning     0.32029    0.17188   1.863  0.07580 . 
## raises       0.09673    0.23039   0.420  0.67866   
## critical     0.04266    0.15048   0.283  0.77945   
## advance     -0.23265    0.18776  -1.239  0.22838   
## male         1.00077    3.02258   0.331  0.74370   
## female            NA         NA      NA       NA   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.209 on 22 degrees of freedom
## Multiple R-squared:  0.7339, Adjusted R-squared:  0.6493 
## F-statistic: 8.669 on 7 and 22 DF,  p-value: 4.1e-05
stargazer(model1, model2,model3, type="text")
## 
## =======================================================================================
##                                             Dependent variable:                        
##                     -------------------------------------------------------------------
##                                                   rating                               
##                              (1)                    (2)                    (3)         
## ---------------------------------------------------------------------------------------
## complaints                 0.755***               0.613***              0.598***       
##                            (0.098)                (0.161)                (0.171)       
##                                                                                        
## privileges                                         -0.073                -0.058        
##                                                   (0.136)                (0.145)       
##                                                                                        
## learning                                           0.320*                0.320*        
##                                                   (0.169)                (0.172)       
##                                                                                        
## raises                                             0.082                  0.097        
##                                                   (0.221)                (0.230)       
##                                                                                        
## critical                                           0.038                  0.043        
##                                                   (0.147)                (0.150)       
##                                                                                        
## advance                                            -0.217                -0.233        
##                                                   (0.178)                (0.188)       
##                                                                                        
## male                                                                      1.001        
##                                                                          (3.023)       
##                                                                                        
## female                                                                                 
##                                                                                        
##                                                                                        
## Constant                   14.376**                10.787                10.064        
##                            (6.620)                (11.589)              (12.020)       
##                                                                                        
## ---------------------------------------------------------------------------------------
## Observations                  30                     30                    30          
## R2                          0.681                  0.733                  0.734        
## Adjusted R2                 0.670                  0.663                  0.649        
## Residual Std. Error    6.993 (df = 28)        7.068 (df = 23)        7.209 (df = 22)   
## F Statistic         59.861*** (df = 1; 28) 10.502*** (df = 6; 23) 8.669*** (df = 7; 22)
## =======================================================================================
## Note:                                                       *p<0.1; **p<0.05; ***p<0.01
plot(model3)