0% found this document useful (0 votes)
18 views7 pages

R Basic Guide

Uploaded by

anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

R Basic Guide

Uploaded by

anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

R Basic Guide

1st step: Load the library needed

```{r Libraries}
library(PoEdata)
library(tidyverse)
library(margins)
library(caret)
library(knitr)
```

2nd step: Import data

```{r data}
data(lasvegas)
head(lasvegas)
summary(lasvegas)
```

3rd step: Model introduction

(what model are you gonna use and what is the purpose)
Introduce your variables:
Dependent Variable
Independent Variable

```{r table}
model_summary <- data.frame(
variable = c(“X1”, “X2”, “X3”),
definition = c(“d1”, “d2”, “d3”),
expected sign = c(“+”, “-“, “+”),
explanation = c(“blah1”, “blah2”, “blah3”),
stringsAsFactors = FALSE)

kable(model_summary, caption = “Name of the table”)

4th step: Data Inspection


```{r data inspection}
Label <- data %>% select(variables that will be used)
Correlation_matrix(or any label) <- cor(Label)
Print(correlation_matrix, round(3))
```

5th step: build your lpm model

```{r lpm}
Lpm_model <- lm(variables, data = (name of data))
Lpm_model %>% summary
```
6th step: coef of lpm

``` {r coef}
coef(lpm_model)

b0 <- coef(lpm_model)[[1]]
b1 <- coef(lpm_model)[[2]]
b2 <- coef(lpm_model)[[3]]
b3 <- coef(lpm_model)[[4]]
b4 <- coef(lpm_model)[[5]]
b5 <- coef(lpm_model)[[6]]
b6 <- coef(lpm_model)[[7]]
```

7th Step:
###Scenario1:
**Normal Case (data observation) **
```{r Step 5.1}
p1 <- c(var1, var2, var3, var4, var4, var5)
prob_p1 <- b0 + b1*p1[1] + b2*p1[2] + b3*p1[3] + b4*p1[4] + b5*p1[5] +
b6*p1[6]
print(paste(
"The probability of a borrower being delinquent with",
p1[1], "refinance status,",
p1[2], "mortgage insurance status,",
p1[3], "% initial interest rate,",
p1[4], "credit score,",
p1[5], "years loan term, and",
p1[6], "adjustable-rate mortgage status is",
prob_p1 %>% round(4)*100, "%"))

8th Step
###Scenario2 {same process sa 7th step}
###Scenario3 {same process sa 7th step}

10th step: Let’s build your Logit Model

```{r Step 6}
##Logit Model
logit_model <- glm(formula = delinquent ~ ref + insur + rate + credit +
term + arm,
family = binomial(link = "logit"),
data = newlv)
logit_model %>% summary()
```

11th step: Marginal Effects of Logit Model

```{r Step 7}
coef(logit_model)
b0 <- coef(logit_model)[[1]]
b1 <- coef(logit_model)[[2]]
b2 <- coef(logit_model)[[3]]
b3 <- coef(logit_model)[[4]]
b4 <- coef(logit_model)[[5]]
b5 <- coef(logit_model)[[6]]
b6 <- coef(logit_model)[[7]]
```

12th step
###Scenario1:
**Normal Case (data observation) **

```{r Step 7.1}


p1 <- c(0, 1, 4.5, 680, 30, 0)
A_p1 <- b0 + b1*p1[1] + b2*p1[2] + b3*p1[3] + b4*p1[4] + b5*p1[5] +
b6*p1[6]
prob_p1 <- 1 / (1 + exp(-A_p1))
print(paste(
"The probability of a borrower being delinquent with",
p1[1], "refinance status,",
p1[2], "mortgage insurance status,",
p1[3], "% initial interest rate,",
p1[4], "credit score,",
p1[5], "years loan term, and",
p1[6], "adjustable-rate mortgage status is",
prob_p1 %>% round(4)*100, "%"))
```

13th step
###Scenario2 {same process sa 12th step}
###Scenario3 {same process sa 12th step}

14th Step Logit Model Evaluation

```{r Step 8}
##Logit Model Test
actual <- lasvegas$delinquent %>% as.factor
logit_prediction <- (fitted(logit_model) > 0.5) %>% as.numeric %>%
as.factor
confusionMatrix(logit_prediction, actual, positive = "1")
```

15th Step. Let’s do Probit Model

```{r Step 9}
##Probit Model
probit_model <- glm(formula = delinquent ~ ref + insur + rate + credit +
term + arm,
family = binomial(link = "probit"),
data = lasvegas)
probit_model %>% summary()
```

16th Step. Marginal Effects of Probit Model

```{r Step 10}


coef(probit_model)

b0 <- coef(probit_model)[[1]]
b1 <- coef(probit_model)[[2]]
b2 <- coef(probit_model)[[3]]
b3 <- coef(probit_model)[[4]]
b4 <- coef(probit_model)[[5]]
b5 <- coef(probit_model)[[6]]
b6 <- coef(probit_model)[[7]]
```

17th Step.
###Scenario1:
**Normal Case (data observation) **
```{r Step 10.1}
p1 <- c(0, 1, 4.5, 680, 30, 0)
A_p1 <- b0 + b1*p1[1] + b2*p1[2] + b3*p1[3] + b4*p1[4] + b5*p1[5] +
b6*p1[6]
prob_p1 <- pnorm(A_p1)
print(paste(
"The probability of a borrower being delinquent with",
p1[1], "refinance status,",
p1[2], "mortgage insurance status,",
p1[3], "% initial interest rate,",
p1[4], "credit score,",
p1[5], "years loan term, and",
p1[6], "adjustable-rate mortgage status is",
prob_p1 %>% round(4)*100, "%"))
```

18th Step.
###Scenario2 {same process sa 17th step}
###Scenario3 {same process sa 17th step}

19th Step. Probit Model Evaluation

```{r Step 11}


##Probit Model Test
actual <- newlv$delinquent %>% as.factor
probit_prediction <- (fitted(probit_model) > 0.5) %>% as.numeric %>%
as.factor
confusionMatrix(probit_prediction, actual, positive = "1")
```

20th Step. Analysis.


Analysis of LPM to each coefficients and its statistical significance.
Analysis of Logit Model to each coefficients and its statistical significance.
Analysis of Probit Model to each coefficients and its statistical significance.

21th Step. Compare and contrast the Confusion Matrix of Logit and Probit Model. Why
the chosen model is best compare to the other one?

22th Step.
Policy Recommendation (mas preferable kung ang kini is connected sa result sa inyong
analysis each model and scenario)

You might also like