Day8HwAssignment

Author

AR

remove(list = ls()) #clear environment 

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 
library(ggplot2) 
library(visdat)
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
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ lubridate 1.9.4     ✔ tibble    3.3.0
✔ purrr     1.1.0     ✔ tidyr     1.3.1
✔ readr     2.1.5     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(MASS)

Attaching package: 'MASS'

The following object is masked from 'package:dplyr':

    select
data(msleep) #upload data set
df <- msleep
summary(df) #summary stats
     name              genus               vore              order          
 Length:83          Length:83          Length:83          Length:83         
 Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character  
                                                                            
                                                                            
                                                                            
                                                                            
 conservation        sleep_total      sleep_rem      sleep_cycle    
 Length:83          Min.   : 1.90   Min.   :0.100   Min.   :0.1167  
 Class :character   1st Qu.: 7.85   1st Qu.:0.900   1st Qu.:0.1833  
 Mode  :character   Median :10.10   Median :1.500   Median :0.3333  
                    Mean   :10.43   Mean   :1.875   Mean   :0.4396  
                    3rd Qu.:13.75   3rd Qu.:2.400   3rd Qu.:0.5792  
                    Max.   :19.90   Max.   :6.600   Max.   :1.5000  
                                    NA's   :22      NA's   :51      
     awake          brainwt            bodywt        
 Min.   : 4.10   Min.   :0.00014   Min.   :   0.005  
 1st Qu.:10.25   1st Qu.:0.00290   1st Qu.:   0.174  
 Median :13.90   Median :0.01240   Median :   1.670  
 Mean   :13.57   Mean   :0.28158   Mean   : 166.136  
 3rd Qu.:16.15   3rd Qu.:0.12550   3rd Qu.:  41.750  
 Max.   :22.10   Max.   :5.71200   Max.   :6654.000  
                 NA's   :27                          
str(df)
tibble [83 × 11] (S3: tbl_df/tbl/data.frame)
 $ name        : chr [1:83] "Cheetah" "Owl monkey" "Mountain beaver" "Greater short-tailed shrew" ...
 $ genus       : chr [1:83] "Acinonyx" "Aotus" "Aplodontia" "Blarina" ...
 $ vore        : chr [1:83] "carni" "omni" "herbi" "omni" ...
 $ order       : chr [1:83] "Carnivora" "Primates" "Rodentia" "Soricomorpha" ...
 $ conservation: chr [1:83] "lc" NA "nt" "lc" ...
 $ sleep_total : num [1:83] 12.1 17 14.4 14.9 4 14.4 8.7 7 10.1 3 ...
 $ sleep_rem   : num [1:83] NA 1.8 2.4 2.3 0.7 2.2 1.4 NA 2.9 NA ...
 $ sleep_cycle : num [1:83] NA NA NA 0.133 0.667 ...
 $ awake       : num [1:83] 11.9 7 9.6 9.1 20 9.6 15.3 17 13.9 21 ...
 $ brainwt     : num [1:83] NA 0.0155 NA 0.00029 0.423 NA NA NA 0.07 0.0982 ...
 $ bodywt      : num [1:83] 50 0.48 1.35 0.019 600 ...
#install.packages("MASS") 
 
vis_dat(msleep)

?msleep
data("msleep") 

#vis_dat(msleep[1:80,])
vis_miss(msleep)

?msleep

df$bodywt <- as.numeric(df$bodywt) 

df$genus <- as.numeric(df$genus)
Warning: NAs introduced by coercion
df$vore <- as.numeric(df$vore)
Warning: NAs introduced by coercion
df$sleep_cycle <- NULL #too much data missing
df$bodywt <- NULL 
df$genus <- NULL 
df$vore <- NULL
reg1 <- lm(data = df, 
           formula = awake ~ sleep_rem + sleep_total + conservation + order + brainwt) 

reg2 <- lm(data = df, 
          formula = awake ~ sleep_rem + conservation + order + brainwt)

stargazer(reg2, type = "text")

================================================
                         Dependent variable:    
                     ---------------------------
                                awake           
------------------------------------------------
sleep_rem                     -1.712***         
                               (0.541)          
                                                
conservationen                  1.499           
                               (1.913)          
                                                
conservationlc                  0.191           
                               (1.044)          
                                                
conservationnt                 -0.940           
                               (1.919)          
                                                
conservationvu                  1.888           
                               (2.593)          
                                                
orderCarnivora                 -1.020           
                               (1.482)          
                                                
orderChiroptera               -7.983***         
                               (2.590)          
                                                
