############################################################
#  R function for fitting a logistic regression model      #
#  for estimating participation probabilities              #
#  using the method of Wang, Valliant and Li (2021, SIM)   #
############################################################

##############################################################
# Input:                                                     #
# Xa, data matrix of x for the nonprobility sample           #
# Xb, data matrix of x for the reference sample              #
# db, the vector of design weights from the reference sample #
#                                                            #
# Output: b0, the estimated regression coefficients          #
#             in the logistic regression model               #
#                                                            #
# Note:                                                      #
# Xa and Xb have the same column variables of x              #
# The first column of Xa and Xb are 1's for an intercept     #
# The R function "CLW" is needed for initialization          #
#                                                            #
# Written by Changbao Wu, August 15, 2024                    #
##############################################################

WVL=function(Xa,Xb,db){
b0=CLW(Xa,Xb,db)
tol=0.0000001
dif=1
while(dif>tol){
mua=1-1/(1+exp(Xa%*%b0))
mub=1-1/(1+exp(Xb%*%b0))
Uab=t(Xa)%*%((1-mua)/(1+mua))-t(Xb)%*%(db*mub*(1-mub)/(1+mub))
Hab=-2*(t(Xa)%*%(c(mua*(1-mua)/(1+mua)^2)*Xa))-t(Xb)%*%(c(db*mub*(1-mub)*(2/(1+mub)^2-1))*Xb)
b1=b0-solve(Hab,Uab)
dif=max(abs(b1-b0))
b0=b1
                      }
return(as.vector(b0))
                     }