Hi everyone,
I am trying to estimate group differences of transition frequencies.
I have a unbalanced panel data with sleep recordings with 15 COPD patients. (simplified dataset added)
Patients receive a placebo and a drug intervention on separate nights (cross over design). I want to test if the drug intervention has altered the dynamics of transitions between sleep stages.

I attempting to compare the overall transition frequency between sleep stages for the placebo and drug night, and then compare the transition frequencies between the three specific stages in the placebo intervention with the transition frequencies between the three specific sleep stages in the drug intervention.


With 3 sleep stages there are 6 different possible transitions; 0 to 1, 0 to 2, 1 to 0, 1 to 2, 2 to 0, 2 to 1. The relative transition frequency should be calculated by dividing the number of transitions between sleep stages by the total number of all transitions.
I haven`t found any information either on this forum of how to do such an analysis, and really appreciate if someone could help me!
I`m using Stata 15.1 for Windows, and I`m a quite new STATA user.

Epoch= time period
ID_= subject ID
Intervention_: placebo=2, drug=1
Stage: 0,1,2

Thank you!
Harald

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double Epoch long(ID_ intervention_) double Stage
1 1 2 0
2 1 2 1
3 1 2 2
4 1 2 0
1 2 2 0
2 2 2 1
3 2 2 2
4 2 2 1
5 2 2 1
1 1 1 0
2 1 1 1
3 1 1 0
4 1 1 2
5 1 1 0
1 2 1 0
2 2 1 1
3 2 1 2
4 2 1 0
5 2 1 1
6 2 1 0
end
label values ID_ ID_
label def ID_ 1 "ID1", modify
label def ID_ 2 "ID2", modify
label values intervention_ intervention_
label def intervention_ 1 "n", modify
label def intervention_ 2 "p", modify
label values Stage NREM
label def NREM 0 "VĂ„ken", modify
label def NREM 1 "NREM", modify
label def NREM 2 "REM", modify