#ELMR 5.5

library(faraway)
library(nnet)
data<-faraway::debt
ccID<-data$ccarduse
levels(ccID)<-c("Never","Occassional","Regularly")
summary(ccID)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   1.000   1.000   1.000   1.677   2.000   3.000      34
table(ccID)
## ccID
##   1   2   3 
## 232 105  93
lmod<-lm(ccID~.,debt)
summary(lmod)
## 
## Call:
## lm(formula = ccID ~ ., data = debt)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -2.212e-15 -2.570e-17 -6.000e-20  3.269e-17  5.339e-16 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept) -1.758e-16  8.735e-17 -2.013e+00  0.04508 *  
## incomegp    -9.831e-17  7.491e-18 -1.312e+01  < 2e-16 ***
## house       -1.792e-17  1.492e-17 -1.201e+00  0.23061    
## children     1.293e-17  8.617e-18  1.501e+00  0.13453    
## singpar     -1.165e-16  3.834e-17 -3.039e+00  0.00259 ** 
## agegp       -2.945e-17  1.059e-17 -2.780e+00  0.00579 ** 
## bankacc     -1.617e-16  2.677e-17 -6.042e+00 4.67e-09 ***
## bsocacc     -4.879e-17  1.847e-17 -2.641e+00  0.00872 ** 
## manage      -1.524e-17  1.019e-17 -1.496e+00  0.13583    
## ccarduse     1.000e+00  1.186e-17  8.433e+16  < 2e-16 ***
## cigbuy      -2.329e-17  1.938e-17 -1.202e+00  0.23035    
## xmasbuy      2.745e-18  2.646e-17  1.040e-01  0.91744    
## locintrn     5.821e-18  9.814e-18  5.930e-01  0.55352    
## prodebt     -8.800e-18  1.294e-17 -6.800e-01  0.49693    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.448e-16 on 290 degrees of freedom
##   (160 observations deleted due to missingness)
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 7.472e+32 on 13 and 290 DF,  p-value: < 2.2e-16
mmod<-multinom(ccID ~ incomegp + bankacc + prodebt, debt)
## # weights:  15 (8 variable)
## initial  value 396.599036 
## iter  10 value 332.156027
## final  value 331.999894 
## converged
summary(mmod)
## Call:
## multinom(formula = ccID ~ incomegp + bankacc + prodebt, data = debt)
## 
## Coefficients:
##   (Intercept)  incomegp  bankacc   prodebt
## 2   -3.984421 0.2841986 1.620668 0.3239365
## 3   -7.006777 0.5041507 2.677107 0.6606552
## 
## Std. Errors:
##   (Intercept)  incomegp   bankacc   prodebt
## 2   0.8107364 0.1050903 0.5549301 0.1946700
## 3   1.2528654 0.1154704 1.0423807 0.2069253
## 
## Residual Deviance: 663.9998 
## AIC: 679.9998
par(mfrow = c(1,3))
matplot(prop.table(table(data$prodebt,ccID),1),type="l",xlab="ProDebt",ylab="Proportion",lty=c(1,2,3))
matplot(prop.table(table(data$incomegp,ccID),1),type="l",xlab="Income Gapt",ylab="Proportion",lty=c(1,2,3))
matplot(prop.table(table(data$bankacc,ccID),1),type="l",xlab="Bank Act",ylab="Proportion",lty=c(1,2,3))