Hi,
I have survey weights in my data. How can I make sure that my charts take these weights into consideration?
Thank you!
Hi,
I have survey weights in my data. How can I make sure that my charts take these weights into consideration?
Thank you!
Hello @Sidhika_Tripathee
can you please elaborate on you data and what you like to archiv?
Best, Michael
Yes, my data consists of respondent data from a survey with each document representing a respondent and how they answer each question. The document for each respondent also includes a weight vector of how much we want to weigh their response in comparison to the entire dataset. I would like a way to apply the weight vector to each of the responses for each respondent and visualize it on MongoDb Charts. I’ve been able to use the raw data and visualize that, but have been unable to apply the weight vector to the raw data and create charts based off that.
Let me know if any other clarifications would be helpful! Thank you.
You can use calculated fields to modify the raw value against each weight, and then build the chart off the calculated values.
Unfortunately that doesn’t work since the weight vector is an array of ints while our other fields are string so I get an error that I can’t multiply the two types. For some of the simple bar charts I’ve been able to add the weight vector and aggregate its sum to get weighted results but this doesn’t work for all the chart types.
You can convert types in the aggregation pipeline if needed. You may need to build a relatively complex pipeline but this will be doable. I can give more specific advice without knowing what the data looks like though.
I ended up using an aggregation pipeline in order to do this. Here’s the code that worked for me!
[
{
"$group": {
"_id": null,
"docs": { "$push": "$$ROOT" },
"totalWeightedValue": {
"$sum": {
"$multiply": ["$weightvec", "$Q2_Field1"]
}
},
"totalWeight": {
"$sum": "$weightvec"
}
}
},
{
"$unwind": "$docs"
},
{
"$addFields": {
"docs.Q2_Field1_weightedAverage": {
"$divide": ["$totalWeightedValue", "$totalWeight"]
}
}
},
{
"$replaceRoot": { "newRoot": "$docs" }
}
]
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.