使用R,乒乓球数据集

时间:2016-11-30 16:26:43

标签: r function

我有一个包含乒乓球比赛的两个二进制变量的数据集:service.A显示一名球员是否正在服役并且点.A显示球员A是否得分。

每个点由重要性因子加权。 1到3之间的因子表明了该特定点的重要性。

我想估计四个参数strengthP1Service,strengthP1Return ..这些是玩家在服务或返回时的估计。

代码没有给出任何错误,但是我没有从optim函数得到一个带有四个参数估计(MLE)的输出。 如果有人能帮我估算这四个参数,我将非常感激。

非常感谢, 维姆

myfunction <- function(data, theta1, theta2, theta3, theta4, importance) {
  N <- length(data)
  nllfunction<-function(p){
    strengthP1Service<- p[1]
    strengthP1Return<- p[2]
    strengthP2Service<- p[3]
    strengthP2Return<- p[4]

# four possible outcomes since there are 2 binary variables
    ratio1 <- strengthP1Service/(strengthP1Service + strengthP2Return) 
    ratio2 <- strengthP2Return/(strengthP1Service + strengthP2Return) 
    ratio3 <- strengthP2Service/(strengthP1Return + strengthP2Service) 
    ratio4 <- strengthP1Return/(strengthP1Return + strengthP2Service) 

# weighted likelihood function
    nll<- -sum(log(ratio1^((data$service.A==1 & data$point.A == 1)*importance) *
                     ratio2^((data$service.A==1 & data$point.A == 0)*importance) *
                     ratio3^((data$service.A==0 & data$point.A == 0)*importance) *
                     ratio4^((data$service.A==0 & data$point.A == 1)*importance)))

    return(nll)
  }
    out<-optim(par=c(0.5, 0.5, 0.5, 0.5),  
             fn=myfunction, gr=NULL, method="L-BFGS-B",
             lower=c(0,0,0,0), upper=c(1,1,1,1))
  }



# first lines of the dataset
Player.A Player.B service.A punt.A importance
1   Sharon  Kenneth         1      1        2.0
2   Sharon  Kenneth         1      1        2.0
3   Sharon  Kenneth         0      1        2.0
4   Sharon  Kenneth         0      1        2.0
5   Sharon  Kenneth         1      0        1.5
6   Sharon  Kenneth         1      1        2.0

0 个答案:

没有答案