Lecture 4: Introduction to Regression CS109A Introduction to Data - - PowerPoint PPT Presentation

β–Ά
lecture 4 introduction to regression
SMART_READER_LITE
LIVE PREVIEW

Lecture 4: Introduction to Regression CS109A Introduction to Data - - PowerPoint PPT Presentation

Lecture 4: Introduction to Regression CS109A Introduction to Data Science Pavlos Protopapas, Kevin Rader and Chris Tanner Background Roadmap: Lecture 1 What is Data Science? Lecture 2 Data: types, formats, issues, etc, and briefly


slide-1
SLIDE 1

CS109A Introduction to Data Science

Pavlos Protopapas, Kevin Rader and Chris Tanner

Lecture 4: Introduction to Regression

slide-2
SLIDE 2

CS109A, PROTOPAPAS, RADER, TANNER

Background

Roadmap:

1

What is Data Science? Data: types, formats, issues, etc, and briefly visualization How to quickly prepare data and scrape the web How to model data and evaluate model fitness. Linear regression, confidence intervals, model selection cross validation, regularization Lecture 1 Lecture 2 Lecture 3 and Lab2 This lecture Next 3 lectures

slide-3
SLIDE 3

CS109A, PROTOPAPAS, RADER, TANNER

Lecture Outline

Statistical Modeling k-Nearest Neighbors (kNN) Model Fitness How does the model perform predicting? Comparison of Two Models How do we choose from two different models?

2

slide-4
SLIDE 4

CS109A, PROTOPAPAS, RADER, TANNER

Predicting a Variable

Let's image a scenario where we'd like to predict one variable using another (or a set of other) variables. Examples:

  • Predicting the amount of view a YouTube video will get next week

based on video length, the date it was posted, previous number of views, etc.

  • Predicting which movies a Netflix user will rate highly based on their

previous movie ratings, demographic data etc.

3

slide-5
SLIDE 5

CS109A, PROTOPAPAS, RADER, TANNER

Data

TV radio newspaper sales 230.1 37.8 69.2 22.1 44.5 39.3 45.1 10.4 17.2 45.9 69.3 9.3 151.5 41.3 58.5 18.5 180.8 10.8 58.4 12.9

4

,

The Advertising data set consists of the sales of that product in 200 different markets, along with advertising budgets for the product in each of those markets for three different media: TV, radio, and newspaper. Everything is given in units

  • f $1000.

Some of the figures in this presentation are taken from "An Introduction to Statistical Learning, with applications in R" (Springer, 2013) with permission from the authors: G. James, D. Witten, T. Hastie and R. Tibshirani "

slide-6
SLIDE 6

CS109A, PROTOPAPAS, RADER, TANNER

Response vs. Predictor Variables

There is an asymmetry in many of these problems: The variable we'd like to predict may be more difficult to measure, is more important than the other(s), or may be directly or indirectly influenced by the values of the

  • ther variable(s).

Thus, we'd like to define two categories of variables:

  • variables whose value we want to predict
  • variables whose values we use to make our prediction

5

slide-7
SLIDE 7

CS109A, PROTOPAPAS, RADER, TANNER

Response vs. Predictor Variables

TV radio newspaper sales 230.1 37.8 69.2 22.1 44.5 39.3 45.1 10.4 17.2 45.9 69.3 9.3 151.5 41.3 58.5 18.5 180.8 10.8 58.4 12.9

6

Y

  • utcome

response variable dependent variable X predictors features covariates

p predictors n observations

slide-8
SLIDE 8

CS109A, PROTOPAPAS, RADER, TANNER

Response vs. Predictor Variables

TV radio newspaper sales 230.1 37.8 69.2 22.1 44.5 39.3 45.1 10.4 17.2 45.9 69.3 9.3 151.5 41.3 58.5 18.5 180.8 10.8 58.4 12.9

7

  • utcome

response variable dependent variable π‘Œ = π‘Œ#, … , π‘Œ& π‘Œ

' = 𝑦#', … , 𝑦)', … , 𝑦*'

predictors features covariates

p predictors n observations

𝑍 = 𝑧#, … , 𝑧*

slide-9
SLIDE 9

CS109A, PROTOPAPAS, RADER, TANNER

Definition

We are observing π‘ž + 1 number variables and we are making π‘œ sets of

  • bservations. We call:
  • the variable we'd like to predict the outcome or response variable;

typically, we denote this variable by 𝑍 and the individual measurements 𝑧).

  • the variables we use in making the predictions the features or

predictor variables; typically, we denote these variables by π‘Œ = π‘Œ#, … , π‘Œ& and the individual measurements 𝑦),'.

8

