-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-codes 1
32 lines (26 loc) · 1.21 KB
/
R-codes 1
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
require("emplik") # installting the required package "emplik"
library(emplik)
#running empirical likelihood ratio test for specific mean
d=rnorm(100)
el.test(d,0)
el.test(d,2)
# Comparison of parametric and empirical likelihood
el=function(data,test.mu,true.mu) # test.mu= set of different test values of mu, true.mu=true value of mu
{
el=c() #creating empty vectors for el and parametric likelihood(lik)
lik=c()
m=length(test.mu)
for(j in 1:m)
{
el[j]=el.test(data,test.mu[j])$'-2LLR' #for each test value for mu, calculating the -2 logel lik ratio(LLR)
lik[j]=-2*sum(log(dnorm(data,test.mu[j],1)/dnorm(data,true.mu,1))) # #for each test value for mu, calculating the -2 log lik ratio(LLR) (parametric)
}
data.frame(el,lik)
}
d=rnorm(100) #creating data vector from standard normal distribution
mu=seq(-1,1,0.001) # creating test values for mu
res=el(d,mu,0) # running the function to calculate -2*logLLR using parametric and empirical lik
#Plotting parametric and empirical -2* log LLR
plot(mu,res$el,type="l",col="red",main="Empirical Likelihood and Likelihood ratio Curves", xlab=expression(mu),ylab="-2* log(LR)")
lines(mu,res$lik,lty=2,col="blue")
legend(0.7,10,c("EL","PL"),lty=c(1,2),col=c("red","blue"))