library(tidyverse)
library(knitr)
library(ISLR2)
theme_set(theme_bw())
Classification is the process by which we divide observations into groups. Common examples include “Is this spam”, “Is this fraudulent”, “Is this a real person”.
As with Regression we have training observations that we can use to build a classifier.
defData<-Default
ggplot(defData,aes(x=balance,y=income,color=default))+
geom_point(alpha=.25)+
scale_color_manual(values=c("lightblue","red"))
ggplot(defData,aes(x=balance,y=default,color=as.factor(default)))+
geom_point()+
scale_color_manual(values=c("lightblue","red"))
Question 1 Create a Least Squares Regression and add it as an abline to the above plot to demonstrate the non-meaningful estimates.
Goal: Predict Pr(Y|X) or the Probability of Y given X.
Question 2 What percent of people with the following odds will default? 1/4, 9, 49, 99, N
Once we have the coefficients, we can make predictions by plugging our \(\beta\) values back into the original equation \(p(x)=\frac{e^{\beta_0+\beta_1X}}{1+e^{\beta_0+\beta_1X}}\)
log_model<-glm(default~balance, defData,family = binomial)
summary(log_model)
##
## Call:
## glm(formula = default ~ balance, family = binomial, data = defData)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.065e+01 3.612e-01 -29.49 <2e-16 ***
## balance 5.499e-03 2.204e-04 24.95 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2920.6 on 9999 degrees of freedom
## Residual deviance: 1596.5 on 9998 degrees of freedom
## AIC: 1600.5
##
## Number of Fisher Scoring iterations: 8
predict.glm(log_model,data.frame(balance=1000)) #logodds
## 1
## -5.152414
predict.glm(log_model,data.frame(balance=1000),type="response") #\hat{p}(x)
## 1
## 0.005752145
logOdds=predict.glm(log_model,data.frame(balance=1000)) #proof
exp(logOdds)/(1+exp(logOdds))
## 1
## 0.005752145
Start Working with partner
As with linear regression we can have multiple predictors, and it extends the same way.
\[p(x)=\frac{e^{\beta_0+\sum_i(\beta_iX_i)}}{1+e^{\beta_0+\sum_i(\beta_iX_i)}}\] Question 3
Y=sample(c(1,2,3,5),100,TRUE)
X=runif(100)
This week we discussed Logistic Regression and Classification:
\(~\)
Answers
Question 1
defData2<-defData
defData2$default<-as.integer(defData2$default)
defData2$default<-defData2$default-1
coefs=lm(default~balance,defData2)$coefficients
ggplot(defData2,aes(x=balance,y=default,color=as.factor(default)))+
geom_point()+
scale_color_manual(values=c("lightblue","red"))+
geom_abline(slope=coefs[2],intercept=coefs[1])
Question 2 What percent of people with the following odds will default?
Question 3
A. You can use dummy variables: Create a logistic model for default given student status. Are students more or less likely to default? More B. Create a logistic model to predict default given balance, income, and student. What do you notice? Income doesn’t seem to matter, Student sign flips C. Can you explain the difference between A and B? Students have more debt on average, but for the same amount of debt are less risky (Confounding variables matter)
log_model<-glm(default~student, defData,family = binomial)
summary(log_model)
##
## Call:
## glm(formula = default ~ student, family = binomial, data = defData)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -3.50413 0.07071 -49.55 < 2e-16 ***
## studentYes 0.40489 0.11502 3.52 0.000431 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2920.6 on 9999 degrees of freedom
## Residual deviance: 2908.7 on 9998 degrees of freedom
## AIC: 2912.7
##
## Number of Fisher Scoring iterations: 6
log_model<-glm(default~balance+income+student, defData,family = binomial)
summary(log_model)
##
## Call:
## glm(formula = default ~ balance + income + student, family = binomial,
## data = defData)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.087e+01 4.923e-01 -22.080 < 2e-16 ***
## balance 5.737e-03 2.319e-04 24.738 < 2e-16 ***
## income 3.033e-06 8.203e-06 0.370 0.71152
## studentYes -6.468e-01 2.363e-01 -2.738 0.00619 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2920.6 on 9999 degrees of freedom
## Residual deviance: 1571.5 on 9996 degrees of freedom
## AIC: 1579.5
##
## Number of Fisher Scoring iterations: 8