answer question

1 answer below »
Answered Same DayDec 17, 2021

Answer To: answer question

Mohd answered on Dec 18 2021
128 Votes
classification
classification
Catherine
12/18/2021
knitr::opts_chunk$set(echo = TRUE,cache = TRUE,warning = FALSE,message = FALSE,dpi = 180,fig.width = 8,fig.height = 5)
library(dplyr)
library(ggplot2)
library(magrit
tr)
library(rmarkdown)
library(C50)
library(pROC)
Fit Decision Tree and Logistic Regression to predict affairs (Attribute if_affair is the dependent/target variable).
library(readr)
affairs <- read_csv("New folder (2)/affairs.csv")
affairs$if_affair<-factor(affairs$if_affair)
str(affairs)
## spec_tbl_df [601 x 5] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ age : num [1:601] 37 27 32 57 22 32 22 57 32 22 ...
## $ yearsmarried : num [1:601] 10 4 15 15 0.75 1.5 0.75 15 15 1.5 ...
## $ religiousness: num [1:601] 3 4 1 5 2 2 2 2 4 4 ...
## $ rating : num [1:601] 4 4 4 5 3 5 3 4 2 5 ...
## $ if_affair : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "spec")=
## .. cols(
## .. age = col_double(),
## .. yearsmarried = col_double(),
## .. religiousness = col_double(),
## .. rating = col_double(),
## .. if_affair = col_character()
## .. )
## - attr(*, "problems")=
affairs<-affairs%>%
mutate(if_affair=ifelse(if_affair=="no",0,1))
summary(affairs)
## age yearsmarried religiousness rating
## Min. :17.50 Min. : 0.125 Min. :1.000 Min. :1.000
## 1st Qu.:27.00 1st Qu.: 4.000 1st Qu.:2.000 1st Qu.:3.000
## Median :32.00 Median : 7.000 Median :3.000 Median :4.000
## Mean :32.49 Mean : 8.178 Mean :3.116 Mean :3.932
## 3rd Qu.:37.00 3rd Qu.:15.000 3rd Qu.:4.000 3rd Qu.:5.000
## Max. :57.00 Max. :15.000 Max. :5.000 Max. :5.000
## if_affair
## Min. :0.0000
## 1st Qu.:0.0000
## Median :0.0000
## Mean :0.2496
## 3rd Qu.:0.0000
## Max. :1.0000
affairs$if_affair<-factor(affairs$if_affair)
set.seed(333)
size <- floor(0.8 * nrow(affairs))

### randomly decide which ones for training
training_index <- sample(nrow(affairs), size = size, replace = FALSE)

train <- affairs[training_index,]
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here