forked from vcheng3/MachineLearning_Models-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXPRESS_nested_models.R
More file actions
50 lines (34 loc) · 995 Bytes
/
EXPRESS_nested_models.R
File metadata and controls
50 lines (34 loc) · 995 Bytes
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
#set directory
setwd()
dir()
#load data
del = read.table(file="EXPRESS.txt", header=TRUE)
ls()
dimnames(del)
del
#get vectors of variables
y = del[,"Cost"] #other way of writing: x1 = del[,1] meaning column 1 of data
x1 = del[,"Weight"]
x2 = del[,"Distance"]
n = length(Y)
n
#make vectors for remaining variables-square terms
x1sq = x1^2
x2sq = x2^2
#Fit 2 models
ReducedModel = lm(y~x1+x2+x1:x2)
CompleteModel = lm(y~x1+x2+x1sq+x2sq+x1:x2)
summary_R=summary(ReducedModel)
summary_C=summary(CompleteModel)
#degree of freedom
df_SSE_R = length(y)-3-1 # 3 because 3 predictors-x1,x2,x1:x2
df_SSE_C = length(y)-5-1
# Finding SSE (SSE is sigma^2 * df)
SSE_R = summary_R$sigma^2*df_SSE_R
SSE_C = summary_C$sigma^2*df_SSE_C
# Finding F stat
F_stat = (SSE_R-SSE_C)/(5-3)/(SSE_C/df_SSE_C)
# Finding p value
pf(F_stat, df1=2, df2=df_SSE_C, lower.tail = F) #Lower tail false because test is one sided
# Doing the comparison of models directly using code
anova(ReducedModel, CompleteModel)