orderCingulata                -5.790**          
                               (2.271)          
                                                
orderDidelphimorphia           -4.611           
                               (2.887)          
                                                
orderErinaceomorpha             0.130           
                               (2.107)          
                                                
orderHyracoidea                 0.188           
                               (1.866)          
                                                
orderLagomorpha                -1.706           
                               (2.089)          
                                                
orderPerissodactyla            -0.460           
                               (2.026)          
                                                
orderPrimates                 -4.735**          
                               (2.073)          
                                                
orderRodentia                 -3.933**          
                               (1.572)          
                                                
orderSoricomorpha              -2.214           
                               (1.776)          
                                                
brainwt                         6.609           
                               (4.586)          
                                                
Constant                      18.767***         
                               (1.536)          
                                                
------------------------------------------------
Observations                     31             
R2                              0.948           
Adjusted R2                     0.879           
Residual Std. Error        1.639 (df = 13)      
F Statistic            13.879*** (df = 17; 13)  
================================================
Note:                *p<0.1; **p<0.05; ***p<0.01
clean_data <- na.omit(msleep)

reg2 <- lm(sleep_total ~ bodywt + vore + brainwt, data = clean_data)

best_model <- stepAIC(object = reg2, direction = c("backward"))
Start:  AIC=57.13
sleep_total ~ bodywt + vore + brainwt

          Df Sum of Sq    RSS    AIC
- vore     3   14.3503 205.38 52.582
- bodywt   1    1.9848 193.01 55.340
- brainwt  1    6.7623 197.79 55.829
<none>                 191.03 57.133

Step:  AIC=52.58
sleep_total ~ bodywt + brainwt

          Df Sum of Sq    RSS    AIC
- brainwt  1    5.0474 210.42 51.068
- bodywt   1    5.2649 210.64 51.088
<none>                 205.38 52.582

Step:  AIC=51.07
sleep_total ~ bodywt

         Df Sum of Sq    RSS    AIC
<none>                210.42 51.068
- bodywt  1       182 392.42 61.532
summary(best_model)

Call:
lm(formula = sleep_total ~ bodywt, data = clean_data)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.4230 -2.5661 -0.0006  1.8834  7.1959 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 12.504535   0.830465  15.057 1.21e-11 ***
bodywt      -0.017742   0.004497  -3.946 0.000948 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.419 on 18 degrees of freedom
Multiple R-squared:  0.4638,    Adjusted R-squared:  0.434 
F-statistic: 15.57 on 1 and 18 DF,  p-value: 0.0009479

Step AIC

vis_dat(msleep) 

library(MASS)
reg1 <-
  lm(formula = awake ~ ., data = msleep)

stargazer(reg1, type = "text")

==============================================================
                                       Dependent variable:    
                                   ---------------------------
                                              awake           
