Maximize *custom* likelihood function for logistic regression My dataset contains n observations $X_i$ of n individuals and I want to predict a binary outcome $Y_i$. 


*

*logistic regression model
It is fair to assume that this $Y_i$ is the realization of a Bernoulli experiment and is 1 with probability $\pi_i$. Under the logistic regression model, I model $\log\big(\frac{\pi_i}{1-\pi_i}\big) = \mathbf{X_i} \mathbf{\beta}$ which is fine by me. $\beta$ is solved for by optimizing the log-likelihood of the data under the model. The individual log-likelihood is 
$$\log L_\beta(i)=\pi_i^{y_i}(1-\pi_i)^{1-y_i}$$
The total log likelihood is the sum of the individual likelihood, expressed in terms of $\beta$.

*Changes I want to make
The trick is, I also have historical data about previous outcome ($Y$) for some individuals! I do not have the associated previous $X$ though. In my case, it is fair to assume that the probability $\pi_i$ does not change in the life of an individual so I want to take into account the previous outcome in the likelihood! 
If I call  $n(i)$ the total number of observations ($\geq1$) for individual $i$ and $Y_{i,j}$ the $j^{th}$ outcome observed for individual $i$, my new individual likelihood is:
$$\log L_\beta(i)= \sum_j^{n(i)} \pi_i^{y_{i,j}}(1-\pi_i)^{1-y_{i,j}}$$
And the total likelihood to optimize is the sum of this over $i$.

*My questions


*

*Does it make sense to do this or is there any obvious flaw I missed? I am open to alternatives. 

*Can I implement this using glm (R)? I would need to define my own family, I don't know if it is possible. I guess the likelihood above would have to be in the exponential family and I am not sure it is since the number of observations for a given individual is not fixed.

*Do I need to optimize manually (using optim for R for ex) to fit my model? I don't have a problem with this approach per se, but in the end I would like to use this likelihood in a LASSO or elastic net framework and it would be so much more convenient and effective to be able to use glmnet!
 A: I realized something:
$$\begin{align}
\log L_\beta(i) & = \sum_j^{n(i)} \pi_i^{y_{i,j}}(1-\pi_i)^{1-y_{i,j}} \\
& = \pi_i^{y_{i,1}}(1-\pi_i)^{1-y_{i,1}} + \sum_j^{n(i)-1} \pi_i^{y_{i,j+1}}(1-\pi_i)^{1-y_{i,j+1}} \\
& = \pi_i^{y_{i}}(1-\pi_i)^{1-y_{i}} + \pi_i^{y_{i,2}}(1-\pi_i)^{1-y_{i,2}} & \mbox{assuming  } n(i)=2 \mbox{ to simplify}
\end{align}$$
Since $\pi_i = f(X_i)$, the individual likelihood for the $i^{th}$ observation is equal to the likelihood of $n(i)$ observations, all with same $X=X_i$ but with outcomes resp. $Y_{i,1}, ..., Y_{i,n(i)}$ .
I implemented the likelihood described in my question and optimized it manually with optim. I also tried the approach described in my answer which consists of transforming the dataset to create "fake" observations corresponding to previous history, and using glm on the transformed data. The coefficients estimates match!
This great because it allows me to use glmnet in a plug-and-play manner. 
A: Here is what I would do:

Notice that I am taking the mean value for the covariates at the individual level and the log likelihood will be:

I think you need to hard code this in R.
