Docsity

Logistic Regression and Non-parametric Regression: Lecture Notes, Lecture notes of Statistics

Partial preview of the text

Download Logistic Regression and Non-parametric Regression: Lecture Notes and more Lecture notes Statistics in PDF only on Docsity!

Lecture 11: Logistic Regression

and Non-parametric Regression

Outline

Lecture 11: Logistic Regression and Non-parametric Regression

Example – Menarche

Example – Odds of an Event

Theory – Logistic Regression

Example – Plotting Fitted Values from Logistic Regression

Example – Interpretation on Logistic Model

Example – Child lung function data

Theory – Non-parametric regression: LOESS

Example – LOESS with different smoothing parameter

Example – Menarche

plot(Menarche/Total ~ Age, data=menarche)

l l l l^ l^ l^

ll^ l

l l l l

l

l (^) ll

l l^ l^ l^ l^ l^ l^ l

10 12 14 16

Age

Menarche/Total

What is the best model to fit to this data?

Example – Odds of an Event

The odds of an event is defined as

odds =

p

1 − p

where p is the probability of an event occurring.

odds =

probability that event will occur

probability that event will not occur

Example – Odds of an event

I Suppose that the probability that your favourite team wins a

football game is 0.6 (i.e. p = 0. 6 ).

I Then the odds that your favourite team will win the game is 3

to 2:

odds =

Theory – Logistic Function

The logistic function is given

as

f (x) =

1 + e−x

−6 −4 −2 0 2 4 6

0.^ 0.^ 0.^ 0.^ 0.^

The Standard Logistic Function

x

I The fitted regression model is given as

logit(ˆpi) = ln

pˆi

1 − pˆi

= βˆ 0 + βˆ 1 xi 1 +... + βˆkxik.

I Taking the exponential of both sides and rearranging we get

p ˆi =

1 + e−( βˆ^0 + βˆ^1 xi^1 +...+ βˆk^ xik^ )^

i.e. it is in the same form of a logistic function!

Example – Logistic Regression

tab <- with(menarche, cbind(Menarche, Total - Menarche))

M1 <- glm(tab ~ Age, family="binomial", data=menarche)

I glm is the function used to perform generalized linear models.

I The family="binomial" in glm fits a logistic regression.

I glm takes a data.frame (or matrix) in which the first

column is number of successes and the second column is

number of failures.

Example – Plotting Fitted Values from Logistic Regression

plot(Menarche/Total ~ Age, data=menarche, pch=19, col="navy") lines(menarche$Age, M1$fitted, type="l", col="red") title(main="Menarche Data with Fitted Logistic Regression Line")

l l l l^ l^ l^

ll^ l

l l

l l

l

l (^) ll

l l^ l^ l^ l^ l^ l^ l

10 12 14 16

Age

Menarche/Total

Menarche Data with Fitted Logistic Regression Line

Example – Interpretation for Logistic Model

summary(M1)

....

Estimate Std. Error z value Pr(>|z|)

(Intercept) -21.22639 0.77068 -27.54 <2e-16 ***

Age 1.63197 0.05895 27.68 <2e-16 ***

....

I The fitted model is

logit(ˆpi) = ln

ˆpi

1 − pˆi

= − 21 .336 + 1. 632 · Age.

I For each increase of one year in age, the natural log of the

odds of having reached menarche increase by 1. 632 (or

alternatively, the odds of having reached menarche increase by

(e^1.^632 ) ≈ 5. 11 times).

Example – Inference for Logistic Model

summary(M1)

....

Estimate Std. Error z value Pr(>|z|)

(Intercept) -21.22639 0.77068 -27.54 <2e-16 ***

Age 1.63197 0.05895 27.68 <2e-16 ***

....

I z value is given by z∗^ =

βˆj

SE( βˆj )

I For large sample size, n, we assume z∗^ ∼ N (0, 1) and so for

H 0 : βj = 0, the p-value is given by P (|Z| > |z∗|).

I Why don’t we use a t-test?

Example – Inference for Logistic Model

summary(M1)

....

Estimate Std. Error z value Pr(>|z|)

(Intercept) -21.22639 0.77068 -27.54 <2e-16 ***

Age 1.63197 0.05895 27.68 <2e-16 ***

....

I z value is given by z∗^ =

βˆj

SE( βˆj )

I For large sample size, n, we assume z∗^ ∼ N (0, 1) and so for

H 0 : βj = 0, the p-value is given by P (|Z| > |z∗|).

I Why don’t we use a t-test? We ought to but this is not

