Home > Software design >  Can we extract an output text from a function in R?
Can we extract an output text from a function in R?

Time:07-08

I have simulation data that it repeated 100 times. I applied a mclustBIC for each sample. Then, I would like to access the top result of this function. However, I could not access it. I provided an example of this function.

library(mclust)
mclustBIC(iris[,-5])

The output is:

    Bayesian Information Criterion (BIC): 
         EII        VII        EEI        VEI        EVI        VVI       EEE       VEE       EVE       VVE       EEV
1 -1804.0854 -1804.0854 -1522.1202 -1522.1202 -1522.1202 -1522.1202 -829.9782 -829.9782 -829.9782 -829.9782 -829.9782
2 -1123.4117 -1012.2352 -1042.9679  -956.2823 -1007.3082  -857.5515 -688.0972 -656.3270 -657.2263 -605.1841 -644.5997
3  -878.7650  -853.8144  -813.0504  -779.1566  -797.8342  -744.6382 -632.9647 -605.3982 -666.5491 -636.4259 -644.7810
4  -893.6140  -812.6048  -827.4036  -748.4529  -837.5452  -751.0198 -646.0258 -604.8371 -705.5435 -639.7078 -699.8684
5  -782.6441  -742.6083  -741.9185  -688.3463  -766.8158  -711.4502 -604.8131        NA -723.7199 -632.2056 -652.2959
6  -715.7136  -705.7811  -693.7908  -676.1697  -774.0673  -707.2901 -609.8543 -609.5584 -661.9497 -664.8224 -664.4537
7  -731.8821  -698.5413  -713.1823  -680.7377  -813.5220  -766.6500 -632.4947        NA -699.5102 -690.6108 -709.9530
8  -725.0805  -701.4806  -691.4133  -679.4640  -740.4068  -764.1969 -639.2640 -654.8237 -700.4277 -709.9392 -735.4463
9  -694.5205  -700.0276  -696.2607  -702.0143  -767.8044  -755.8290 -653.0878        NA -729.6651 -734.2997 -758.9348
        VEV       EVV       VVV
1 -829.9782 -829.9782 -829.9782
2 -561.7285 -658.3306 -574.0178
3 -562.5522 -656.0359 -580.8396
4 -602.0104 -725.2925 -630.6000
5 -634.2890        NA -676.6061
6 -679.5116        NA -754.7938
7 -704.7699 -809.8276 -806.9277
8 -712.8788 -831.7520 -830.6373
9 -748.8237 -882.4391 -883.6931

Top 3 models based on the BIC criterion: 
    VEV,2     VEV,3     VVV,2 
-561.7285 -562.5522 -574.0178 

I want to access the last line and extract values from it (is that possible?)

 Top 3 models based on the BIC criterion: 
        VEV,2     VEV,3     VVV,2 
    -561.7285 -562.5522 -574.0178 

update: using summary() will help to get to this value, but not to extract from it

I tried to solve this point using another way. I first extract only the values, such that:

res <- mclustBIC(iris[,-5])
res1 <- as.data.frame(res[,1:14])
res2 <- max(res1[[1]])

However, res2 will provide me with the maximum value for a specific model. In addition, I need to know the number of clusters (from 1 to 9). I would like to have it like this:

"EII, 9, -694.5205". ## the last line of EII.

CodePudding user response:

A possible solution:

library(mclust)

m <- mclustBIC(iris[,-5])
BIC <-  as.numeric(summary(m))
names(BIC) <- names(summary(m))
BIC

#>     VEV,2     VEV,3     VVV,2 
#> -561.7285 -562.5522 -574.0178
  •  Tags:  
  • r
  • Related