Sampling weights for mxFitFunctionMultigroup()

Posted on
No user picture. Manuel Rein Joined: 04/01/2024
Hi all,

I was wondering if there is a way to specify sampling weights in a multi-group model? I want all observations in a certain group to be weighed equally, but groups with a larger weight should have more influence on the estimation of parameters that are shared/equal between groups.

Best,
Manuel

Replied on Thu, 04/11/2024 - 16:07
Picture of user. mhunter Joined: 07/31/2009

Hi Manuel,

There's nothing special you need to do here. Just specify the weights in the mxData() for each group. The weights will happen to be the same for all rows within a group, and different for different groups, but there's nothing special that needs to happen with mxFitFunctionMultigroup().

Specify the data in each group with mxData(..., weight='someWeight'). The data will look like


> g1data
x someWeight
1 0.1252453 0.6
2 1.1185059 0.6
3 -0.5192638 0.6
4 0.8916350 0.6
5 0.7211928 0.6
6 -0.1211315 0.6
> g2data
x someWeight
1 0.6902390 0.2
2 -1.3505596 0.2
3 1.1450130 0.2
4 -0.8126846 0.2
5 0.2834178 0.2
6 -0.8290546 0.2

Hope that helps!

Replied on Fri, 04/12/2024 - 04:59
No user picture. Manuel Rein Joined: 04/01/2024

Hi Mike,

thanks for your reply! I had tried that argument, but my R crashed repeatedly ("R Session aborted"). It is quite curious, I now tried it again and got this error message: "MxExpectationStateSpace: row frequencies or weights provided in 'person 1.data' are not supported". I then ran the script again without making changes to it, and it crashed again. I can't figure out why it crashes most of the time. In about 10 tries I saw the error only twice, the rest of the time the R Session aborted.

Replied on Fri, 04/12/2024 - 10:01
Picture of user. mhunter Joined: 07/31/2009

Ahh. Well, R shouldn't be crashing in this case. But the error message is correct: we do not currently support sample weights or row frequencies for state space models. The math should work the same as for other models, but I just don't know what it would mean. I've created [an issue on GitHub](https://github.com/OpenMx/OpenMx/issues/389) to resolve the crashing problem, but I do not plan to implement this feature.

You can do what you need with an `mxFitFunctionAlgebra()` instead of an `mxFitFunctionMultigroup()`. It would look something like this


mxModel(...,
mxAlgebra(.2*yourModel1Name.fitfunction + .8*yourModel2Name.fitfunction, name='yourWeightedFit'),
mxFitFunctionAlgebra('yourWeightedFit')
)

If you have a large number of groups you can use `mxAlgebraFromString()`.


weights <- round(runif(10), 2)
modFitNames <- paste0('someModel', 1:10, '.fitfunction')
weightString <- paste0(weights, '*', modFitNames, collapse=' + ')
mod <- mxModel(...,
mxAlgebraFromString(weightString, name='yourWeightedFit'),
mxFitFunctionAlgebra('yourWeightedFit')
)

Replied on Fri, 04/12/2024 - 10:18
No user picture. Manuel Rein Joined: 04/01/2024

In reply to by mhunter

Yes, I also found the mxFitFunctionAlgebra() function earlier today and it seems to be exactly what I need. I've been doing some tests and it works well so far.
Thanks for the advice on using mxAlgebraFromString()! I used eval(parse(...)) to turn my string into an object, but this seems easier.