R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Including Plots

You can also embed plots, for example:

## load/install libraries
## load/install libraries
.libPaths(c("./Rpackages",.libPaths()))
library(car)
library(lmtest)
library(sandwich)
library(performance)
library(broom)
library(ggplot2)
library(ggfortify)
library(olsrr)
library(visreg)
library(dplyr)
library(knitr, rmarkdown)
library(stargazer, modelsummary)

####fitting simple linear regression
fit <- lm(Y ~ X)
summary(fit)
## 
## Call:
## lm(formula = Y ~ X)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -14.567 -10.249   2.171   6.906  12.301 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   7.0194     7.9708   0.881    0.399    
## X             0.9560     0.1293   7.393 2.33e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.23 on 10 degrees of freedom
## Multiple R-squared:  0.8453, Adjusted R-squared:  0.8299 
## F-statistic: 54.65 on 1 and 10 DF,  p-value: 2.335e-05
####Load the data set mtcars from R and fit the model wt on mpg and interpret the coefficients. 
data(mtcars)
dim(mtcars); head(mtcars)
## [1] 32 11
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
plot(mtcars$wt,mtcars$mpg)

cor(mtcars$wt, mtcars$mpg)
## [1] -0.8676594
model2<-lm(mtcars$mpg~mtcars$wt)
summary(model2)
## 
## Call:
## lm(formula = mtcars$mpg ~ mtcars$wt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## mtcars$wt    -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10
####Predict the wt when mpg is 40.3.
wt_value <- 40.3
y_hat <- coef(model2)[1] + coef(model2)[2] * wt_value
y_hat
## (Intercept) 
##   -178.0971
####Example Calculation of Spearman’s Rank Correlation
x<-c(50,40,30,20,10)
Y<-c(10,20,30,40,50)
cor(x, Y, method = "spearman")
## [1] -1

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.