Plots

Author

Sarah Urbut

Published

January 7, 2024

Quarto

The success is here!

Recall the topics:

Code
# estimated_Theta=readRDS("~/estimated_theta_all38.rds")
# adjusted_gp_mean=readRDS("~/adjusted_gp_mean_all38.rds")
# gp_mean_array=readRDS("~/gp_mean_array_all38.rds")
# gp_means_pop=readRDS("~/gp_means_pop_all38.rds")
library(ggplot2)
library(data.table)
library(ggsci)
library(dplyr)
library(ATM)
beta=ATM::UKB_HES_10topics # age times disease times topics
data=ATM::HES_age_example
data$round_age=round(data$age_diag,0)
# Prepare the data by grouping and summarizing
data_grouped <- data %>%
  group_by(eid, round_age) %>%
  summarise(diagnoses = list(diag_icd10), .groups = 'drop')%>%ungroup()
D=length(unique(data_grouped$eid)) # To test individuals
#D=10
K <- dim(beta)[3]  # Number of topics
T <- dim(beta)[1]  # Number of time points

csv=fread("~/Library/CloudStorage/Dropbox-Personal/41588_2023_1522_MOESM4_ESM.csv")
topics=unique((csv$V1))[4:13]

# estimated_Theta=readRDS("estimated_theta_all_log_38.rds")
# matplot(t(apply(estimated_Theta[,,c(1:3)],c(2,3),mean)),type = "l")
# adjusted_gp_mean=readRDS("adjusted_gp_mean_all_log_38.rds")
# gp_mean_array=readRDS("gp_mean_array_all_log_38.rds")
# gp_means_pop=readRDS("gp_means_pop_all_log_38.rds")

estimated_Theta=readRDS("estimated_theta_all_log_10_simk.rds")
matplot(t(apply(estimated_Theta[,,c(1:3)],c(2,3),mean)),type = "l")

Code
adjusted_gp_mean=readRDS("adjusted_gp_mean_all_log_10_simk.rds")
gp_mean_array=readRDS("gp_mean_array_all_log_10_simk.rds")
gp_means_pop=readRDS("gp_means_pop_all_log_10_simk.rds")

pt_ids=unique(data$eid)[1:dim(estimated_Theta)[1]]
data_grouped=data_grouped[data_grouped$eid%in%pt_ids,]
dimnames(estimated_Theta)=dimnames(adjusted_gp_mean)=dimnames(gp_mean_array)=list(pt_ids,topics,c(1:81))




# Assuming 'estimated_Theta', 'adjusted_gp_mean', and 'gp_means_pop' are loaded into your R session

# Set the dimensions for plotting
num_individuals <- dim(estimated_Theta)[1]
num_topics <- dim(estimated_Theta)[2]
num_time_points <- dim(estimated_Theta)[3]

# Sample a random individual
population_theta <- gp_means_pop[,-c(1:3)]
# Create a data frame for plotting
time <- 4:num_time_points
df_population <- data.frame(time = rep(time, num_topics),
                            topic = rep(topics, each = num_time_points-3),
                            theta = as.vector(t(population_theta)))
# Plot for the individual
ggplot(df_population, aes(x = time, y = theta, color = topic)) +
  geom_line() +
  labs(title = sprintf("Topic Proportions Over Time for Population"),
       x = "Time Point",
       y = "Population GP Mean",
       color = "Topic") +scale_color_d3(
  palette = c("category20"),alpha = 1)+
  theme_minimal() 

Code
dg=data_grouped%>%group_by(eid)%>%summarise(n=length(eid))%>%arrange(desc(n))