Note: 𝑗 indexes the observation (𝑗 = 1, … , π‘œ) and π‘˜ indexes the value of the π‘˜-th predictor variable (j = 1, … , π‘ž).

slide-10
SLIDE 10

CS109A, PROTOPAPAS, RADER, TANNER

Statistical Model

9

slide-11
SLIDE 11

CS109A, PROTOPAPAS, RADER, TANNER

True vs. Statistical Model

We will assume that the response variable, 𝑍, relates to the predictors, π‘Œ, through some unknown function expressed generally as: 𝑍 = 𝑔 π‘Œ + 𝜁 Here, 𝑔 is the unknown function expressing an underlying rule for relating 𝑍 to π‘Œ, 𝜁 is the random amount (unrelated to π‘Œ) that 𝑍 differs from the rule 𝑔 π‘Œ . A statistical model is any algorithm that estimates 𝑔. We denote the estimated function as 𝑔. 9

10

slide-12
SLIDE 12

CS109A, PROTOPAPAS, RADER, TANNER

Statistical Model

11

x y

slide-13
SLIDE 13

CS109A, PROTOPAPAS, RADER, TANNER

Statistical Model

12

What is the value of y at this 𝑦?

How do we find 𝑔 A 𝑦 ?

slide-14
SLIDE 14

CS109A, PROTOPAPAS, RADER, TANNER

Statistical Model

13

How do we find 𝑔 A 𝑦 ?

  • r this
  • ne?
slide-15
SLIDE 15

CS109A, PROTOPAPAS, RADER, TANNER

Statistical Model

14

Simple idea is to take the mean of all y’s, 𝑔 A 𝑦 = #

* βˆ‘ 𝑧) * #

slide-16
SLIDE 16

CS109A, PROTOPAPAS, RADER, TANNER

Prediction vs. Estimation

For some problems, what's important is obtaining 𝑔 A, our estimate of 𝑔. These are called inference problems. When we use a set of measurements, (𝑦),#, … , 𝑦),&) to predict a value for the response variable, we denote the predicted value by: 𝑧 E) = 𝑔 A(𝑦),#, … , 𝑦),&). For some problems, we don't care about the specific form of 𝑔 A, we just want to make our predictions 𝑧 E’s as close to the observed values 𝑧’s as possible. These are called prediction problems.

15

slide-17
SLIDE 17

CS109A, PROTOPAPAS, RADER, TANNER

Simple Prediction Model

16

What is 𝑧 EFat some 𝑦F ?

𝑦F

Predict 𝑧 EF = 𝑧& 𝑧 EF Find distances to all other points 𝐸(𝑦F, 𝑦)) Find the nearest neighbor, (𝑦&, 𝑧&) (𝑦&, 𝑧&)

slide-18
SLIDE 18

CS109A, PROTOPAPAS, RADER, TANNER

Simple Prediction Model

17

Do the same for β€œall” 𝑦′𝑑

slide-19
SLIDE 19

CS109A, PROTOPAPAS, RADER, TANNER

Extend the Prediction Model

18

What is 𝑧 EFat some 𝑦F ?

𝑦F

Predict 𝑧F J =

# K βˆ‘ 𝑧FL K )

𝑧 EF Find distances to all other points 𝐸(𝑦F, 𝑦)) Find the k-nearest neighbors, 𝑦FM, … , 𝑦FN

slide-20
SLIDE 20

CS109A, PROTOPAPAS, RADER, TANNER

Simple Prediction Models

19

slide-21
SLIDE 21

CS109A, PROTOPAPAS, RADER, TANNER

Simple Prediction Models

20

We can try different k-models on more data

slide-22
SLIDE 22

CS109A, PROTOPAPAS, RADER, TANNER

k-Nearest Neighbors

21

The k-Nearest Neighbor (kNN) model is an intuitive way to predict a quantitative response variable: to predict a response for a set of observed predictor values, we use the responses of other observations most similar to it Note: this strategy can also be applied in classification to predict a categorical variable. We will encounter kNN again later in the course in the context of classification.

slide-23
SLIDE 23

CS109A, PROTOPAPAS, RADER, TANNER

Λ† yn = 1 k

k

X

i=1

yni

k-Nearest Neighbors - kNN

22

For a fixed a value of k, the predicted response for the 𝑗-th

  • bservation is the average of the observed response of the k-

closest observations: where 𝑦*#, … , 𝑦*K are the k observations most similar to 𝑦) (similar refers to a notion of distance between predictors).

slide-24
SLIDE 24

CS109A, PROTOPAPAS, RADER, TANNER

ED quiz: Lecture 4 | part 1

23

slide-25
SLIDE 25

CS109A, PROTOPAPAS, RADER, TANNER

