Search code examples
rnlpcluster-analysisquanteda

multiple co-occurence clusters on single term


I have corpus in which a key term occurs at least once. From this I made fcm that looks much like this.

txts <- c("a a a b b c", "a a c e", "a c b e f g", "e d j b", "b g k l", "b a a g l", "e c b j k l", "b g w m")
total <- fcm(txts, context = "document", count = "frequency")

Feature co-occurrence matrix of: 12 by 12 features.
12 x 12 sparse Matrix of class "fcm"
    features
features a b c e f g d j k l w m
   a 5 9 6 3 1 3 0 0 0 2 0 0
   b 0 1 4 3 1 4 1 2 2 3 1 1
   c 0 0 0 3 1 1 0 1 1 1 0 0
   e 0 0 0 0 1 1 1 2 1 1 0 0
   f 0 0 0 0 0 1 0 0 0 0 0 0
   g 0 0 0 0 0 0 0 0 1 2 1 1
   d 0 0 0 0 0 0 0 1 0 0 0 0
   j 0 0 0 0 0 0 0 0 1 1 0 0
   k 0 0 0 0 0 0 0 0 0 2 0 0
   l 0 0 0 0 0 0 0 0 0 0 0 0
   w 0 0 0 0 0 0 0 0 0 0 0 1
   m 0 0 0 0 0 0 0 0 0 0 0 0

From this, I would like to find the different clusters around 'b'.

With an eye on scaling, my actual fcm has 239104369 elements and a size of 1.2GB.

A matrix of its first 10 features looks like this

Feature co-occurrence matrix of: 10 by 10 features.
10 x 10 sparse Matrix of class "fcm"
           features
features        international monetary    fund development association bolivia assessment interim  poverty reduction
international       2885797  1345055 3340282    12013377      857864  199985     605036  202117  3996710   1319199
monetary                  0   227329  973979     2326677      234565   39802      93927   65773   884341    330250
fund                      0        0 1766657     6530594      621315   99900     355415  204229  2534382    927737
development               0        0       0    20054398     1683896  485906    2235294  406575 13674085   4091506
association               0        0       0           0      122947   25954      87756   47038   580721    204144
bolivia                   0        0       0           0           0   26062      35164    5336   254924     71428
assessment                0        0       0           0           0       0     203933   24196  1420850    377398
interim                   0        0       0           0           0       0          0   20595   172870     67705
poverty                   0        0       0           0           0       0          0       0  9131869   4026961
reduction                 0        0       0           0           0       0          0       0        0    642944

My goal is to visualise the clusters around the key-term (https://bost.ocks.org/mike/miserables/) and to create term lists from it.

https://www.r-bloggers.com/turning-keywords-into-a-co-occurrence-network/

https://www.r-bloggers.com/collapsing-a-bipartite-co-occurrence-network/

Co occurrence plot in R

In my search I also stumbled on the cooccurNet package, but I don't how to adept it. https://cran.r-project.org/web/packages/cooccurNet/index.html


Solution

  • quanteda has textstat_simil() that returns a dist object for hierarchical clustering. This function only takes a DFM but an FCM can be converted to the object using as.dfm().

    require(quanteda)
    txt <- c("a a a b b c", "a a c e", "a c b e f g", "e d j b", "b g k l", "b a a g l", "e c b j k l", "b g w m")
    dmt <- dfm(txt)
    # dmt <- dfm_trim(dmt, min_termfreq = 10) # you might need this to reduce the size of fcm
    fmt <- fcm(dmt, context = "document")
    
    dist <- textstat_simil(as.dfm(fmt), margin = "features")
    tree <- hclust(dist)
    cutree(tree, 2)