我有大约280个人的数据和几个假的协变量,称为"外国" " prevgrad。"数据如下所示:
Name Program foreign prevgrad
******* ******* 0 0
******* ******* 0 0
******* ******* 1 1
******* ******* 0 0
******* ******* 1 1
******* ******* 0 1
每个人都属于一个程序集群A或B.我将数据分成两个子集A和B.我试图产生一个简单的最大似然估计,我试着做以下事情: / p>
##The density function for subset A
PrA <- function(u, piA0, piA1, piA2, piB0, piB1, piB2, z1, z2) {
(1/sqrt(2*pi)*exp(-u^2/2))*3*exp(piA0+piA1*z1+piA2*z2+u /(3*exp(piA0+piA1*z1+piA2*z2 + u)+5*exp(piB0+piB1*z1+piB2*z2))
}
logIntA <- function(a0, a1, a2, b0, b1, b2, zee1, zee2) {
log(integrate(function(u, piA0=a0, piA1=a1, piA2=a2, piB0=b0, piB1=b1, piB2=b2, z1=zee1, z2=zee2) PrA(u, piA0 = piA0, piA1 =piA1, piA2=piA2, piB0 = piB0, piB1 =piB1, piB2=piB2, z1=z1, z2=z2), lower=-10, upper=10)$integral)
}
#The density function for subset B
PrB <- function(u, piA0, piA1, piA2, piB0, piB1, piB2, z1, z2) {
(1/sqrt(2*pi)*exp(-u^2/2))*5*exp(piB0+piB1*z1+piB2*z2)/(3*exp(piA0 + piA1*z1 +piA2*z2 + u)+5*exp(piB0+piB1*z1+piB2*z2))
}
logIntB <- function(a0, a1, a2, b0, b1, b2, zee1, zee2) {
log(integrate(function(u, piA0=a0, piA1=a1, piA2=a2, piB0=b0, piB1=b1, piB2=b2, z1=zee1, z2=zee2) PrB(u, piA0 = piA0, piA1 =piA1, piA2=piA2, piB0 = piB0, piB1 =piB1, piB2=piB2, z1=z1, z2=z2), lower=-10, upper=10)$integral)
}
##The maximum likelihood statistic
toMax2 <- function(t) {
piA0=t[1]
piA1=t[2]
piA2=t[3]
piB0=t[4]
piB1=t[5]
piB2=t[6]
(sum(apply(studentsA[,c('foreign','prevgrad')],1, function(s) logIntA(piA0, piA1, piA2, piB0, piB1, piB2, s['foreign'], s['prevgrad'])))+sum(apply(students.AB[,c('foreign','prevgrad')],1, function(s) logIntB(piA0, piA1, piA2, piB0, piB1, piB2, s['foreign'], s['prevgrad']))))
}
我无法在文档中找到相关的例子。&#39; apply。&#39;目前我收到错误&#34;数学函数的非数字参数。&#34;我无法弄清楚如何使z1等于“外国人”。并且z2等于prevgrad&#39;在每一行(即每个人)。
看起来像一个简单的修复,但你可能会告诉我,我是一个新手。我很感激任何建议(包括对代码的一般改进)。谢谢!