Loading [MathJax]/jax/output/CommonHTML/jax.js

Lecture_13__Simple_Linear_Regression_in_Matrix_Format_25_02_18__ (3).pdf
0.30MB

Regression modeling with a two-level categorical variable

Suppose that Z is a two-level categorical variable such that Z = A or B.

Define

X={1,if Z=A0,otherwise

 

Then we can use the following regression model, Y=β0+β1X+ϵ

  • β0=μB(called the base line)
  • β1=μAμB
  • Consequently, β0+β1=μA

Since E(Y)=β0+β1X,

if Z = A, X = 1, E(Y)=β0+β1=μA

if Z = B, X = 0, E(Y)=β0=μB

 

 

Suppose that Z is a three-level categorical variable such that Z = A, B or C. 

Define

X1={1,if Z=A0,otherwise

 

X2={1,if Z=B0,otherwise

 

Then we can use the following regression model, y=β0+β1X1+β2X2+ϵ

  • β0=μC (called the base line)
  • β1=μAμC
  • β2=μBμC

 

Since E(Y)=β0+β1X1+β2X2,

if Z = A, (1, 0), E(Y)=β0+β1=μA

if Z = B, (0, 1), E(Y)=β0+β2=μB

if Z = C, (0, 0), E(Y)=β0=μC

 

 

Two categorical variables

Consider two categorical variables: One at 3 levels (F1,F2,F3) and the other at 2 levels (B1,B2).

Then, the model can be written as Y=β0+β1X1+β2X2+β3X3+ϵ,

where

X1=1ifF2X1=0,ifnot

X2=1ifF3X2=0,ifnot

X3=1ifB2X3=0,ifnot

 

Note that F1 and B1 : base levels

  • β0=μ11 (mean of combination of base levels)
  • β1=μ2jμ1j for any level Bj (j = 1, 2)
  • β2=μ3jμ1j for any level Bj (j = 1, 2)
  • β3=μi2μi1 for any level Fi (i = 1, 2, 3)

 

Interaction model with two categorical variables 

Consider an extended model as follows:

Y=β0+β1X1+β2X2+β3X3+β4X4+β5X5+ϵ,

where 

X1=1ifF2X1=0,ifnot

X2=1ifF3X2=0,ifnot

X3=1ifB2X3=0,ifnot

X4=X1X3,andX5=X2X3

 

Note that F1 and B1 : Base levels.

  • β0=μ11 (mean of combination of base levels)
  • β1=μ21μ11 for any level B1 only
  • β2=μ31μ11 for any level B1 only
  • β3=μ12μ11 for any level F1 only 
  • β4=(μ22μ12)(μ21μ11)
  • β5=(μ32μ12)(μ31μ11)

 

Since F2, B1, μ21=β0+β1 then we can write β1=μ21μ11.

 

 

Example(Two categorical variables with interaction)

 

이걸 보고 우리가 질문할 수 있는 것은 다음과 같습니다. 

  1. interaction이 유의한가요?
    • H0:β4=β5=0 vs. H1:β40orβ50
    • SAS에서 추가적인 옵션이 test를 걸어줘서 확인을 해도 되나, T-test에서 유추가 가능합니다. 
  2. interaction이 없는 모델과 비교할 때는 R2a을 비교합니다. 

결론: 범주형에 대한 회귀분석을 진행할 때도 interaction을 고려해볼 수 있다는 것입니다. 

 

 

Needs for transformations

Transformations are applied to accomplish certain objectives such as 

  • to ensure that the model is correctly specified,
  • to achieve normality,
  • or to stabilize the variance.

 

 

Example : Bacteria deaths due to X-ray radiation

nt=β0+β1+ϵt

 

9개의 plot 중에 첫 번째 plot을 통해 Lack of fit(E(ϵ)0) 문제를 확인할 수 있습니다.

따라서 X 변환을 통해 이 문제를 해결해주면 좋을 듯 합니다. 

그렇기에 우리가 처음 썼던 식에서 조금 변형을 해주면 다음과 같이 쓸 수 있습니다.

nt=β0+β1t+β2t2+ϵt

그런데 첫 번째 그림에서 Lack of fit의 문제는 어느 정도 해결된 듯 보이나 

unequal variance 문제가 발생하였습니다. 

따라서 이 문제를 해결하기 위해 Y 변환을 고려하는 것이 좋을 듯 합니다. 

 

생물학에서는 Bacteria deaths due to X-ray radiation 문제의 식이 따로 존재합니다. 

nt=n0eαt

 

따라서 이 식을 통해 다음과 같은 식을 유도할 수 있습니다.

lognt=logn0+αt

 

이 식은 lognt를 Y로 생각하고 logn0를 \beta_0, αtβ1X로 생각하면 선형회귀의 식으로 이해할 수 있습니다. 

 

따라서 lognt=β0+β1t+ϵt의 값으로 fitting을 하면 

 

네 이렇게 해서 lack of fit과 unequal variance에 대한 문제를 해결했습니다. 

3번째 그림을 통해 inferential observation을 관찰한 결과, 영향점은 없는 것으로 확인되고 있습니다. 

qqplot을 통해서 Normality도 보장되고 있다는 것을 확인할 수 있습니다. 

그럼 잔차의 독립성을 위해 Durbin-Watson 검정을 시행하면 

다음과 같이 나옵니다. 

Durbin-Watson D의 값이 2보다 크기 때문에 Pr > DW 부분의 p-value값을 통해 검정을 시행해보면

We fail to reject the null이기에 H0를 기각하지 못합니다.

그렇다고 안 좋은 게 아닙니다. 절대! 

Durbin-Watson 검정은 H0를 기각하지 않아야 잔차의 독립성을 만족한다고 생각하기에 

위의 값을 통해 잔차의 독립성이 만족된다는 것을 확인할 수 있습니다. 

 

따라서 저희는 이렇게 하여 회귀분석 4가지 가정을 모두 만족시켰습니다. 

 

 

또 다른 얘기를 위해 한 가지 예제를 더 들겠습니다. 

 

Example : Supervisors and supervised workers in industrial establishments

여기서 R-square가 0.7759가 나왔는데 사실 field에 따라 결정계수의 높고 낮음의 기준은 다르기에 field에 따라 결정계수의 값이 높다 나쁘다가 결정됩니다. 

 

여기서도 마찬가지로 첫 번째 그림에서 Lack of fit과 unequal variance가 의심이 됩니다. 

따라서 X, Y 변환을 고려해야 합니다. 

X의 제곱근을 더해주고, boxcox 변환을 통해 Y변환을 취해 줍니다. 

여기서 만약 선형모형이 맞다고 생각이 들더라도 Lack of fit이 의심되면 2차를 오버피팅하는 습관은 아주 좋습니다.

어차피 선형모형이 맞다고 하면 X의 제곱의 ˆβ = 0 으로 나올 것이기 때문입니다. 

 

현재 \lamda = 0이 나왔기에 boxcox 변환할 때 log 변환을 취해주고 fitting을 해주면

 

위와 같은 결과가 나옵니다. 

여기서 그림을 살펴보면 기본 4가지 가정 중 3가지는 만족하는 것으로 보입니다만, 

inferential point가 2개 있는 것을 할 수 있습니다. 

 

일단 이 결과를 바탕으로 Durbin-Watson 검정을 시행해주면

위와 같은 결과가 나오기에 We fail to reject the null 이기에 잔차의 독립성은 만족됩니다.

 

 

Sales = β0 + β1youtube + β2facebook + β3newspaper + ϵi

Q1. Write the assumptions we impose on ϵ
>> ϵ ~ iid N(0, σ2)


Q2. Using the OLS method, estimate regression coefficients.
^β0 = 3.52667
^β1 = 0.04576
^β2 = 0.18853
^β3 = -0.00104


Q3. Estimate σ2
4.09096

없으면 root MSE 제곱해서 답 적기(2.022612)
SSE / DF = MSE


Q4. Test the overall utility of the model.
H0:β1=β2=β3=0
H1:βj0 for some j = 1,2,3

Let α=0.05
Since p-value =< 0.0001 is less than α=0.05, we reject H0.
Thus, the overall model is useful.


Q5. Can we claim that the facebook advertising is related to sales? Justify.
H0:β2=0
H1:β20

Let α = 0.05.
Since p-value =< 0.0001 is less than α = 0.05, we reject H0.
That is, we can claim that the facebook advertising is related to sales.


Q5-1. Can we claim that the newspaper advertising is related to sales? Justify.
H0: β3=0
H1: β30

Let α = 0.05.
Since p-value = 0.8599 is greater than α = 0.05, we fail to reject H0.
That is, we can claim that the facebook advertising is related to sales.
This means that the newspaper advertising is not related to sales.


Q6. Compute the expected change in the sales when our spending for youtube advertisement increases by 1000달러 while the others are fixed. 
45.76달러(0.04576 * 1000달러)


Q7. Compute the expected sale when we spend 1000달러 for each method of adv.
^Sale
45.76 + 188.53 + 3526.67 - 1.04= 3.7599 * 1000

Q7-1. Compute the expected sale when we spend 0달러 for all of adv.
x들의 값이 0일 때의 기댓값이 위의 질문에 대한 대답
β0 => 3.52667 * 1000달러

β0는 실제의 데이터 변수의 성질에 따라 x들의 값을 0으로 둘 수 있는지 없는지가 갈린다. 

Q8. Can we argue that youtube is negatively related to the sale?
1. youtube랑 sales와 관계가 있는지부터 확인(β)
2. 그러고 난 다음에 관계가 있으면 ˆβ의 값을 신뢰할 수 있음. 
   왜냐하면 β1의 값이 0이 아니라고 1번에서 결론지었기 때문에

We conclude that β10.
However, since ^β1 = 0.04576 is greater than 0, we cannot argue that...

Q8-1. Can we argue that newspaper is negatively related to the sale?
We conclude that β3 = 0. We cannot argue that ... 


Q9. Compute the R-squared and interpret it.
R2 = 0.8972

89.72% variability in sales can be explained 
by the fitted model (with youtube, facebook, and newspaper.)


Q9-1. adj. R-squared 도 마찬가지.

'통계학 > 회귀분석(Regression Analysis)' 카테고리의 다른 글

Matrix format  (0) 2025.02.18
Qualitative variables as predictors  (0) 2024.11.22
Transformation of variables  (0) 2024.11.19
단순선형회귀 (Simple linear regression)  (3) 2024.09.25

(학부생이라 오류가 있을 수 있습니다. 댓글로 정정해서 남겨주시면 감사드리겠습니다.)


 

단순선형회귀는 input이 하나이고 이 input을 통해 y값을 예측하는 모형입니다.

input이 만약에 여러 개면 Mutiple linear regression이라고 하는데 이 부분은 다음 포스팅 때 다뤄보도록 하겠습니다.

 

단순선형회귀는 독립변수 하나와 종속변수의 관계를 관측할 수 있게 해주는 설명력이 높은 통계적 방법입니다. 

회귀분석의 첫 단추를 끼우는 만큼 단순선형회귀에 대해 제가 배운 내용을 바탕으로 설명을 해보겠습니다. 

 

Simple linear regression model

  • The response variable Y 와 the predictor variable XY=β0+β1X+ϵ, where ϵ is a random error with E(ϵ)=0. (Population 수준의 모델)

 

Simple regression model with the observed data

Observation Number Response Variable Y Predictor X
1 y1 x1
2 y2 x2
.
.
.
.
.
.
.
.
.
.
.
.
n yn xn
  • The regression model for the observed data is yi=β0+β1xi+ϵi,i=1,2,....,n, where ϵi represents the error in yi.

 

Parameter estimation

  • To estimate the unknown regression coefficients, β0 and β1, the ordinary least squares(OLS) method is commonly used. 
  • From the regression model, we can write ϵi=yiβ0β1xi,i=1,2,....,n.
  • We estimate β0 and β1 by minimizing S(β0,β1)=ni=1(yiβ0β1x1)2 >> 가장 오른쪽에 있는 식은 Convex function이어서 미분 최솟값 구하면 됩니다.  

우리가 parameter 값을 구하고 싶은데, 현실은 observed data의 X와 Y값 만을 알고 있는 상태입니다. 

그렇기 때문에 우리는 parameter 값을 추정하는 겁니다.  실제 X값과 Y값을 통해 구할 수 있는데,

추정은 다음과 같은 식을 통해 구할 수 있습니다. 

OLS estimate 

  • It can be shown that the estimates of \beta_0 and \beta_1 that minimize S(β0,β1) are given by ^β1=ni=1(yi¯y)(xi¯x)ni=1(xi¯x)2 and ^β0=¯y^β1¯x
  • ^β0=¯y^β1¯x 증명

  • ^β1=ni=1(yi¯y)(xi¯x)ni=1(xi¯x)2 증명

 

이렇게 하여 ^β0^β1을 구할 수 있는데,

여기서 ^ 을 취한 이유는 True 값(추정치가 아닌 값)을 모르고 추정치만 알고 있어서 ^을 취했습니다.

 

 

Fitted values

  • The OLS regression line is obtained as ˆY=^β0+^β1X.
  • The i-th fitted value is given by ^yi=^β0+^β1xi,i=1,2,...,n.
  • Example.

 

 

이렇게 fitted 했다면 해석은 어떻게 해야 하는지 궁금할 수 있을 것 같은데요... 

그런데 그 전에 Y=β0+β1X+ϵ 이 식 양변에 평균을 취하게 되면 다음과 같이 나옵니다.

E(Y)=β0+β1X.

왜 이렇게 나오냐면 E(β0+β1X+ϵ) 에서 β0+β1X는 이미 값을 알고 있고 그렇기 때문에 상수로 처리됩니다. 그러면 자연스럽게 β0+β1X+E(ϵ) 이 되고, 회귀분석에서는 E(ϵ)=0이라고 하는 아주 중요한 가정이 있기에 자연스럽게 E(Y)=β0+β1X 이 식이 유도가 됩니다. 

 

Interpertation of coefficients

  • Recall that E(Y)=β0+β1X.
    - β0 is the expected value of Y when X=0.
    - β1 is the amount of increase in the expected value of Y for every one-unit increase inn X.
  • For example E(minutes) = 4.162 + 15.509 × Units.
    - The average length of calls is the 4.162 minutes when no component needs to be repaired.
    - The average length of calls increases by 15.509 minutes for each additional component that has to be repaired. 

 

Test of hypothesis

  • In the simple linear regression analysis, the usefulness of the predictor(= X) can be tested by using the following hypothesis test:
    H0:β1=0versusH1:β10.
    (X와 Y의 linear relationship을 β1을 체크하여 확인할 수 있습니다.)
    (주의할 점: ^β1을 이용해서 가설검정을 하는 것이 아닙니다.)
  • To this end, we need to further assume that

 

 

이렇게 간단한 식에 4가지의 가정이 들어가있습니다. 중요하기에 반드시 알아두는게 좋다고 합니다.

1. E(ϵi)=0

2. Var(ϵi)=σ2

3. ϵi ~ Normal

4. ϵ1,.....ϵn are independent.

 

 

T-test

  • Under H0 : β1=0, it can be shown that T=^β1s.e.(^β1) follows a Student's t distribution with n - 2 degrees of freedom, where s.e.(^β1)=ni=1(yi^yi)2/(n2)ni=1(xi¯x)2.

Using t-distribution, we can compute the p-value. At significant level α=0.05, we reject H0 if the p-value 0.05. Otherwise, we fail to reject H0.

 

보통은 모델이 유용하기를 바라기 때문에 귀무가설을 기각하기를 원합니다. 

 

 

앞서말한 계수들 뿐만 아니라 σ2를 추정하는 것도 중요한데요

바로 error의 변동성을 설명하기 위함입니다.

 

Estimation of σ2

  • Define ei=yi^yi,i=1,2,....,n which are called the residuals.
    이렇게 하는 이유는 ϵi=yiβ0β1xi를 
  • We can estimate σ2 by using ^σ2=ni=1e2in2=i=1n(yi^yi)2n2 MSE
    where i=1n(yi^yi)2 is referred to as SSE (Sum of Squares of Errors) and n -2 is called the df(degrees of freedom).
    여기서 n은 전체 데이터이고 2는 추정치의 개수입니다. 추정치는 β0,β1으로 2개가 존재했습니다. 그렇기에 2를 빼주는 것입니다. 

 

SAS라는 통계 프로그램을 통해서 MSE를 관측할 수 있는데 밑에 그림의 빨간색 부분이 MSE입니다. 

 

MSE in SAS

 

 

Confidence intervals 

  • The (1 - α) × 100% confidence intervals (or limits) for β0andβ1 are given by ^β0±tn2,α/2×s.e.(^β1),
    where tn2,α/2 is the (1 - α / 2) percentile of a t - distribution with n - 2 df

Construting Cls for coefficients in SAS

 

이렇게 신뢰구간도 설정하고 회귀모델을 fitting 했으면 이제 예측을 해봅시다

(예측은 크게 설명력과 예측력으로 구분할 수 있습니다.)

 

Prediction

  • There are two types of predictions:
    1. Prediction of the value of Y given X, i.e., Y=β0+β1X+ϵ.
    2. Prediction of the mean of Y given X, i.e., E(Y)=β0+β1X.

여기서 하나 알아갈 수 있는 점은 confidence interval은 prediction of the value of Y given X가 넓을 수 밖에 없습니다. 왜냐하면 ϵ, 즉, error가 포함되어 있기 때문입니다. 

 

  • Given X=x0,
    - in the first case, the predicted value is ^y0=^β0+^β1x0.
    - in the second case, the mean response is ^μ0=^β0+^β1x0.

 

 

Prediction intervals

  • The (1 - α) × 1000% prediction limits are given by ^μ0±tn2,α/2×s.e.(^μ0) and ^y0±tn2,α/2×s.e.(^y0),
    where s.e.(^μ0)=ˆσ1n+(x0¯x)2ni=1(xi¯x)2 and s.e.(^y0)=ˆσ1+1n+(x0¯x)2ni=1(xi¯x)2

    (여기서 보면 Expected value와 value 값의 interval을 측정할 때 차이점이 보입니다. 바로 1이 더해졌냐 안 더해졌냐인데요. value값은 아까 Prediction 파트에서 value값의 confidence interval이 넓을 수 밖에 없다고 한 것과 비슷한 매락의 이야기입니다. σ2ϵ의 분산이고 ϵ만큼 더해진 것을 확인할 수 있습니다. 그렇기에 더 넓은 confidence interval을 가질 수 밖에 없다고 설명한 것입니다.)

 

 

Role of σ2

  • 분산이 크다는 것은 값들이 선에 비교적 가깝지 않고, 분산이 작다는 것은 선에 비교적 선에 가깝다는 얘기입니다. 
  • 그러나 분산 즉, σ의 분산만을 가지고는 선형성을 추론하기에 어려움이 있습니다. 가령, 범위가 다를 때는 σ의 분산으로 선형성을 추론하면 오류가 발생할 수 있습니다.

 

그래서 이 상황에서 고안된 것이

 

 

Measuring the strength of the linear relationship

  • To remedy the limitation of σ2, we can propose to use σ2Var(Y), since it decreases if σ2 decreases or Var(Y) increases.
  • 우리는 σ2의 값을 모르기 때문에 σ2의 추정치인 MSE를 사용할 것입니다. 복습을 하자면 MSE=ni=1(yi^yi)2n2 이고 Var(Y)도 모르기 때문에 추정치인 ^Var(Y)을 사용할 것입니다. 이것도 다시 remind 하자면 ^Var(Y)=ni=1(yi¯y)2n1 입니다. 

 

여기서 선형성인 것을 나타내주는 지표인 결정계수가 나옵니다.

R2

  • To measure the strength of the linear relationship, we define the so-called R2 as follows:
    R2=1i=1n(yi^yi)2i=1n(yi¯y)2=1SSESST
    where SST stands for the total sum of squared deviation in Y from its mean.

 

 

Property of R2

  • It can be shown that ni=1(yi¯y)2=ni=1(^yi¯y)2+ni=1(yi^yi)2
    where 왼쪽 식에서 첫 번째 식은 SSR(the sum of squares due to regression)으로 불린다.
    That is, SST=SSR+SSE.

  • This implies that 0R21
    (그 이유는 R2=SSR/SST and 0SSRSST).

 

'통계학 > 회귀분석(Regression Analysis)' 카테고리의 다른 글

Matrix format  (0) 2025.02.18
Qualitative variables as predictors  (0) 2024.11.22
Transformation of variables  (0) 2024.11.19
다중선형회귀 (Multiple linear regression)  (0) 2024.10.06

+ Recent posts