straight forward for logistic regression so care should be taken

for small n.

Example – Child lung function

I FEV (forced expiratory

volume) is a measure of lung

function.

I The data include

determinations of FEV on

318 female children who

were seen in a childhood

respiratory disease study in

Massachusetts in the U.S.

I Child’s age (in years) also

recorded.

I Of interest to model FEV by

age.

Reference: Tager et al. (1979). American J of Epidemiology, 110 , 15-26.

Example – Child lung function

dat = read.table(file = "../datasets/fev.txt", header = T)
plot(FEV ~ Age, data = dat, pch = 19)
M1 = lm(FEV ~ Age, data=dat)
abline(coef(M1), col="blue")
M2 = loess(FEV ~ Age, data=dat)
lines(sort(dat$Age), predict(M2, data.frame(Age=sort(dat$Age))), col="red")

l l l

l l l

l (^) l l

l l ll l

l l

l

l

l l l

l

l

l l

l l

ll l

l l

l

l (^) l l l

l l

l

l l l l

l

ll

l

l

ll

l

l l

l l l

l l l l

l l

l l

l

l

l ll

l l (^) l

l

l l l l

l

ll

ll

l l (^) l

l l

l

l

l

l

l

l l

l l l

l l

l l

l

l

l

l l l l l

l l l

l

l

l

l

l l

l l

l

l

l

l (^) l l

ll

l l

ll ll l

l

l

ll l l l

l l

l (^) l

l

l

l

l

l

l (^) l

l

l

l l l

l

l l

l l

l l l

l l

l l l l

l l l

l

l

ll

l

l

l

l (^) l l

l

l l l

l

l

l

l

l

l

l l (^) l ll l l

l

l

l l

l l l

l

ll l

l l l

l l l l l

l

l l

l l l

l l

l

ll

l

l l

l

l

l

l

l

l l l l

l l

l

l l l

ll

l

l

l (^) ll l ll l

l l l

l

l

l l l l l

l ll l

ll l l

l l

l l l l

l l l

l l l

l l

ll l

l

ll l

l l

l l l

l l ll l

l l ll

l l

l

5 10 15

Age

FEV

Example – LOESS with different smoothing parameter

l l l

l l l

l (^) l l

l l ll

l

l l

l

l

l

l l

l

l

l

l

l

l

l

l

l

l l

l

l (^) l l l

l l

l

l l

l

l

l

l

l

l

l

ll

l

l l

l

l l

l l l

l

l l

l

l

l

l

l l l

l l (^) l

l

l l l l

l

l l

ll

l

l (^) l

l

l

l

l

l

l

l

l l

l l l

l l

l l

l

l

l

l

l l l l

l l l

l

l

l

l

l l

l l

l

l

l

l (^) l l

ll

l l

ll ll l

l

l

l l

l l

l

l

l

l (^) l

l

l

l

l

l

l (^) l

l

l

l l

l

l

l l

l l

l l

l

l l

l

l

l l

l

l

l

l

l

ll

l

l

l

l l

l

l

l

l l

l

l

l

l

l

l

l

l l l l l

l

l

l

l

l

l

l l

l

l l l

l l l

l l l

l

l

l

l l

l l l

l l

l

ll

l

l l

l

l

l

l

l

l l l

l

l l

l

l l l

l

l

l

l

l

lll (^) l l l

l

l l

l

l

l l l

l l

l

l l

l

l l l l

l l

l l l

l

l l

l

l l

l

l

l

ll l

l

ll l

l l

l l l

l

l

l

l

l

l l

ll

l l

l

5 10 15

Age

FEV

LS span=0. span=0. span=

Example – LOESS with different smoothing parameter

dat = read.table(file = "../datasets/fev.txt", header = T) plot(FEV ~ Age, data = dat, pch = 19) M1 = lm(FEV ~ Age, data=dat) abline(coef(M1), col="blue") M2 = loess(FEV ~ Age, data=dat) lines(sort(dat$Age), predict(M2, data.frame(Age=sort(dat$Age))), col="red") M3 = loess(FEV ~ Age, data=dat, span=1/3) lines(sort(dat$Age), predict(M3, data.frame(Age=sort(dat$Age))), col="green") M4 = loess(FEV ~ Age, data=dat, span=1) lines(sort(dat$Age), predict(M4, data.frame(Age=sort(dat$Age))), col="black") legend("topleft", c("LS", "span=0.75", "span=0.33", "span=1"), lty=1, col=c("blue", "red", "green", "black"))