lesson 2: linear regression - wordpress.com › 2016 › 02 › ... · learn about linear...

30
Lesson 2: Linear Regression

Upload: others

Post on 09-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

Lesson 2: Linear Regression

Page 2: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

Learn about linear regression

This Lesson’s Goals

Summarise results in an R Markdown document

Do a linear regression in R

Make a figure for data from a linear regression

Page 3: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

Math

Page 4: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

yi = specific y value (dependent variable)

a = intercept

b = slope

xi = specific x value (independent variable)

ei = random variance or the residual

Page 5: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

● ●

●●

●● ●

● ● ●

● ● ●

●●

●●

●● ●

●●

●● ●

● ● ●

●●

●● ● ● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ● ● ● ● ●● ●

●●

●●

● ●●

●●

● ●

●●

●●

● ● ● ●

● ●

● ●

●●

●●

● ●

0

100

200

300

0 5 10 15 20Time

Wei

ght i

n gr

ams

Chick Weight Over Time

yi = a + bxi + ei

Page 6: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

● ●

●●

●● ●

● ● ●

● ● ●

●●

●●

●● ●

●●

●● ●

● ● ●

●●

●● ● ● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ● ● ● ● ●● ●

●●

●●

● ●●

●●

● ●

●●

●●

● ● ● ●

● ●

● ●

●●

●●

● ●

0

100

200

300

0 5 10 15 20Time

Wei

ght i

n gr

ams

Chick Weight Over Time

yi = a + bxi + ei

Page 7: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

● ●

●●

●● ●

● ● ●

● ● ●

●●

●●

●● ●

●●

●● ●

● ● ●

●●

●● ● ● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ● ● ● ● ●● ●

●●

●●

● ●●

●●

● ●

●●

●●

● ● ● ●

● ●

● ●

●●

●●

● ●

0

100

200

300

0 5 10 15 20Time

Wei

ght i

n gr

ams

Chick Weight Over Time

yi = a + bxi + ei

Page 8: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

● ●

●●

●● ●

● ● ●

● ● ●

●●

●●

●● ●

●●

●● ●

● ● ●

●●

●● ● ● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ● ● ● ● ●● ●

●●

●●

● ●●

●●

● ●

●●

●●

● ● ● ●

● ●

● ●

●●

●●

● ●

0

100

200

300

0 5 10 15 20Time

Wei

ght i

n gr

ams

Chick Weight Over Time

yi = a + bxi + ei

Page 9: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

● ●

●●

●● ●

● ● ●

● ● ●

●●

●●

●● ●

●●

●● ●

● ● ●

●●

●● ● ● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ● ● ● ● ●● ●

●●

●●

● ●●

●●

● ●

●●

●●

● ● ● ●

● ●

● ●

●●

●●

● ●

0

100

200

300

0 5 10 15 20Time

Wei

ght i

n gr

ams

Chick Weight Over Time

yi = a + bxi + ei

Page 10: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

● ●

●●

●● ●

● ● ●

● ● ●

●●

●●

●● ●

●●

●● ●

● ● ●

●●

●● ● ● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ● ● ● ● ●● ●

●●

●●

● ●●

●●

● ●

●●

●●

● ● ● ●

● ●

● ●

●●

●●

● ●

0

100

200

300

0 5 10 15 20Time

Wei

ght i

n gr

ams

Chick Weight Over Time

yi = a + bxi + ei

Page 11: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

● ●

●●

●● ●

● ● ●

● ● ●

●●

●●

●● ●

●●

●● ●

● ● ●

●●

●● ● ● ●

●● ●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●

● ● ● ● ● ●● ●

●●

●●

● ●●

●●

● ●

●●

●●

● ● ● ●

● ●

● ●

●●

●●

● ●

0

100

200

300

0 5 10 15 20Time

Wei

ght i

n gr

ams

Chick Weight Over Time

yi = a + bxi + ei

Page 12: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

categorical?

yi = a + bxi + ei

Page 13: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

continuouspredictors

categoricalpredictors

x = a set of continuous data points

a + bx

x = a set of binary/categorical data points

a = the value of y when x is 0

a = the value of y when the x is the default level

b = the change in y for one change in x

b = the change in y when x is the non-default level

Page 14: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 15: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 16: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 17: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 18: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 19: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 20: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 21: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 22: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

●●

●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

900

950

1000

1050

1100

high lowProficiency in L2

Rea

ctio

n tim

es in

ms

Reaction Times by L2 Proficiency Level

0 1

Page 23: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

R Code

Page 24: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

lm(weight ~ Time)

Page 25: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

yi = a + bxi + ei

lm(rt ~ type)

Page 26: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

Lab

Page 27: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

Dataset: Baby names per year from USA Social Security Administration

Continuous Predictor: Does your name get more or less popular between the years of 1901 and 2000?

Categorical Predictor: Is your name more or less popular with females or males?

yi = a = b = xi = ei =

frequency of name? - will get from model? - will get from modelyear? - will get from model

yi = a = b = xi = ei =

frequency of name? - will get from model? - will get from modelsex? - will get from model

Continuous Predictor Categorical Predictor

source: R package “babynames”

Page 28: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

●●

●●

−5.5

−5.0

−4.5

1925 1950 1975 2000Year

Prop

ortio

n of

Peo

ple

(log

base

10

trans

form

ed)

Proportion of People withthe Name 'Page' Over Time

End of Lesson Food for Thought

Page 29: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

●●

●●

−5.5

−5.0

−4.5

1925 1950 1975 2000Year

Prop

ortio

n of

Peo

ple

(log

base

10

trans

form

ed)

Proportion of People withthe Name 'Page' Over Time

End of Lesson Food for Thought

Page 30: Lesson 2: Linear Regression - WordPress.com › 2016 › 02 › ... · Learn about linear regression This Lesson’s Goals Summarise results in an R Markdown document Do a linear

●●

●●

●●

●●

●●

−5.5

−5.0

−4.5

1925 1950 1975 2000Year

Prop

ortio

n of

Peo

ple

(log

base

10

trans

form

ed)

Proportion of People withthe Name 'Page' Over Time

End of Lesson Food for Thought