-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxLik BFGS.R
140 lines (127 loc) · 2.97 KB
/
MaxLik BFGS.R
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
##Statdes
library("pastecs")
library("dplyr")
data=read.csv(file.choose(), header=TRUE, sep= ";")
View(data)
names(data)
data$X2<-as.factor(data$X2)
data$X4<-as.factor(data$X4)
data$X41<-ifelse(data$X4==1,1,0)
data$X41<-as.factor(data$X41)
data$X42<-ifelse(data$X4==2,1,0)
data$X42<-as.factor(data$X42)
data$X43<-ifelse(data$X4==3,1,0)
data$X43<-as.factor(data$X43)
data$X5<-as.factor(data$X5)
data$X6<-as.factor(data$X6)
str(data)
attach(data)
summary(data$X1)
summary(data$X3)
summary(data$X7)
##Pendeteksian Multikolinieritas
library("car")
reg=lm(V~X1+X3+X7,data=data)
vif(reg)
#Tabulasi X2 dengan X41
tab.x2.x4=table(data$X2,data$X4)
tab.x2.x4
ind.test.x2.x4=chisq.test(tab.x2.x4)
ind.test.x2.x4
#Tabulasi X2 dengan X5
tab.x2.x5=table(data$X2,data$X5)
tab.x2.x5
ind.test.x2.x5=chisq.test(tab.x2.x5)
ind.test.x2.x5
#Tabulasi X2 dengan X6
tab.x2.x6=table(data$X2,data$X6)
tab.x2.x6
ind.test.x2.x6=chisq.test(tab.x2.x6)
ind.test.x2.x6
#Tabulasi X4 dengan X5
tab.x4.x5=table(data$X4,data$X5)
tab.x4.x5
ind.test.x4.x5=chisq.test(tab.x4.x5)
ind.test.x4.x5
#Tabulasi X4 dengan X6
tab.x4.x6=table(data$X6,data$X4)
tab.x4.x6
ind.test.x4.x6=chisq.test(tab.x4.x6)
ind.test.x4.x6
#Tabulasi X5 dengan X6
tab.x5.x6=table(data$X5,data$X6)
tab.x5.x6
ind.test.x5.x6=chisq.test(tab.x5.x6)
ind.test.x5.x6
##Estimasi Parameter
library("maxLik")
library("optimx")
library("pscl")
#Fungsi Likelihood
ll<- function(par){
y<- as.vector(data$Y)
x<- as.matrix(cbind(1, data$X1, data$X2, data$X3, data$X5, data$X6, data$X7))
beta <- par[1:7]
m = length(par)
n = length(y)
loglik = rep(0,n)
for(i in 1:n){
xbeta= as.numeric(x[i,]%*%beta)
yd = y[i]*xbeta
loglik[i]=yd-log(1+exp(xbeta))
}
return(loglik)
}
#Gradien
gl<- function(par){
y<- as.matrix(data$Y)
x <- as.matrix(cbind(1, data$X1, data$X2, data$X3, data$X5, data$X6, data$X7))
beta <- par[1:7]
n = length(y)
m = length(par)
gg <- matrix(0,n,m)
p<- matrix(n,1)
xbeta<- matrix(n,1)
for(i in 1:n){
for(j in 1:m){
xbeta[i] <- as.numeric(x[i,]%*%beta)
p[i]<- exp(xbeta[i])/(1+exp(xbeta[i]))
gg[i,j] <- x[i,j]%*%(y[i]-p[i])
}
}
return(gg)
}
#MaxLikBFGS
sv<- c(Intercept=0, B1=0, B2=0, B3=0, B5=0, B6=0, B7=0)
max <- maxControl(tol=1e-3,print.level=3,iterlim=200)
mle<-maxLik(logLik=ll, grad = gl, start= sv, method="BFGS", control=max)
mle
gradient(mle)
hessian(mle)
##Uji Simultan
library("MASS")
llfull<-as.matrix(ll(c(coef(mle))))
llreduced<-as.matrix(ll(c(0,0,0,0,0,0,0,0,0,0)))
model_full <- as.numeric(colSums(llfull))
model_full
model_reduced <-as.numeric(colSums(llreduced))
model_reduced
LRT <- -2*(model_reduced-model_full)
LRT
p.val <-pchisq(LRT, df = 9, lower.tail=FALSE)
p.val
##Uji Parsial
BFGS=coef(mle)
BFGS
se=stdEr(mle)
W<-BFGS/se
W
##Odd Ratio
Ob1<-4.4899213
Ob7<--1.9893193
#Odd Ratio B1
OR1<-exp(Ob1)*0.01
OR1
#Odd Ratio B7
OR7<-exp(Ob7)*1
OR7