You are on page 1of 11

LINEAR REGRESSION

names(LungCapData)

attach(LungCapData)

plot(Age, LungCap, main = "scatterplot")

cor(Age, LungCap)

help(lm)

model = lm(LungCap ~ Age)

summary(model)

attributes(model)

model$coefficients

abline(model, col = "RED")

confint(model, level = 0.99)

anova(model)
NON CONSTANT VARIANCE
NON LINEAR RELATIONSHIP
MULTIPLE REGRESSIONS

names(LungCapData)

attach(LungCapData)

model = lm(LungCap~ Age + Height)

summary(model)

cor(Age, Height, method = "pearson")

confint(model, conf.level = 0.95)

model2 = lm(LungCap~ Age + Height + Smoke + Gender + Caesarean)

summary(model2)

plot(model2)

CATEGORY

attach(LungCapData)

View(LungCapData)
?cut

class(Height)

Height

min(Height)

max(Height)

cutheight = cut(Height, breaks = c(0,50,55,60,65,70,100), labels =c("A","B","C","D","E","F"))

cutheight

Height[1:10]

cutheight[1:10]

cutheight = cut(Height, breaks = c(0,50,55,60,65,70,100), labels =c("A","B","C","D","E","F"), right =


FALSE)

Height[1:10]

cutheight[1:10]

mean(LungCap[cutheight == "A"])

mean(LungCap[cutheight == "B"])

mean(LungCap[cutheight == "C"])

mean(LungCap[cutheight == "D"])

mean(LungCap[cutheight == "E"])

mean(LungCap[cutheight == "F"])

model = lm(LungCap ~ cutheight)

summary(model)

FOR CATEGORY VARIABLE

attach(LungCapData)

names(LungCapData)

dim(LungCapData)

levels(Smoke)
Smoke = c(Smoke)

Smoke = factor(Smoke)

levels(Smoke)

model = lm(LungCap ~ Age + Smoke)

summary(model)

table(Smoke)

Smoke = relevel(Smoke, ref = "yes")

table(Smoke)

model = lm(LungCap ~ Age + Smoke)

summary(model)

CATEGORICAL VARIABLES

attach(LungCapData)

View(LungCapData)

?cut

class(Height)

cutheight = cut(Height, breaks = c(0,50,55,60,65,70,100), labels =c("A","B","C","D","E","F"))

cutheight

Height[1:10]

cutheight[1:10]

cutheight = cut(Height, breaks = c(0,50,55,60,65,70,100), labels =c("A","B","C","D","E","F"), right =


FALSE)

Height[1:10]

cutheight[1:10]

mean(LungCap[cutheight == "A"])

mean(LungCap[cutheight == "B"])

mean(LungCap[cutheight == "C"])
mean(LungCap[cutheight == "D"])

mean(LungCap[cutheight == "E"])

mean(LungCap[cutheight == "F"])

model = lm(LungCap ~ Age + cutheight)

summary(model)

a1 =Age[cutheight == "A"]

b1 =LungCap[cutheight == "A"]

a2 =Age[cutheight == "B"]

b2 =LungCap[cutheight == "B"]

a3 =Age[cutheight == "C"]

b3 =LungCap[cutheight == "C"]

a4 =Age[cutheight == "D"]

b4 =LungCap[cutheight == "D"]

a5 =Age[cutheight == "E"]

b5 =LungCap[cutheight == "E"]

a6 =Age[cutheight == "F"]

b6 =LungCap[cutheight == "F"]

plot(a1,b1, col = 2, ylim = c(0,15), xlim = c(0,20), xlab ="AGE", ylab = "LungCap" )

points(a2,b2,col = 3)

points(a3,b3,col = 4)

points(a4,b4,col = 5)

points(a5,b5,col = 6)

points(a6,b6,col = 7)
abline(a=0.99,b=0.20,col = 2, lwd = 3)

abline(a=2.47,b=0.20,col = 3, lwd = 3)

abline(a=3.66,b=0.20,col = 4, lwd = 3)

abline(a=4.91,b=0.20,col = 5, lwd = 3)

abline(a=6,b=0.20,col = 6, lwd = 3)

abline(a=7.57,b=0.20,col = 7, lwd = 3)