I am trying to calculate beta regression by using Hat matrix, manually. I am using idea of https://stats.stackexchange.com/questions/8126/how-to-calculate-the-hat-matrix-for-logistic-regression-in-r?noredirect=1&lq=1 answer. But its not working for me. Further, I am not sure its correct or not. Kindly help.
My code
require(betareg)
df<-data("ReadingSkills")
y<-ReadingSkills$accuracy
n<-length(y)
x1<-rnorm(n,0,1)
x2<-rnorm(n,0,1)
X<-cbind(1,x1, x2)
bfit <- solve(t(X) %*% X + u * diag(3)) %*% t(X) %*% y
bfit1<-betareg(accuracy ~ x1+x2, data = ReadingSkills, link="logit")
bfit
bfit1
v <- 1/(1+exp(-X%*%bfit))
VX <- X*v
H <- VX%*%solve(crossprod(VX,VX),t(VX))
This problem is best suited for cross validated as it requires math equations for the proofs. But we can still do the code here. Anyway, since your interest is the coefficients, it is worthwile to not that just like
logistic regression
,betareg
does not have a closed form solution for the MLEs. You need to use any iterative methods for optimization. Below I use the readily availableoptim
function from base R.Can we do the same directly without depending on the
betareg
function? Yes of course. we just write the log likelihood function and optimize that for the 4 parameters, ie the 3 betas, and the phi coefficient:You can see that the results from
optim
match exactly what we have in beta regression.