Things to Consider

Model Fitness How does the model perform predicting? Comparison of Two Models How do we choose from two different models? Evaluating Significance of Predictors Does the outcome depend on the predictors? How well do we know π’ˆ P The confidence intervals of our 𝑔 A

24

slide-26
SLIDE 26

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

25

slide-27
SLIDE 27

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

26

Start with some data.

slide-28
SLIDE 28

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

27

Hide some of the data from the model. This is called train-test split. We use the train set to estimate 𝑧 E, and the test set to evaluate the model.

slide-29
SLIDE 29

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

28

Estimate 𝑧 E for k=1 .

slide-30
SLIDE 30

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

29

Now, we look at the data we have not used, the test data (red crosses).

slide-31
SLIDE 31

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

30

Calculate the residuals (𝑧) βˆ’ 𝑧 E)).

slide-32
SLIDE 32

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

31

Do the same for k=3.

slide-33
SLIDE 33

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

32

In order to quantify how well a model performs, we define a loss or error function. A common loss function for quantitative outcomes is the Mean Squared Error (MSE): The quantity 𝑧) βˆ’ 𝑧 E) is called a residual and measures the error at the i-th prediction.

MSE = 1 n

n

X

i=1

(yi βˆ’ b yi)2

slide-34
SLIDE 34

CS109A, PROTOPAPAS, RADER, TANNER

Error Evaluation

33

Caution: The MSE is by no means the only valid (or the best) loss function! Question: What would be an intuitive loss function for predicting categorical

  • utcomes?

Note: The square Root of the Mean of the Squared Errors (RMSE) is also commonly used.

RMSE = √ MSE = v u u t 1 n

n

X

i=1

(yi βˆ’ b yi)2

slide-35
SLIDE 35

CS109A, PROTOPAPAS, RADER, TANNER

Things to Consider

Comparison of Two Models How do we choose from two different models? Model Fitness How does the model perform predicting? Evaluating Significance of Predictors Does the outcome depend on the predictors? How well do we know π’ˆ P The confidence intervals of our 𝑔 A

34

slide-36
SLIDE 36

CS109A, PROTOPAPAS, RADER, TANNER

Model Comparison

35

slide-37
SLIDE 37

CS109A, PROTOPAPAS, RADER, TANNER

Model Comparison

36

Do the same for all k’s and compare the RMSEs. k=3 seems to be the best model.

slide-38
SLIDE 38

CS109A, PROTOPAPAS, RADER, TANNER

Things to Consider

Comparison of Two Models How do we choose from two different models? Model Fitness How does the model perform predicting? Evaluating Significance of Predictors Does the outcome depend on the predictors? How well do we know π’ˆ P The confidence intervals of our 𝑔 A

37

slide-39
SLIDE 39

CS109A, PROTOPAPAS, RADER, TANNER

Model Fitness

38

slide-40
SLIDE 40

CS109A, PROTOPAPAS, RADER, TANNER

Model fitness

39

For a subset of the data, calculate the RMSE for k=3. Is RMSE=5.0 good enough?

slide-41
SLIDE 41

CS109A, PROTOPAPAS, RADER, TANNER

Model fitness

40

What if we measure the Sales in cents instead of dollars? RMSE is now 5004.93. Is that good?

slide-42
SLIDE 42

CS109A, PROTOPAPAS, RADER, TANNER

Model fitness

41

It is better if we compare it to something. Λ† y = 1 n

n

X

i

yi

We will use the simplest model:

slide-43
SLIDE 43

CS109A, PROTOPAPAS, RADER, TANNER

R-squared

  • If our model is as good as the mean value, 𝑧

R, then 𝑆T = 0

  • If our model is perfect then 𝑆T = 1
  • 𝑆T can be negative if the model is worst than the average. This can

happen when we evaluate the model in the test set.

42

R2 = 1 βˆ’ P

i(Λ†

yi βˆ’ yi)2 P

i(Β―

y βˆ’ yi)2

slide-44
SLIDE 44

CS109A, PROTOPAPAS, RADER, TANNER

Summary

Comparison of Two Models How do we choose from two different models? Model Fitness How does the model perform predicting? Evaluating Significance of Predictors Does the outcome depend on the predictors? How well do we know π’ˆ P The confidence intervals of our 𝑔 A

43

slide-45
SLIDE 45

CS109A, PROTOPAPAS, RADER, TANNER

Summary

Model Fitness How does the model perform predicting? Comparison of Two Models How do we choose from two different models? Evaluating Significance of Predictors Does the outcome depend on the predictors? How well do we know π’ˆ P The confidence intervals of our 𝑔 A

44

Next lecture