You are here

Sampling weights for mxFitFunctionMultigroup()

7 posts / 0 new
Last post
Manuel Rein's picture
Offline
Joined: 04/01/2024 - 14:29
Sampling weights for mxFitFunctionMultigroup()

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

mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
Use the weight argument in mxData

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!

Manuel Rein's picture
Offline
Joined: 04/01/2024 - 14:29
Thank you!

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.

mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
Try mxFitFunctionAlgebra()

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 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')
)
Manuel Rein's picture
Offline
Joined: 04/01/2024 - 14:29
Yes, I also found the

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.

AdminNeale's picture
Offline
Joined: 03/01/2013 - 14:09
Please share whatever crashed R for you

Hi Manuel

Could you please provide a script that crashes R, including any data that it needs? We all hate it when R crashes, and it is almost certainly a bug, which we would like to repair.

Cheers
Mike

mhunter's picture
Offline
Joined: 07/31/2009 - 15:26
No script needed

I already made a script that replicates the crash.