From ad4145e38d5d869bd47ad230e43994efe67eb463 Mon Sep 17 00:00:00 2001 From: Xiyu Date: Mon, 22 Apr 2024 17:18:04 -0400 Subject: [PATCH] Add notation of the output --- R/Gibbs_sampler.R | 15 ++++++++++++++- README.md | 32 +++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/R/Gibbs_sampler.R b/R/Gibbs_sampler.R index adf3025..73a85da 100644 --- a/R/Gibbs_sampler.R +++ b/R/Gibbs_sampler.R @@ -346,6 +346,8 @@ SpaTopic_inference<-function(tissue, ntopics, sigma = 50, region_radius = 400, k } ### release memory for large items + celltype<-levels(itr_df$type) + cellname<-rownames(itr_df) rm(list= c("itr_df")) gc() @@ -371,7 +373,8 @@ SpaTopic_inference<-function(tissue, ntopics, sigma = 50, region_radius = 400, k if(ninit > 1){ ## need update only when several initialization ## number of regions - M<-max(D)+1 ## M might be changed since D changed + M<-max(D)+1 ## M might be changed since D changed + # A potential bug (what if the max(D) has been changed after gibbs sampling) Ndk <- table_2d_fast(D, Z, M, K) ## number of cells per topic per region (image specific) Nwk <- table_2d_fast(C, Z, V, K) ## number of cells per topic per celltype @@ -408,6 +411,16 @@ SpaTopic_inference<-function(tissue, ntopics, sigma = 50, region_radius = 400, k message("Output model perplexity:") message(gibbs.res$Perplexity) + ## Beta: Topic Content + gibbs.res$Beta<-as.data.frame(gibbs.res$Beta) + colnames(gibbs.res$Beta)<-paste0("topic",1:K) + rownames(gibbs.res$Beta)<-celltype + + ## Z.trace: posterior prob for every single cell + gibbs.res$Z.trace<-as.data.frame(gibbs.res$Z.trace/niter) + colnames(gibbs.res$Z.trace)<-paste0("topic",1:K) + rownames(gibbs.res$Z.trace)<-cellname + return(gibbs.res) } diff --git a/README.md b/README.md index 2e314fb..9985085 100644 --- a/README.md +++ b/README.md @@ -69,15 +69,29 @@ It is stored in a Seurat v5 object. ``` r str(gibbs.res) -#List of 8 -# $ Perplexity : num 11.3 -# $ Deviance : num 485960 -# $ loglikelihood: num -242980 -# $ Beta : num [1:38, 1:7] 0.03587 0.02539 0.00755 0.01858 0.02585 ... -# $ Theta : num [1:971, 1:7] 0.855601 0.000232 0.999269 0.99889 0.998725 ... -# $ Ndk : int [1:971, 1:7] 107 0 82 54 47 72 100 0 0 0 ... -# $ Nwk : int [1:38, 1:7] 390 276 82 202 281 505 697 522 29 58 ... -# $ Z.trace : int [1:100149, 1:7] 13 173 27 164 157 4 20 21 15 19 ... +# List of 8 +# $ Perplexity : num 11.3 +# $ Deviance : num 485960 +# $ loglikelihood: num -242980 +# $ Beta :'data.frame': 38 obs. of 7 variables: +# ..$ topic1: num [1:38] 0.03587 0.02539 0.00755 0.01858 0.02585 ... +# ..$ topic2: num [1:38] 6.51e-03 3.55e-02 2.62e-06 5.80e-04 7.75e-01 ... +# ..$ topic3: num [1:38] 4.54e-06 4.54e-06 9.13e-04 3.45e-01 1.73e-03 ... +# ..$ topic4: num [1:38] 0.02664 0.01743 0.00186 0.0152 0.08919 ... +# ..$ topic5: num [1:38] 2.99e-06 2.99e-06 5.32e-03 1.91e-02 4.90e-03 ... +# ..$ topic6: num [1:38] 6.35e-06 6.35e-06 2.04e-02 3.43e-03 6.35e-06 ... +# ..$ topic7: num [1:38] 0.00534 0.00699 0.00604 0.01843 0.00655 ... +# $ Theta : num [1:971, 1:7] 0.855601 0.000232 0.999269 0.99889 0.998725 ... +# $ Ndk : int [1:971, 1:7] 107 0 82 54 47 72 100 0 0 0 ... +# $ Nwk : int [1:38, 1:7] 390 276 82 202 281 505 697 522 29 58 ... +# $ Z.trace :'data.frame': 100149 obs. of 7 variables: +# ..$ topic1: num [1:100149] 0.065 0.865 0.135 0.82 0.785 0.02 0.1 0.105 0.075 0.095 ... +# ..$ topic2: num [1:100149] 0 0 0 0 0 0 0 0 0 0 ... +# ..$ topic3: num [1:100149] 0.275 0.005 0.21 0.005 0.005 0.77 0.02 0.015 0.085 0.075 ... +# ..$ topic4: num [1:100149] 0.415 0 0 0.01 0.005 0.1 0.665 0.62 0.015 0.025 ... +# ..$ topic5: num [1:100149] 0.005 0.01 0 0 0 0 0.005 0.005 0.005 0 ... +# ..$ topic6: num [1:100149] 0 0 0.655 0.165 0.205 0.005 0 0 0 0 ... +# ..$ topic7: num [1:100149] 0.24 0.12 0 0 0 0.105 0.21 0.255 0.82 0.805 ... ``` ## Citation