for(i in 1:10){
  
sample_id <- as.character(dg$eid[i])

# Extract data for the individual and population
individual_theta <- estimated_Theta[sample_id, ,-c(1:3) ]
individual_gp_means <- adjusted_gp_mean[sample_id, ,-c(1:3) ]
df_individual <- data.frame(time = rep(time, num_topics),
                            topic = rep(topics, each = num_time_points-3),
                            theta = as.vector(t(individual_gp_means)))

# Plot for the individual
library(ggsci)
print(ggplot(df_individual, aes(x = time, y = theta, color = topic)) +
  geom_line() +
  labs(title = paste("Topic Proportions Over Time for Individual", sample_id),
       x = "Time Point",
       y = "Adjusted GP Mean",
       color = "Topic") +scale_color_d3(
  palette = c("category20"),alpha = 1)+
  theme_minimal())


#data.frame(data_grouped[data_grouped$eid%in%sample_id,])

disease_merge=merge(data,ATM::disease_info_phecode_icd10,by.x="diag_icd10",by.y="phecode")
d=disease_merge[disease_merge$eid%in%sample_id,]%>%arrange(round_age)
print(d[,c("round_age","phenotype","exclude_name")])



#nw <- loading2weights(HES_age_example, ds_list = UKB_349_disease, topics = UKB_HES_10topics)
#saveRDS(nw,"~/Library/CloudStorage/Dropbox-Personal/ATM_simweights.rds")
nw=readRDS("~/Library/CloudStorage/Dropbox-Personal/ATM_simweights.rds")
new_weights=nw$topic_weights
tw=data.frame(new_weights[new_weights$eid%in%sample_id,])
names(tw)[2:11]=topics

mt=melt(tw,id.vars="eid")

gp=ggplot(mt,aes(as.factor(eid),value,fill=variable))+geom_bar(stat="identity")+
  labs(y="Topic Weight",x="Pt ID",fill="Inferred Topic")+scale_fill_d3(
  palette = c("category20"),alpha = 1)
print(gp)
}

   round_age                                           phenotype
1         42               Thyrotoxicosis with or without goiter
2         42                                  Hypothyroidism NOS
3         42                                     Type 1 diabetes
4         43                                     Type 2 diabetes
5         46                                  Reflux esophagitis
6         47                                          Hemiplegia
7         47              Rheumatism, unspecified and fibrositis
8         48                         Osteoarthrosis, generalized
9         49                                   Chronic sinusitis
10        50                              Essential hypertension
11        51   Other chronic ischemic heart disease, unspecified
12        51                              Lump or mass in breast
13        52                             Pulmonary heart disease
14        53                                            Migraine
15        54                   Inflammatory and toxic neuropathy
16        54           Retinal vascular changes and abnomalities
17        54                            Coronary atherosclerosis
18        55                                   Cerebral ischemia
19        56 Phlebitis and thrombophlebitis of lower extremities
20        57                    Myalgia and myositis unspecified
21        58                Hemorrhage of gastrointestinal tract
22        59          Disorders of calcium/phosphorus metabolism
          exclude_name
1  endocrine/metabolic
2  endocrine/metabolic
3  endocrine/metabolic
4  endocrine/metabolic
5            digestive
6         neurological
7      musculoskeletal
8      musculoskeletal
9          respiratory
10  circulatory system
11  circulatory system
12       genitourinary
13  circulatory system
14        neurological
15        neurological
16        sense organs
17  circulatory system
18  circulatory system
19  circulatory system
20            symptoms
21           digestive
22 endocrine/metabolic

   round_age                                                     phenotype
1         44                                         Chronic periodontitis
2         45                                               Hypotension NOS
3         45                                        Lump or mass in breast
4         48                        Esophagitis, GERD and related diseases
5         49                                                       Bipolar
6         49                                      Irritable Bowel Syndrome
7         50                        Rheumatism, unspecified and fibrositis
8         51                                  Other disorders of intestine
9         52                                       Urinary tract infection
10        54                                            Hypothyroidism NOS
11        55             Other chronic ischemic heart disease, unspecified
12        55                            Cellulitis and abscess of arm/hand
13        56 Iron deficiency anemias, unspecified or not due to blood loss
14        56                                              Umbilical hernia
15        56                                         Chronic ulcer of skin
16        56                           Other acquired deformities of limbs
17        58                                    Chronic airway obstruction
18        58  Thoracic or lumbosacral neuritis or radiculitis, unspecified
19        59                                                        Asthma
20        59                           Other and unspecified disc disorder
21        60                                                  Enthesopathy
22        60                                              Osteoporosis NOS
23        61                                               Arthropathy NOS
          exclude_name
