-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear Regression - Evaluation.Rmd
275 lines (181 loc) · 7.4 KB
/
Linear Regression - Evaluation.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
---
title: "Evaluating Linear Regression"
author: "Nouman Riaz"
date: "October 18, 2016"
output: pdf_document
---
#The objective is to calculate multiple statistical measures for Simple and Linear Regression models to evaluate performance.
```{r}
require(MASS)
ads <- read.csv("Advertising.csv", stringsAsFactors=FALSE) #Reading Dataset
```
### Part 1) Caclulate R-squared of Simple Linear Regression model for Sales vs input variables.
We know the formula for R-squared:$$R^2 = 1 - \frac{TSS}{RSS}$$
and we also have the information of TSS & RSS calculation as: $$TSS = \sum_{i=1}^n(y_i - y^-)^2$$
$$RSS = \sum_{i=1}^n(y_i - y_{est})^2$$
```{r}
y = ads$Sales #As 'sales' is the response variable for all models
TSS = sum((y - mean(y))*(y - mean(y))) #TSS for response variable
cat(TSS)
```
#### a) Sales ~ TV
```{r}
#as we know the beta values already from the previous
beta_0_tv <- 7.03
beta_1_tv <- 0.0475
x <- ads$TV
y_est_tv <- beta_0_tv+(beta_1_tv*x) #Cacluating Y-estimated from TV.
RSS_tv <- sum((y-y_est_tv)*(y-y_est_tv)) #Caclulating RSS error
R_sqr_tv <- 1 - (RSS_tv/TSS) #Caclulating R-squared error for Sales~TV
cat(R_sqr_tv)
```
#### b) Sales ~ Radio
```{r}
beta_0_radio <- 9.312
beta_1_radio <- 0.202
x <- ads$Radio
y_est_radio <- beta_0_radio+(beta_1_radio*x) #Cacluating Y-estimated from Radio.
RSS_radio <- sum((y-y_est_radio)*(y-y_est_radio)) #Caclulating RSS error
R_sqr_radio <- 1 - (RSS_radio/TSS) #Caclulating R-squared error for Sales~Radio
cat(R_sqr_radio)
```
#### c) Sales ~ Newspaper
```{r}
beta_0_np <- 12.351
beta_1_np <- 0.055
x <- ads$Newspaper
y_est_np <- beta_0_np+(beta_1_np*x) #Cacluating Y-estimated from Newspaper.
RSS_np <- sum((y-y_est_np)*(y-y_est_np)) #Caclulating RSS error
R_sqr_np <- 1 - (RSS_np/TSS) #Caclulating R-squared error for Sales~Newspaper
cat(R_sqr_np)
```
### Part 2) Caclulate P-values of Simple Linear Regression model for Sales vs input variables.
To find p-values, first we need to caclulate Standard error of beta values: $$S.E(\beta_0)^2 = \sigma^2\biggl[\frac{1}{n}+\frac{x^{-2}}{\sum_{i=1}^n (x_i - x^-)^2}\biggl]$$
$$S.E(\beta_1)^2 = \frac{\sigma^2}{\sum_{i=1}^n (x_i - x^-)^2}$$
and the t-statistic:
$$t-stat(\beta) = \frac{\beta}{S.E(\beta)}$$
#### a) Sales ~ TV
```{r}
n <- length(ads$TV)
df <- n-2
sigma_sqr_tv <- RSS_tv/(n-2) #calculating sigma square
x <- ads$TV
x_bar <- mean(ads$TV)
se_b0_tv <- sqrt((sigma_sqr_tv)*((1/n)+((x_bar*x_bar)/sum((x-x_bar)*(x-x_bar))))) #Calculating Standard Error
cat(se_b0_tv)
t_stat_b0 <- beta_0_tv/se_b0_tv #The T-score of Beta.o
cat(t_stat_b0)
p_value_b0 <- 2*pt(t_stat_b0,df,lower.tail = FALSE) #Calculating P-value for Beta.o
cat(p_value_b0)
se_b1_tv <- sqrt(sigma_sqr_tv/sum((x-x_bar)*(x-x_bar)))
cat(se_b1_tv)
t_stat_b1 <- beta_1_tv/se_b1_tv
cat(t_stat_b1)
p_value_b1 <- 2*pt(t_stat_b1,df,lower.tail = FALSE) #Calculating P-value for Beta.1
cat(p_value_b1)
```
#### b) Sales ~ Radio
```{r}
n <- length(ads$TV)
df <- n-2
sigma_sqr_tv <- RSS_tv/(n-2) #calculating sigma square
x <- ads$Radio
x_bar <- mean(ads$Radio)
se_b0_tv <- sqrt((sigma_sqr_tv)*((1/n)+((x_bar*x_bar)/sum((x-x_bar)*(x-x_bar))))) #Calculating Standard Error
cat(se_b0_tv)
t_stat_b0 <- beta_0_tv/se_b0_tv #The T-score of Beta.o
cat(t_stat_b0)
p_value_b0 <- 2*pt(t_stat_b0,df,lower.tail = FALSE) #Calculating P-value for Beta.o
cat(p_value_b0)
se_b1_tv <- sqrt(sigma_sqr_tv/sum((x-x_bar)*(x-x_bar)))
cat(se_b1_tv)
t_stat_b1 <- beta_1_tv/se_b1_tv
cat(t_stat_b1)
p_value_b1 <- 2*pt(t_stat_b1,df,lower.tail = FALSE) #Calculating P-value for Beta.1
cat(p_value_b1)
```
#### c) Sales ~ Newspaper
```{r}
n <- length(ads$TV)
df <- n-2
sigma_sqr_tv <- RSS_tv/(n-2) #calculating sigma square
x <- ads$Newspaper
x_bar <- mean(ads$Newspaper)
se_b0_tv <- sqrt((sigma_sqr_tv)*((1/n)+((x_bar*x_bar)/sum((x-x_bar)*(x-x_bar))))) #Calculating Standard Error
cat(se_b0_tv)
t_stat_b0 <- beta_0_tv/se_b0_tv #The T-score of Beta.o
cat(t_stat_b0)
p_value_b0 <- 2*pt(t_stat_b0,df,lower.tail = FALSE) #Calculating P-value for Beta.o
cat(p_value_b0)
se_b1_tv <- sqrt(sigma_sqr_tv/sum((x-x_bar)*(x-x_bar)))
cat(se_b1_tv)
t_stat_b1 <- beta_1_tv/se_b1_tv
cat(t_stat_b1)
p_value_b1 <- 2*pt(t_stat_b1,df,lower.tail = FALSE) #Calculating P-value for Beta.1
cat(p_value_b1)
```
### Part 3) Caclulate R-squared of Multiple Linear Regression model for Sales vs all input variables.
```{r}
#As we know the beta values of MLR from previous
X <- matrix(c(seq(from=1,to=1,length.out = nrow(ads)),ads$TV,ads$Radio,ads$Newspaper), nrow=nrow(ads), ncol=4) #Constructing input matrix
BetaV <- c(2.938889,0.04576465,0.18853,0.001037493)
y_est <- X%*%BetaV
RSS <- sum((y-y_est)*(y-y_est)) #Caclulating RSS error
R_sqr <- 1 - (RSS/TSS) #Caclulating R-squared error for MLR
cat(R_sqr)
```
### Part 4) Caclulate p-values of Multiple Linear Regression model for Sales vs all input variables.
```{r}
n = length(ads$Sales)
df=n-2
p = ncol(X)
sigma_sqr <- RSS/(n-p-1)
var_covar <- sigma_sqr*ginv((t(X)%*%X)) # variance covariance matrix
se_v <- sqrt(diag(var_covar))
cat(se_v) #Standard Error Matrix
t_values <- BetaV/se_v
cat(t_values)
p_values <- 2*pt(t_values,df,lower.tail = FALSE) #Calculating p-values
cat(p_values)
```
### Part 5) Introducing interaction terms
```{r}
#a) TV*Radio as interaction term
X <- matrix(c(seq(from=1,to=1,length.out = nrow(ads)),ads$TV,ads$Radio,ads$Newspaper,ads$TV*ads$Radio), nrow=nrow(ads), ncol=5) #Constructing input matrix
#Finding beta vector for this
Xt <- t(X) #Taking transpose of input matrix
a <- Xt %*% X #Multiplaying input matrix with its transpose
b <- Xt %*% y #Multiplaying input matrix with output variable
inv <- ginv(a) #Taking inverse
BetaV <- inv %*% b #Caculating Beta vector
y_est <- X%*%BetaV
RSS <- sum((y-y_est)*(y-y_est)) #Caclulating RSS error
R_sqr2 <- 1 - (RSS/TSS) #Caclulating R-squared error for MLR
cat(R_sqr2)
#b) Radio*Newspaper
X <- matrix(c(seq(from=1,to=1,length.out = nrow(ads)),ads$TV,ads$Radio,ads$Newspaper,ads$Radio*ads$Newspaper), nrow=nrow(ads), ncol=5) #Constructing input matrix
#Finding beta vector for this
Xt <- t(X) #Taking transpose of input matrix
a <- Xt %*% X #Multiplaying input matrix with its transpose
b <- Xt %*% y #Multiplaying input matrix with output variable
inv <- ginv(a) #Taking inverse
BetaV <- inv %*% b #Caculating Beta vector
y_est <- X%*%BetaV
RSS <- sum((y-y_est)*(y-y_est)) #Caclulating RSS error
R_sqr3 <- 1 - (RSS/TSS) #Caclulating R-squared error for MLR
cat(R_sqr3)
#c) Tv*Radio but no newspaper
X <- matrix(c(seq(from=1,to=1,length.out = nrow(ads)),ads$TV,ads$Radio,ads$TV*ads$Radio), nrow=nrow(ads), ncol=4) #Constructing input matrix
#Finding beta vector for this
Xt <- t(X) #Taking transpose of input matrix
a <- Xt %*% X #Multiplaying input matrix with its transpose
b <- Xt %*% y #Multiplaying input matrix with output variable
inv <- ginv(a) #Taking inverse
BetaV <- inv %*% b #Caculating Beta vector
y_est <- X%*%BetaV
RSS <- sum((y-y_est)*(y-y_est)) #Caclulating RSS error
R_sqr4 <- 1 - (RSS/TSS) #Caclulating R-squared error for MLR
cat(R_sqr4)
cat(sprintf("R2 = %s\nR2_2 = %s\nR2_3 = %s\nR2_4 = %s\n",R_sqr,R_sqr2,R_sqr3,R_sqr4))
```
So the better R-square we have, is achieved by introducing "Radio*Newspaper" interaction term.