library(readr)
dat <- Downloads
N <- length(dat$X1)
###likelihood functions
likeli.fix <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    temp <- dpois(y[t], l+y[t-1] * exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1])))
    logL <- logL + log(temp)
  }
  -logL
}
likeli.unif <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    m <- 0:y[t]
    gamma1 <- expint::gammainc(m+1, 2*y[t-1]*exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1])))
    gamma2 <- gamma(m+1)
    if (y[t-1] == 0)
      temp <- dpois(y[t], l)
    else
      temp <- sum(dpois(y[t]-m, l) / (y[t-1]*factorial(m) * (2 * exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1])))) * (gamma2 - gamma1)) 
    logL <- logL + log(temp)
  }
  -logL
}
likeli.exp <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    m <- 0:y[t]
    if (y[t-1] == 0)
      temp <- dpois(y[t], l)
    else
    {
      v = (1 + exp(b1+b2*y[t-1])) / exp(b1+b2*y[t-1])
      temp <- sum(v / (v+y[t-1])^(m+1) * y[t-1]^m * dpois(y[t]-m, l))
    }
    logL <- logL + log(temp)
  }
  -logL
}
likeli.chi <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    m <- 0:y[t]
    if (y[t-1] == 0)
      temp <- dpois(y[t], l)
    else
    {
      v = exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1]))
      temp <- sum(y[t-1]^m / factorial(m) * dpois(y[t]-m, l) * gamma((v+2*m)/2) /2^(v/2) / gamma(v/2) / (0.5+y[t-1])^((v+2*m)/2))
    }
    logL <- logL + log(temp)
  }
  -logL
}
###CLS
target <- function(theta)
{
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  sum((y1 - y0 * exp(b1+b2*y0)/(1 + exp(b1+b2*y0)) - l)^2)
}
###CLS1
target1 <- function(theta)
{
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  sum(y1 - y0 * exp(b1+b2*y0)/(1 + exp(b1+b2*y0)) - l)^2
}
y <- dat$X1
y1 <- dat$X1[2:N]
y0 <- dat$X1[1:(N-1)]
theta.0 <- c(1, -0.5, 2)
fit.cls <- optim(theta.0 ,target, hessian = T)
fit.cls1 <- optim(theta.0 ,target1, hessian = T)
fit.cml.fix <- optim(theta.0, likeli.fix)
fit.cml.exp <- optim(theta.0, likeli.exp)
fit.cml.chi <- optim(theta.0, likeli.chi)
fit.cml.unif <- optim(c(0.8757, -0.0142, 2.6853), likeli.unif)

likeli.chi(c(0.8757, -0.0142, 2.6853))
likeli.exp(c(0.8757, -0.0142, 2.6853))
likeli.fix(c(0.8757, -0.0142, 2.6853))
likeli.unif(c(0.8757, -0.0142, 2.6853))
######################################################
2*fit.cml.fix$value + 6
2*fit.cml.exp$value + 6
2*fit.cml.chi$value + 6
2*fit.cml.unif$value + 6
2*fit.cml.fix$value + 3*log(N)
2*fit.cml.exp$value + 3*log(N)
2*fit.cml.chi$value + 3*log(N)
2*fit.cml.unif$value + 3*log(N)
#####################################################
dat <- data.frame(y1 = y1, y0 = y0)
fct.r <- function(theta, x)
{
  b1 <- -0.3
  cbind(
    (x[1] - x[2]*exp(theta[1]+b1*y0)/(1+exp(theta[1]+b1*y0)) - theta[2]) * exp(theta[1]+b1*y0)/(1+exp(theta[1]+b1*y0))^2 * x[2],
    (x[1] - x[2]*exp(theta[1]+b1*y0)/(1+exp(theta[1]+b1*y0)) - theta[2]) * exp(theta[1]+b1*y0)/(1+exp(theta[1]+b1*y0))^2 * x[2]^2,
    x[1] - x[2]*exp(theta[1]+b1*y0)/(1+exp(theta[1]+b1*y0)) - theta[2]
  )
}
fit.r <- gel(fct.r, x = dat, c(1, 1.2))
gama.r <- -fit.r$lambda
thet.r <- fit.r$coefficients
Mt.r <- as.matrix(fct.r(thet.r, dat))
2*sum(log(as.vector(1+gama.r %*% t(Mt.r))))
qchisq(df=1, p=0.95)
######################################################
mat <- matrix(c(3,3,1,2), 2, 2,byrow = T)
layout(mat, heights = c(3,2))
acf(y,main='')
pacf(y,main='')
plot(y, type = "b", main='')