1            digestive
2   circulatory system
3        genitourinary
4            digestive
5     mental disorders
6            digestive
7      musculoskeletal
8            digestive
9        genitourinary
10 endocrine/metabolic
11  circulatory system
12        dermatologic
13       hematopoietic
14           digestive
15        dermatologic
16     musculoskeletal
17         respiratory
18            symptoms
19         respiratory
20     musculoskeletal
21     musculoskeletal
22     musculoskeletal
23     musculoskeletal

   round_age                                                     phenotype
1         49              Unstable angina (intermediate coronary syndrome)
2         50           Cerebral artery occlusion, with cerebral infarction
3         50                                            Calculus of ureter
4         51 Iron deficiency anemias, unspecified or not due to blood loss
5         52                                                Pyelonephritis
6         52                                            Calculus of kidney
7         53            Other hypertrophic and atrophic conditions of skin
8         54                                        Essential hypertension
9         56                       Late effects of cerebrovascular disease
10        56                                          Diaphragmatic hernia
11        56                                                Blood in stool
12        57                                           Respiratory failure
13        58                                    Chronic airway obstruction
14        58                          Cholecystitis without cholelithiasis
15        59                      Other chronic nonalcoholic liver disease
16        59                                                Cholelithiasis
17        60                                           Visual disturbances
18        60                                                        Asthma
19        60                                                Diverticulosis
20        60                       Cholelithiasis with other cholecystitis
21        65                                        Pneumococcal pneumonia
22        66                                       Streptococcus infection
23        66                                                  Hypoglycemia
24        66                                       Cerebrovascular disease
25        66                               Functional disorders of bladder
26        66                                                 Pain in joint
          exclude_name
1   circulatory system
2   circulatory system
3        genitourinary
4        hematopoietic
5        genitourinary
6        genitourinary
7         dermatologic
8   circulatory system
9   circulatory system
10           digestive
11           digestive
12         respiratory
13         respiratory
14           digestive
15           digestive
16           digestive
17        sense organs
18         respiratory
19           digestive
20           digestive
21         respiratory
22 infectious diseases
23 endocrine/metabolic
24  circulatory system
25       genitourinary
26     musculoskeletal

   round_age                                                   phenotype
1         33                                                   Back pain
2         34                               Noninfectious gastroenteritis
3         36                                  Acquired spondylolisthesis
4         37                                    Irritable Bowel Syndrome
5         37                    Neuralgia, neuritis, and radiculitis NOS
6         38                                                        GERD
7         38                                             Arthropathy NOS
8         40                                      Essential hypertension
9         41                                   Staphylococcus infections
10        41                                                     E. coli
11        41                                             Angina pectoris
12        41                                             Hypotension NOS
13        42                                     Bacterial infection NOS
14        45                         Noninflammatory disorders of vagina
15        45                Disorder of skin and subcutaneous tissue NOS
16        46                                          Ulcer of esophagus
17        47                                    Gastritis and duodenitis
18        48                                             Decubitus ulcer
19        50 Pulmonary collapse; interstitial and compensatory emphysema
20        50                                                Constipation
          exclude_name
1             symptoms
2            digestive
3      musculoskeletal
4            digestive
5             symptoms
6            digestive
7      musculoskeletal
8   circulatory system
9  infectious diseases
10 infectious diseases
11  circulatory system
12  circulatory system
13 infectious diseases
14       genitourinary
15        dermatologic
16           digestive
17           digestive
18        dermatologic
19         respiratory
20           digestive

   round_age                                           phenotype
