relabel.RdThe components are sorted by the value of one of the parameters or according to an integer vector containing the permutation of the numbers from 1 to the number of components.
relabel(object, by, ...)
# S4 method for class 'FLXdist,character'
relabel(object, by, which = NULL, ...)An object of class "flexmix".
If a character vector, it needs to be one of "prior",
"model", "concomitant" indicating if the parameter
should be from the component-specific or the concomitant variable
model. If an integer vector it indicates how the components should
be sorted. If missing, the components are sorted by component
size.
Name (or unique substring) of a parameter if by is
equal to "model" or "concomitant".
Currently not used.
set.seed(123)
beta <- matrix(1:16, ncol = 4)
beta
#> [,1] [,2] [,3] [,4]
#> [1,] 1 5 9 13
#> [2,] 2 6 10 14
#> [3,] 3 7 11 15
#> [4,] 4 8 12 16
df1 <- ExLinear(beta, n = 100, sd = .5)
f1 <- flexmix(y~., data = df1, k = 4)
## There was label switching, parameters are not in the same order
## as in beta:
round(parameters(f1))
#> Comp.1 Comp.2 Comp.3 Comp.4
#> coef.(Intercept) 1 5 9 13
#> coef.x1 2 6 10 14
#> coef.x2 3 7 11 15
#> coef.x3 4 8 12 16
#> sigma 0 0 1 1
betas <- rbind(beta, .5)
betas
#> [,1] [,2] [,3] [,4]
#> [1,] 1.0 5.0 9.0 13.0
#> [2,] 2.0 6.0 10.0 14.0
#> [3,] 3.0 7.0 11.0 15.0
#> [4,] 4.0 8.0 12.0 16.0
#> [5,] 0.5 0.5 0.5 0.5
## This makes no sense:
summary(abs(as.vector(betas-parameters(f1))))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.007918 0.025205 0.063959 0.092210 0.117254 0.347028
## We relabel the components by sorting the coefficients of x1:
r1 <- relabel(f1, by = "model", which = "x1")
round(parameters(r1))
#> Comp.1 Comp.2 Comp.3 Comp.4
#> coef.(Intercept) 1 5 9 13
#> coef.x1 2 6 10 14
#> coef.x2 3 7 11 15
#> coef.x3 4 8 12 16
#> sigma 0 0 1 1
## Now we can easily compare the fit with the true parameters:
summary(abs(as.vector(betas-parameters(r1))))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.007918 0.025205 0.063959 0.092210 0.117254 0.347028