How calculate the frequency distribution based on probability given by GMM (mclust)?

I am currently trying to perform clustering on 1-D data using GMM. The idea is to separate 2 populations (k=2). Even though the major part of my code is in Python (not displayed here), I use mclust in R which is more adapted than scikit-learn in my case as it automatically adjusts the model used (E for equal variance and V for varying variance). Once done, I'd like to reconstruct the individual distribution of each cluster, even the initially missing part, using the probabilities given by mclust. The aim is to have the frequency (and not the density!) vs the units of my data. Right now 1 I only use geom_line to display the estimate of the density that I convert into a frequency. But I guess that using the probabilities given by mclust would be more reliable. Does anyone have an idea how I could do that?

Here is the Python code:

import rpy2
import rpy2.situation
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
import pandas as pd
from plotnine import *
from plotnine.stats import *
import warnings
import os
import matplotlib.pyplot as plt
warnings.filterwarnings("ignore")
pandas2ri.activate()


# import R's utility package
utils = rpackages.importr('utils')

utils.chooseCRANmirror(ind=1)

path = os.getcwd()
name = input("Enter filename ")
name = name+".csv"

for root, dirs, files in os.walk(path):
    if name in files:
        print(os.path.join(root, name))
        v=os.path.join(root, name)

# R package names
packnames = ('mclust','ggplot2')

# Selectively install what needs to be install.
names_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
if len(names_to_install) > 0:
    utils.install_packages(StrVector(names_to_install))


name1 = "mclustnano.R"

for root, dirs, files in os.walk(path):
    if name1 in files:
        print(os.path.join(root, name1))
        v1=os.path.join(root, name1)

r = robjects.r
r['source'](v1)
# Loading the function we have defined in R.
mclust_app = robjects.globalenv['applied_mclust']
# Reading and processing data
df = pd.read_csv(v, sep=";")
#converting it into r object for passing into r function
df_r = robjects.conversion.py2rpy(df)
#Invoking the R function and getting the result
df_result_r = mclust_app(df_r)
#Converting it back to a pandas dataframe.
df_result = robjects.conversion.rpy2py(df_result_r)

df_result['cat'] = df_result['Classification'].astype('category')
print(df_result)

binwith=(max(df_result["Data"])-min(df_result["Data"]))/100

c2=df_result[df_result['Classification']==2]["cat"].count()
c1=df_result[df_result['Classification']==1]["cat"].count()

a=(
   ggplot() +
  geom_histogram(df_result,aes(x="Data",y = "..count.."), 
           #bins = 100,
           binwidth = binwith,
           colour = "black" 
           ) 
  +geom_line(df_result[df_result['cat']==1] ,aes(x="Data",y ="..density..     *c1*binwith"), colour = "red",size = 1, stat = 'density')
  +geom_line(df_result[df_result['cat']==2] ,aes(x="Data",y = "..density.. *c2*binwith"), colour = "green",size = 1, stat = 'density')
  +geom_line(df_result ,aes(x="Data",y = "..density.. *(c1+c2)*binwith"), colour =     "yellow",size = 1, stat = 'density')
#+coord_cartesian(ylim=(0,150), xlim=(0,1000000))

)

a.draw(return_ggplot=True, show=True)

And here is the R code:

library(mclust)
applied_mclust <- function(df){

  kr1<-Mclust(df$Data,G=2)
  print(kr1$density[,2]) 


  df<-cbind(kr1$classification,kr1$data,kr1$z[,2])
  df<-data.frame(df)
  colnames(df)[1]<-"Classification"
  colnames(df)[2]<-"Data"
  colnames(df)[3]<-"Proba"
  print(summary(kr1, parameters = TRUE))
  return(df)
}