1         54                                  Hypothyroidism NOS
2         54                              Essential hypertension
3         54                       Hemorrhage of rectum and anus
4         55                                             Obesity
5         55                                      Ventral hernia
6         57                                     Type 2 diabetes
7         57                            Coronary atherosclerosis
8         58                              Polymyalgia Rheumatica
9         59                                     Senile cataract
10        59                             Orthostatic hypotension
11        59                                Urinary incontinence
12        60                                Rheumatoid arthritis
13        61                                Diabetic retinopathy
14        61                                         Hemorrhoids
15        61                      Other disorders of gallbladder
16        61                                         Hematemesis
17        61               Congenital anomalies of great vessels
18        62                           Staphylococcus infections
19        62      Type 2 diabetes with ophthalmic manifestations
20        62                                        Hypoglycemia
21        62                                     Hypotension NOS
22        62                              Pneumococcal pneumonia
23        62                       Noninfectious gastroenteritis
24        62                             Urinary tract infection
25        63              Lipoma of skin and subcutaneous tissue
26        63                               Chronic ulcer of skin
27        65 Phlebitis and thrombophlebitis of lower extremities
28        67                                Aortic valve disease
29        68                     Atrial fibrillation and flutter
30        68                         Chronic renal failure [CKD]
           exclude_name
1   endocrine/metabolic
2    circulatory system
3             digestive
4   endocrine/metabolic
5             digestive
6   endocrine/metabolic
7    circulatory system
8       musculoskeletal
9          sense organs
10   circulatory system
11        genitourinary
12      musculoskeletal
13  endocrine/metabolic
14   circulatory system
15            digestive
16            digestive
17 congenital anomalies
18  infectious diseases
19  endocrine/metabolic
20  endocrine/metabolic
21   circulatory system
22          respiratory
23            digestive
24        genitourinary
25            neoplasms
26         dermatologic
27   circulatory system
28   circulatory system
29   circulatory system
30        genitourinary

   round_age                                              phenotype
1         31                    Chronic tonsillitis and adenoiditis
2         31                        Other upper respiratory disease
3         32                        Atrial fibrillation and flutter
4         32                                      Heart failure NOS
5         32                   Cholecystitis without cholelithiasis
6         32                       Myalgia and myositis unspecified
7         33                                Bacterial infection NOS
8         33                              Staphylococcus infections
9         34                                   Hypercholesterolemia
10        35                                     Reflux esophagitis
11        36                                         Hyperlipidemia
12        40 Other local infections of skin and subcutaneous tissue
13        43                    Hypertensive chronic kidney disease
14        43                                                   GERD
15        44                                        Type 2 diabetes
16        45                                                Obesity
17        46                                           Constipation
18        48                              Osteoarthritis; localized
          exclude_name
1          respiratory
2          respiratory
3   circulatory system
4   circulatory system
5            digestive
6             symptoms
7  infectious diseases
8  infectious diseases
9  endocrine/metabolic
10           digestive
11 endocrine/metabolic
12        dermatologic
13  circulatory system
14           digestive
15 endocrine/metabolic
16 endocrine/metabolic
17           digestive
18     musculoskeletal

   round_age                                          phenotype
1         38          Other disorders of the kidney and ureters
2         39                                     Ingrowing nail
3         44                                          Back pain
4         46                             Essential hypertension
5         46                 Osteoarthrosis, localized, primary
6         47              Other disorders of bone and cartilage
7         50                          Staphylococcus infections
8         50 Other hypertrophic and atrophic conditions of skin
9         52                            Benign neoplasm of skin
10        52                                     Ventral hernia
11        52                            Urinary tract infection
12        54               Scar conditions and fibrosis of skin
13        55                                        Hematemesis
14        56                              Myocardial infarction
15        57              Thyrotoxicosis with or without goiter
16        57                                        Hemorrhoids
17        58                        Transient cerebral ischemia
          exclude_name
