给定二进制数据,确定 R 中两个参数的估计量

Determining an estimator for two parameters in R over a sum, given binary data

我有以下总和(它是部分似然的得分向量),我想找到方程为零的 beta_0 和 beta_1 的值。

Y_t的{t=0,...n}是不同时间点的已知测量值,只能是0或1(二进制数据)。

这是我到目前为止想出的代码。我尝试实现了功能,应该是最小化:

    y = c(1,0,1,1,1,1,0)

    f <- function(beta_0, beta_1){

      sum = matrix(c(0, 0), ncol = 1, nrow = 2)

      for(t in 2:length(y))
      {
        sum = sum + matrix(c(1, y[t-1]), ncol = 1, nrow = 2)  %*% c(y[t] - (exp(beta_0 + beta_1 * y[t-1])) / (1 + exp(beta_0 + beta_1 * y[t-1])))

      }

      return(sum)
    } 
    #Just making a guess:
    f(0.5,0.5)

对此没有解析解,因此我想使用 R 来确定给定一组二进制数据的两个参数(beta_0 和 beta_1)。要找到数值解,我认为某种迭代解是可行的方法。

到目前为止,我已经查看了 R 中的不同优化包,但找不到合适的。

我面临的一个问题是函数的结果是一个向量,到目前为止我看到的优化方法都只使用一个变量的输出。

你能推荐一个解决这个问题的包,或者我可以用其他任何方法来解决这个问题吗?也许通过添加它的绝对值将 Vector 视为一个数字?

提前致谢!

我能够通过使用绝对值和优化来解决问题:

 estimate_parameter_binary_model  <- function(y = y) {

  #Function to be optimized:
  optimization_function_binary_model <- function(p = c(1,1)){ #to use the optim function, it has to be called by a vector p. Which contains: p[1] = beta_0 and p[2] = beta_1

    #p is an vector and contains both parameters
    beta_0 = p[1]
    beta_1 = p[2]

    sum = matrix(c(0, 0), ncol = 1, nrow = 2)

    for(t in 2:length(y))
    {
      sum = sum + matrix(c(1, y[t-1]), ncol = 1, nrow = 2)  %*% c(y[t] - (exp(beta_0 + beta_1 * y[t-1])) / (1 + exp(beta_0 + beta_1 * y[t-1])))

    }

    return(abs(sum[1]) + abs(sum[2])) #optim just needs one number, so we use absolute values here and add them.
  } 

  #Call optim()

  par = optim(c(0,0), optimization_function_binary_model)$par

  Solution<- list(beta_0 = par[1],beta_1 = par[2])

  return(Solution)
}