Sampling weights for mxFitFunctionMultigroup()
Posted on
Manuel Rein
Joined: 04/01/2024
Forums
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
Use the weight argument in mxData
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 withmxFitFunctionMultigroup()
.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!
Log in or register to post comments
Thank you!
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.
Log in or register to post comments
Try mxFitFunctionAlgebra()
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')
)
Log in or register to post comments
In reply to Try mxFitFunctionAlgebra() by mhunter
Yes, I also found the
Thanks for the advice on using mxAlgebraFromString()! I used eval(parse(...)) to turn my string into an object, but this seems easier.
Log in or register to post comments
In reply to Yes, I also found the by Manuel Rein
Please share whatever crashed R for you
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
Log in or register to post comments
In reply to Please share whatever crashed R for you by AdminNeale
No script needed
Log in or register to post comments