1        genitourinary
2         dermatologic
3             symptoms
4   circulatory system
5      musculoskeletal
6      musculoskeletal
7  infectious diseases
8         dermatologic
9            neoplasms
10           digestive
11       genitourinary
12        dermatologic
13           digestive
14  circulatory system
15 endocrine/metabolic
16  circulatory system
17  circulatory system

   round_age                       phenotype        exclude_name
1         37       Prolapse of vaginal walls       genitourinary
2         38      Other disorders of bladder       genitourinary
3         39        Benign neoplasm of colon           neoplasms
4         40                         Obesity endocrine/metabolic
5         40      Anal and rectal conditions           digestive
6         41                   Endometriosis       genitourinary
7         45                     Candidiasis infectious diseases
8         45                      Duodenitis           digestive
9         48 Other disorders of soft tissues     musculoskeletal
10        49        Gastritis and duodenitis           digestive
11        49 Functional disorders of bladder       genitourinary
12        49                    Pain in limb            symptoms
13        50                  Ventral hernia           digestive
14        50    Other intestinal obstruction           digestive
15        52            Hypercholesterolemia endocrine/metabolic
16        53           Chronic ulcer of skin        dermatologic
17        54            Tobacco use disorder    mental disorders

   round_age                                phenotype          exclude_name
1         59                 Bladder neck obstruction         genitourinary
2         62                          Arthropathy NOS       musculoskeletal
3         62             Internal derangement of knee injuries & poisonings
4         63                          Type 2 diabetes   endocrine/metabolic
5         64                     Hypercholesterolemia   endocrine/metabolic
6         65                          Spinal stenosis       musculoskeletal
7         65                Osteoarthritis; localized       musculoskeletal
8         66                          Angina pectoris    circulatory system
9         66 Peripheral vascular disease, unspecified    circulatory system
10        66                  Urinary tract infection         genitourinary
11        66                  Hyperplasia of prostate         genitourinary
12        68              Osteoarthrosis, generalized       musculoskeletal
13        69                              Sleep apnea          neurological
14        69               Chronic airway obstruction           respiratory
15        70                  Bacterial infection NOS   infectious diseases
16        70               Pleurisy; pleural effusion           respiratory
17        70                 Cyst of kidney, acquired         genitourinary
18        71               Other disorders of bladder         genitourinary
19        72  Late effects of cerebrovascular disease    circulatory system
20        73                    Myocardial infarction    circulatory system
21        73       Congestive heart failure (CHF) NOS    circulatory system

   round_age                                          phenotype
1         60                                    Type 1 diabetes
2         60   Unstable angina (intermediate coronary syndrome)
3         60  Other chronic ischemic heart disease, unspecified
4         61            Strabismus (not specified as paralytic)
5         62                           Benign neoplasm of ovary
6         64                                 Hypothyroidism NOS
7         64                    Other upper respiratory disease
8         64          Other diseases of respiratory system, NEC
9         65                     Occlusion of cerebral arteries
10        66 Benign neoplasm of other parts of digestive system
11        66     Type 2 diabetes with ophthalmic manifestations
12        66                              Myocardial infarction
13        66                                  Cerebral ischemia
14        67                 Congestive heart failure (CHF) NOS
15        67                                      Gastric ulcer
16        67                           Gastritis and duodenitis
17        67                                    Acute gastritis
18        67                              Anal and rectal polyp
19        68                Hypertensive chronic kidney disease
20        69                                       Cardiomegaly
21        70                                     Hyperlipidemia
22        71             Rheumatism, unspecified and fibrositis
23        71                                   Osteoporosis NOS
24        73                            Other retinal disorders
          exclude_name
1  endocrine/metabolic
2   circulatory system
3   circulatory system
4         sense organs
5            neoplasms
6  endocrine/metabolic
7          respiratory
8          respiratory
9   circulatory system
10           neoplasms
11 endocrine/metabolic
12  circulatory system
13  circulatory system
14  circulatory system
15           digestive
16           digestive
17           digestive
18           digestive
19  circulatory system
20  circulatory system
21 endocrine/metabolic
22     musculoskeletal
23     musculoskeletal
24        sense organs