A function that attempts to coerce a theta-like list into a proper formatted
object of class theta
.
as.theta(x)
x | A theta-like object that can be coerced. |
---|
A theta object. See rtheta
.
First, if the list is of length 3 and not 5, the number of components and
dimension is assumed to be missing and added.
Secondly, the class is added.
Thirdly, names are added if needed.
Next, matrix means and array covariances are
coerced to list form.
Covariances on array form are assumed to be d
by d
by m
.
Means on matrix form are as assumed to be d
by m
. I.e.
rows correspond to the dimensions and columns to components, or the mean vectors
as column vectors.
Finally, the sum constraint of 1 for the mixture proportions is enforced.
m <- 2 d <- 3 x <- list(m = m, d = d, pie = c(0.5, 0.5), mu = list(comp1=rep(0,d), comp2=rep(1,d)), sigma = list(comp1=diag(d), comp2=diag(d))) print(x)#> $m #> [1] 2 #> #> $d #> [1] 3 #> #> $pie #> [1] 0.5 0.5 #> #> $mu #> $mu$comp1 #> [1] 0 0 0 #> #> $mu$comp2 #> [1] 1 1 1 #> #> #> $sigma #> $sigma$comp1 #> [,1] [,2] [,3] #> [1,] 1 0 0 #> [2,] 0 1 0 #> [3,] 0 0 1 #> #> $sigma$comp2 #> [,1] [,2] [,3] #> [1,] 1 0 0 #> [2,] 0 1 0 #> [3,] 0 0 1 #> #>#> theta object with d = 3 dimensions and m = 2 components: #> #> $pie #> [1] 0.5 0.5 #> #> $mu #> $mu$comp1 #> [1] 0 0 0 #> #> $mu$comp2 #> [1] 1 1 1 #> #> #> $sigma #> $sigma$comp1 #> [,1] [,2] [,3] #> [1,] 1 0 0 #> [2,] 0 1 0 #> [3,] 0 0 1 #> #> $sigma$comp2 #> [,1] [,2] [,3] #> [1,] 1 0 0 #> [2,] 0 1 0 #> [3,] 0 0 1 #> #>x2 <- unname(list( # Unnamed # missing m and d pie = c(1, 1), # Does not sum to 1 mu = simplify2array(list(comp1=rep(0,d), comp2=rep(1,d))), # matrix, not a list sigma = simplify2array(list(comp1=diag(d), comp2=diag(d))) # array, not a list )) theta2 <- as.theta(x2)#> Warning: x$pie rescaled to enforce sum constraint of 1#> theta object with d = 3 dimensions and m = 2 components: #> #> $pie #> [1] 0.5 0.5 #> #> $mu #> $mu$comp1 #> [1] 0 0 0 #> #> $mu$comp2 #> [1] 1 1 1 #> #> #> $sigma #> $sigma$comp1 #> [,1] [,2] [,3] #> [1,] 1 0 0 #> [2,] 0 1 0 #> [3,] 0 0 1 #> #> $sigma$comp2 #> [,1] [,2] [,3] #> [1,] 1 0 0 #> [2,] 0 1 0 #> [3,] 0 0 1 #> #>