Hello,

I have a panel data of participants with their blood sugar levels. I want to calculate probabilities for each of the following:
  • controlled blood sugar, given the last measure of blood sugar was controlled
  • uncontrolled blood sugar, given the last measure of blood sugar was controlled
  • controlled blood sugar, given the last measure of blood sugar was uncontrolled
  • uncontrolled blood sugar, given the last measure of blood sugar was uncontrolled
Sample dataset below. Here, 'patientid' is unique id of participant, 'mosfrombs' is months on treatment plan from baseline, and 'cont_fbs' is whether the blood sugar is controlled (cont_fbs=1) or uncontrolled (cont_fbs=0)

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str22 patientid float(mosfrombs cont_fbs)
"10-986"   1 0
"10-986"   2 0
"10-986"   4 0
"10-986"   4 0
"10-986"   5 0
"11-1050" 11 1
"11-1050" 12 1
"2-128"    7 1
"2-128"    8 0
"2-128"   10 1
"2-128"   11 1
"2-128"   12 1
"2-128"   13 1
"2-128"   14 0
end

Below is the code I used to run monthly probability of having controlled blood sugar given (i) last blood sugar measure was controlled (ii) last measure was uncontrolled:
Code:
logistic cont_fbs mosfrombs if cont_fbs[_n-1]==1 
margins, at(mosfrombs=(0(1)36))
Code:
logistic cont_fbs mosfrombs if cont_fbs[_n-1]==0 
margins, at(mosfrombs=(0(1)36))
Are the above codes correct? Or is there a different way to calculate conditional probabilities?

Thanks so much in advance!