--------------------------------------------------------------
nameBrazilian tapir                          15.300           
                                                              
                                                              
nameChinchilla                                7.200           
                                                              
                                                              
nameCow                                      15.700           
                                                              
                                                              
nameDog                                       9.600           
                                                              
                                                              
nameDomestic cat                              7.200           
                                                              
                                                              
nameEastern american mole                    11.300           
                                                              
                                                              
nameEuropean hedgehog                         9.600           
                                                              
                                                              
nameGolden hamster                            5.400           
                                                              
                                                              
nameGreater short-tailed shrew                4.800           
                                                              
                                                              
nameGuinea pig                               10.300           
                                                              
                                                              
nameHorse                                    16.800           
                                                              
                                                              
nameHouse mouse                               7.200           
                                                              
                                                              
nameLaboratory rat                            6.700           
                                                              
                                                              
nameLesser short-tailed shrew                10.600           
                                                              
                                                              
nameLong-nosed armadillo                      2.300           
                                                              
                                                              
nameNorth American Opossum                    1.700           
                                                              
                                                              
namePig                                      10.600           
                                                              
                                                              
nameRabbit                                   11.300           
                                                              
                                                              
nameThirteen-lined ground squirrel            5.900           
                                                              
                                                              
genusBos                                                      
                                                              
                                                              
genusCanis                                                    
                                                              
                                                              
genusCavis                                                    
                                                              
                                                              
genusChinchilla                                               
                                                              
                                                              
genusCryptotis                                                
                                                              
                                                              
genusDasypus                                                  
                                                              
                                                              
genusDidelphis                                                
                                                              
                                                              
genusEptesicus                                                
                                                              
                                                              
genusEquus                                                    
                                                              
                                                              
genusErinaceus                                                
                                                              
                                                              
genusFelis                                                    
                                                              
                                                              
genusMesocricetus                                             
                                                              
                                                              
genusMus                                                      
                                                              
                                                              
genusOryctolagus                                              
                                                              
                                                              
genusRattus                                                   
                                                              
                                                              
genusScalopus                                                 
                                                              
                                                              
genusSpermophilus                                             
                                                              
                                                              
genusSus                                                      
                                                              
                                                              
genusTapirus                                                  
                                                              
                                                              
voreherbi                                                     
                                                              
                                                              
voreinsecti                                                   
                                                              
                                                              
voreomni                                                      
                                                              
                                                              
orderCarnivora                                                
                                                              
                                                              
orderChiroptera                                               
                                                              
                                                              
orderCingulata                                                
                                                              
                                                              
orderDidelphimorphia                                          
                                                              
                                                              
orderErinaceomorpha                                           
                                                              
                                                              
orderLagomorpha                                               
                                                              
                                                              
orderPerissodactyla                                           
                                                              
                                                              
orderRodentia                                                 
                                                              
                                                              
orderSoricomorpha                                             
                                                              
                                                              
conservationen                                                
                                                              
                                                              
conservationlc                                                
                                                              
                                                              
conservationnt                                                
                                                              
                                                              
conservationvu                                                
                                                              
                                                              
sleep_total                                                   
                                                              
                                                              
sleep_rem                                                     
                                                              
                                                              
sleep_cycle                                                   
                                                              
                                                              
brainwt                                                       
                                                              
                                                              
bodywt                                                        
                                                              
                                                              
Constant                                      4.300           
                                                              
                                                              
--------------------------------------------------------------
Observations                                   20             
R2                                            1.000           
==============================================================
Note:                              *p<0.1; **p<0.05; ***p<0.01
summary(reg2)

Call:
lm(formula = sleep_total ~ bodywt + vore + brainwt, data = clean_data)

Residuals:
    Min      1Q  Median      3Q     Max 
-5.6449 -2.6665  0.6286  2.1434  5.6449 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  13.761236   2.179664   6.313 1.91e-05 ***
bodywt       -0.005898   0.015464  -0.381    0.709    
voreherbi    -2.060362   2.551881  -0.807    0.433    
voreinsecti   0.297237   3.400524   0.087    0.932    
voreomni     -1.001401   2.700763  -0.371    0.716    
brainwt     -10.912003  15.500215  -0.704    0.493    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.694 on 14 degrees of freedom
Multiple R-squared:  0.5132,    Adjusted R-squared:  0.3394 
F-statistic: 2.952 on 5 and 14 DF,  p-value: 0.05032
summary(clean_data)
     name              genus               vore              order          
 Length:20          Length:20          Length:20          Length:20         
 Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character  
                                                                            
                                                                            
                                                                            
 conservation        sleep_total       sleep_rem      sleep_cycle    
 Length:20          Min.   : 2.900   Min.   :0.600   Min.   :0.1167  
 Class :character   1st Qu.: 8.925   1st Qu.:1.300   1st Qu.:0.1792  
 Mode  :character   Median :11.300   Median :2.350   Median :0.2500  
                    Mean   :11.225   Mean   :2.275   Mean   :0.3458  
                    3rd Qu.:13.925   3rd Qu.:3.125   3rd Qu.:0.4167  
                    Max.   :19.700   Max.   :4.900   Max.   :1.0000  
     awake          brainwt            bodywt        
 Min.   : 4.30   Min.   :0.00014   Min.   :  0.0050  
 1st Qu.:10.07   1st Qu.:0.00115   1st Qu.:  0.0945  
 Median :12.70   Median :0.00590   Median :  0.7490  
 Mean   :12.78   Mean   :0.07882   Mean   : 72.1177  
 3rd Qu.:15.07   3rd Qu.:0.03670   3rd Qu.:  6.1250  
 Max.   :21.10   Max.   :0.65500   Max.   :600.0000  
head(clean_data)
# A tibble: 6 × 11
  name    genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
  <chr>   <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
1 Greate… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1
2 Cow     Bos   herbi Arti… domesticated         4         0.7       0.667  20  
3 Dog     Canis carni Carn… domesticated        10.1       2.9       0.333  13.9
4 Guinea… Cavis herbi Rode… domesticated         9.4       0.8       0.217  14.6
5 Chinch… Chin… herbi Rode… domesticated        12.5       1.5       0.117  11.5
6 Lesser… Cryp… omni  Sori… lc                   9.1       1.4       0.15   14.9
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>