And here is a sample of my data: Data 1309009. 7364522. 2899284. 2295116. 1895820. 3399268. 2444428. 3041644. 1579848. 3180522. 2326360. 1857622. 3031202. 3788140. 1677066. 9357568. 1902764. 1725676. 2246494. 1982620. 2315956. 2093726. 2319422. 2128446. 2347204. 2041644. 4517340. 1583320. 2850664. 1690952. 3621504. 1270818. 14319344. 2920112. 1309015. 1774282. 1947898. 1458316. 2420112. 3229124. 4406232. 2211774. 2315946. 3916616. 1645812. 7920120. 6562388. 1427072. 1802066. 2374972. 1329848. 1520818. 2468724. 1444430. 2104144. 2486078. 1569427. 2836768. 1107631. 1666652. 1826368. 1979146. 6979088. 1614570. 1336792. 2656228. 1638868. 1256930. 3017324. 1364572. 2656220. 1979142. 4503456. 1784704. 1256930. 2784688. 1621511. 1572898. 2361088. 1760396. 4805542. 5538134. 2065958. 1847202. 2111084. 1788172. 1895816. 2222204. 3600642. 2017344. 1604150. 3086772. 1579848. 2552046. 3868020. 1454850. 1604154. 1784704. 2093728. 2979132. 2607620. 1958314. 1715266. 1531236. 2138862. 1503460. 1739570. 10301952. 1763868. 2604134. 2052060. 1524290. 3604134. 1864572. 4013860. 2545118. 1406234. 2378448. 1947900. 1593728. 6753436. 1489570. 1180542. 1847200. 2680524. 2343728. 2281228. 5489512. 2527766. 1479156. 1746512. 1767342. 1684016. 1993032. 1937482. 2149279. 4357594. 2427062. 5277720. 2534704. 6909656. 1670126. 1638874. 2815958. 2763864. 2222204. 1576376. 2486092. 2253452. 2368032. 2927050. 2840244. 1388878. 5864544. 2565944. 1947902. 2177062. 2190944. 6576288. 2364564. 2249984. 2472196. 1999978. 2683996. 2760388. 2277746. 2263862. 2055534. 4618038. 1322906. 18013708. 2861084. 1309014. 7781192. 4034682. 2708308. 2065950. 2982618. 1170126. 3961756. 1850676. 2524284. 2392328. 2319426. 2374972. 1770812. 1663184. 6423486. 1729152. 2013874. 1545121. 1309020. 1993038. 1812476. 3309002. 1913174. 1378462. 1916648. 2243026. 3972192. 1868040. 1506926. 2454836. 1871510. 1534704. 6902688. 2083318. 2045124. 1788178. 2576352. 2364556. 1663174. 3204818. 5524286. 1840264. 1708312. 1708316. 1197906. 1975668. 1583322. 2399286. 2440944. 2850672. 1458320. 1531230. 2309000. 2645790. 1993032. 2611091. 1409706. 5440880. 1708314. 1343740. 1736105. 1593736. 3211782. 1607624. 2416646. 1888866. 1649290. 2920118. 1576374. 2722178. 1475681. 1604154. 1822895. 1499984. 1270821. 1965254. 1336792. 1315960. 1795122. 2336786. 2586790. 2371484. 1583316. 1999976. 1406232. 1576375. 2329836. 2236090. 5100640. 1204848. 1347206. 1760400. 4034686. 5892306. 1163183. 3062456. 1041654. 1437486. 1458318. 1295128. 2239566. 5951310. 1989562. 1208324. 2513856. 1170127. 3065926. 5340224. 1878454. 1892346. 3097204. 1531232. 1611096. 1902760. 1635402. 3194410. 2211780. 2562472. 1392350. 1947900. 3166642. 1413176. 1506926. 1315958. 2468722. 1552067. 2659686. 1628456. 2545120. 1697894. 14986064. 2513856. 1749982. 6083250. 1934012. 2097192. 1732624. 5465244. 1531240. 6437426. 1701372. 1340264. 1940960. 1451378. 3020808. 2437466. 2406230. 1989560. 7920058. 2069424. 2673582. 1437490. 1409711. 1614569. 2895808. 1909706. 3874974. 2097198. 3361073. 2041640. 4142311. 4742996. 1746510. 2232624. 3204834. 1795120. 2822884. 1083324. 1840264. 1881924. 3243030. 1885400. 1496508. 3999967. 1552066. 5506864. 3871508. 1114572. 2590252. 2020810. 2263864. 1795118. 2722198. 1947900. 4135392. 1770812. 2840248. 1583312. 4815920. 2437480. 1315960. 1232627. 2180540. 4680476. 2701364. 1732618. 1861088. 2857608. 2270810. 3003442. 1361096. 3138848. 2503448. 1597204. 1996512. 1836790. 2531235. 1565954. 3357606. 3722176. 1930544. 5812424. 2052062. 6451302. 1777764. 2305532. 2270808. 1399292. 1791642. 1541650. 6614517. 2704840. 2576358. 1930544. 1878460. 2614552. 1777762. 1222209. 2128458. 2444414. 1538182. 2475680. 1836786. 1767348. 14003322. 6833200. 1489564. 2260394. 4722152. 1951370. 2048594. 1937480. 5149250. 3034692. 2871490. 4885364. 1326374. 2687478. 2374988. 1857622. 3583282. 15524228. 4899261. 2263864. 1524290. 2753444. 4302040. 2256932. 3204816. 1579844. 1729150. 3920092. 3972210. 1618040. 1697898. 1079852. 2402760. 1479150. 2420110. 1586794. 3079814. 5663112. 8614516. 2069420. 3479136. 1413186. 6291614. 3229120. 1114573. 1628456. 2843724. 2336784. 1718734. 1993040. 2246514. 2822886. 1986094. 2600662. 1447894. 1559014. 2062480. 1937484. 3274286. 2645798. 7680458. 3683992. 6104094. 4045116. 3166628. 1281236. 2736082. 1618040. 1732622. 1878448. 1725684. 2774258. 3343718. 6329796. 1843734. 1510400. 2513868. 2277752. 3347174. 2027752. 3152732. 3670096. 2298592. 1798588. 2177062. 2423588. 2260392. 2781212. 5076340. 3128434. 1878458. 2156228. 1999984. 2131916. 2743022. 2527756. 2687464. 2552062. 8690938. 2121512. 2263868. 2229144. 2749960. 1965266. 2274285. 1347206. 3406224. 3260390. 1302074. 1579846. 1128464. 2364552. 3069408. 1704852. 2364564. 2222206. 1274290. 1961788. 2031228. 2430528. 1979148. 1607630. 1972206. 1763864. 2086780. 10836576. 1593736. 3183996. 3993036. 1454852. 2569416. 2197892. 2433998. 2746510. 2326368. 3069424. 1951364. 1763876. 6579736. 2135402. 7562472. 1760398. 2187478. 2086799. 2149280. 1479150. 6576328. 3222178. 2263860. 1836784. 1531238. 2829830. 1739566. 1597204. 2218724. 3885374. 1538178. 4204808. 2513868. 3850644. 1812484. 5263844. 1934011. 1954844. 1986100. 3562464. 2378440. 1704846. 2225670. 1812484. 2208312. 2611084. 2354146. 1722200. 2204840. 2024290. 2281230. 1489570. 3399268. 2003456. 5840229. 2124976. 2194432. 1986088. 2156228. 2392341. 2548572. 1899290. 1472212. 1645810. 1642342. 1805540. 5420060. 4437471. 2510392. 1777762. 2322892. 1659708. 1822896. 2111092. 3552060. 1861096. 1249990. 2291644. 3437456. 1687488. 19617736. 2468732. 1489570. 2111096. 3003442. 1260397. 1746510. 1927060. 1437484. 2958319. 1697900. 2159700. 2027750. 1493040. 1993044. 12621450. 1763870. 2048586. 1847200. 1569428. 4156224. 1572904. 1684014. 2479144. 1809002. 2579832. 2659702. 2784704. 1760394. 1402766. 2538172. 2743030. 2583297. 2152754. 1416650. 1409708. 1673596. 5093698. 9367902. 2163176. 1829840. 1628456. 1444430. 2579844. 2541642. 1638878. 3395820. 1506927. 1944426. 2850656. 7600670. 1190964. 7083290. 4888844. 2156232. 2937450. 1972206. 2090256. 2461788. 1680536. 5034688. 1809008. 2930522. 2163174. 2989558. 2645808. 4729144. 2027765. 1687480. 10076244. 13506830. 1482626. 2440948. 2347200. 1399298. 2378452. 2256924. 4017320. 1746504. 2222198. 2840248. 1097214. 1878456. 1902762. 1784710. 2152752. 1975676. 1857614. 3461770. 4347184. 1343736. 2017342. 2503446. 2024278. 8600644. 19260212. 1774284. 1972202. 1892347. 1541651. 1204848. 1763871. 2208312. 2777752. 1961784. 2475666. 1989562. 3555512. 1739561. 1843728. 1913180. 2135394. 4013856. 2239564. 1815954. 2065948. 1590264. 1885398. 2392344. 1906226. 3003444. 2930528. 1920114. 5836726. 1819426. 2552052. 1652758. 1430540. 2854134. 4538168. 2982602. 1951370. 1923588. 1579842. 2624954. 2840252. 1749982. 2204834. 3267336. 3763844. 1572902. 3079832. 1604146. 2909704. 2329840. 4892312. 4045074. 1958320. 4736060. 3579824. 2159698. 2437468. 2336778. 2430524. 1888868. 2621508. 1711784. 4920094. 3465232. 2267342. 2180532. 2128448. 3895768. 5065914. 2267332. 1857616. 4017316. 2038176. 2857608. 1663172. 6972188. 3055522. 1913178. 3885390. 1802066. 1618036. 2211780. 5131860. 1645816. 1468738. 1895816. 1812482. 5923494. 2024286. 4777732. 1440962. 3104118. 4333290. 3298586. 2357620. 3142332. 2322896. 2597192. 15201120. 2017342. 2232608. 2961772. 11510322. 3024266. 2090250. 2489562. 1663182. 13531168. 10992806. 2690950. 1708318. 2131930. 3618028. 2291638. 1305542. 2302062. 2642330. 1878454. 2371508. 1538176. 1989566. 3284700. 1715264. 2711774. 2256922. 1659702. 3659694. 3385363. 2656223. 2927056. 2343722. 4684004. 2065946. 3607602. 2548578. 1541655. 3798558. 1993044. 5704792. 2298586. 4065942. 5135364. 1784702. 1399290. 2666648. 2347195. 1708316. 1704844. 2003456. 1802070. 1895814. 2142336. 6433952. 3604128. 2364554. 1868036. 1736096. 8298520. 2267344. 2211784. 4381896. 1520810. 2232618. 2177058. 2729136. 1878456. 1989562. 3034700. 3819418. 1677068. 2152760. 2569420. 2083322. 7291548. 2722196. 3708296. 2611082. 2975666. 1874976. 2708309. 3138860. 3558992. 2923598. 2100676. 2954822. 1614576. 2447892. 1840260. 1850674. 1701372. 6569392. 11031076. 2805532. 5937440. 1399292. 1909700. 2989548. 1902756. 2076370. 2656220. 1753462. 2902746. 1795120. 1920120. 3499968. 3461768. 2309000. 1597198. 2947896. 1993040. 1388878. 2670106. 2222196. 2527752. 5468732. 2180530. 6138804. 2277750. 1395814. 1930542. 3593708. 1916652. 1902756. 2059008. 3552036. 2524278. 1822896. 1937476. 1895814. 2090248. 1944422. 2444424. 3979116. 1569428. 2635392. 2656228. 2315960. 1795118. 2638868. 2635394. 2378460. 1999976. 2753444. 1736088. 2211774. 2312476. 1822900. 2666634. 2993028. 2236086. 1822904. 5663132. 3197878. 3208308. 2385396. 2739564. 4916608. 3489556. 2152750. 2149288. 2593728. 2718716. 1927060. 1708318. 2774272. 2406236. 1993040. 2048590. 2048592. 1715262. 3010380. 2184014. 1277766. 1159708. 2156232. 3774270. 2350672. 1993033. 2531224. 1357622. 1347207. 2534696. 1402758. 1812488. 2756916. 2368036. 13864446. 1510408. 9520660. 1878461. 2895808. 3111076.

Thanks is advance.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum