You are on page 1of 3

library(ggplot2)

library(GGally)

str(heart)

names(heart)

head(heart)

summary(heart)

heart$SEX <- factor(heart$sex)

heart$AGE <- factor(heart$age)

heart$OUTPUT <- factor(heart$output)

str(heart)

table(heart$age)

table(heart$sex)

table(heart$cp)

table(heart$trtbps)

table(heart$chol)

table(heart$fbs)

table(heart$restecg)

table(heart$thalachh)

table(heart$exng)

table(heart$oldpeak)

table(heart$slp)

table(heart$caa)

table(heart$thall)

table(heart$output)

# correlation

pairs(data, pch = 18, col = "steelblue")

ggpairs(data)
# numerical data

ggplot(heart, aes(x=age,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=trtbps,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=chol,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=thalachh,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=oldpeak,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

# categorical data

ggplot(heart, aes(x=sex,fill=OUTPUT)) + geom_bar()

ggplot(heart, aes(x=cp,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=fbs,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=restecg,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=exng,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=slp,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=caa,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

ggplot(heart, aes(x=thall,fill=OUTPUT)) + geom_bar() + facet_wrap(~ sex)

# Multiple linear regression

data <- heart[ , c("age", "sex", "cp", "trtbps", "chol", "fbs", "restecg", "thalachh", "exng", "oldpeak",
"slp", "caa", "thall", "output")]

head(data)

model <- lm(age ~ sex+cp+trtbps+chol+fbs+restecg+thalachh+exng+oldpeak+slp+caa+thall+output,


data = data)

summary(model)

ggplot(heart,aes(x=sex+cp+trtbps+chol+fbs+restecg+thalachh+exng+oldpeak+slp+caa+thall+output,y
=age)) +

geom_point(mapping = aes(x =
sex+cp+trtbps+chol+fbs+restecg+thalachh+exng+oldpeak+slp+caa+thall+output, y = age, color =
sex)) +

stat_smooth(method=lm, se=FALSE, color="blue")


hist(residuals(model), col = "steelblue")

plot(fitted(model), residuals(model))

abline(h = 0, lty = 2)

#Prediction

intercept <- coef(summary(model))["(Intercept)", "Estimate"]

sex <- coef(summary(model))["sex", "Estimate"]

cp <- coef(summary(model))["cp", "Estimate"]

trtbps <- coef(summary(model))["trtbps", "Estimate"]

chol <- coef(summary(model))["chol", "Estimate"]

fbs <- coef(summary(model))["fbs", "Estimate"]

restecg <- coef(summary(model))["restecg", "Estimate"]

thalachh <- coef(summary(model))["thalachh", "Estimate"]

exng <- coef(summary(model))["exng", "Estimate"]

oldpeak <- coef(summary(model))["oldpeak", "Estimate"]

slp <- coef(summary(model))["slp", "Estimate"]

caa <- coef(summary(model))["caa", "Estimate"]

thall <- coef(summary(model))["thall", "Estimate"]

output <- coef(summary(model))["output", "Estimate"]

#use the model coefficients to predict the value for age

intercept + sex*0 + cp*3 + trtbps*150 + chol*200 + fbs*0 + restecg*1 + thalachh*150 + exng*0 +


oldpeak*2.0 + slp*1 + caa*0 + thall*1 + output*1

You might also like