-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with inclusion of "weights" #74
Comments
In my brief attempt to replicate the problem, I wasn't able to. However, I have a suspicion that you could check for me. Does this issue keep coming up if you use |
Thank you for the response and help! I appreciate it. Yes, unfortunately, the issue replicates when using Example 1, the mean and SDs are incorrectly calculated when using "weights" in the formula:
Output from Example 1:
Example 2, when removing
Output for Example 2:
Example 3 uses weights and fixes the modx values. Though, the same issue is occuring, where -1 SD should be significant, but it is showing as +1 SD.
Output from Example 3:
|
Checking in to see if you have any additional thoughts on this issue? It does occur with another dataset in our lab, again, when using weights in the lmer model. We tried renaming "weights" column in the event it was conflicting with a variable in the source code, however, it did not change the outcome. Thank you! |
Focusing for the moment on the issue of correctly calculating mean and SD, I am unable to replicate it. Here's my code to generate a fake multilevel dataset with sampling weights then do some basic analyses that don't seem to include mistakes: library(dplyr)
# Set the seed for reproducibility
set.seed(123)
# Define the number of groups and the number of individuals per group
n_groups <- 10
n_individuals_per_group <- 20
# Create a multilevel dataset
data <- expand.grid(group_id = 1:n_groups, individual_id = 1:n_individuals_per_group)
# Generate some group-level and individual-level variables
data$gvar1 <- with(data, ave(group_id, group_id, FUN = function(x) rnorm(1, mean = 100, sd = 15)))
data$gvar2 <- with(data, ave(group_id, group_id, FUN = function(x) rnorm(1, mean = 100, sd = 15)))
data$ivar1 <- rnorm(nrow(data), mean = 100, sd = 15)
data$ivar2 <- data$ivar1 + data$gvar1 + (data$ivar1 * data$gvar1)/100 + rnorm(nrow(data), 0, 15)
# Generate sampling weights (these can be random but should vary)
data$sampling_weight <- runif(nrow(data), min = 0.5, max = 2)
library(lme4)
mod <- lmer(ivar2 ~ gvar1 * ivar1 + (1 | group_id), data = data)
summary(mod)
interact_plot(mod, ivar1, gvar1)
sim_slopes(mod, ivar1, gvar1)
modw <- lmer(ivar2 ~ gvar1 * ivar1 + (1 | group_id), data = data, weights = sampling_weight)
summary(modw)
interact_plot(modw, ivar1, gvar1)
sim_slopes(modw, ivar1, gvar1) In the case of your output, I suppose my issue is that it's not clear that it is wrong about the slopes, means, SDs because the output doesn't tell me that. I expect it to be different depending on the inclusions of weights because then we are calculating weighted means and SDs. Of course, without the data I can't be sure whether those weighted means and SDs are in fact the correct weighted means and SDs, I can only check it on my contrived example where I can verify it works right. I'll look into the issue about fixing the moderator values separately and report back. |
Looking into the fixed values/labels thing, I'm not replicating an issue there either --- code below which, when run, seems to show the labels are applying correctly. In the output you shared and the one plot for the weighted model, the library(dplyr)
# Set the seed for reproducibility
set.seed(123)
# Define the number of groups and the number of individuals per group
n_groups <- 10
n_individuals_per_group <- 20
# Create a multilevel dataset
data <- expand.grid(group_id = 1:n_groups, individual_id = 1:n_individuals_per_group)
# Generate some group-level and individual-level variables
data$gvar1 <- with(data, ave(group_id, group_id, FUN = function(x) rnorm(1, mean = 100, sd = 15)))
data$gvar2 <- with(data, ave(group_id, group_id, FUN = function(x) rnorm(1, mean = 100, sd = 15)))
data$ivar1 <- rnorm(nrow(data), mean = 100, sd = 15)
data$ivar2 <-(data$ivar1 + data$gvar1 + (data$ivar1 * data$gvar1)/100) + rnorm(nrow(data), 0, 15)
# Generate sampling weights (these can be random but should vary)
data$sampling_weight <- runif(nrow(data), min = 0.5, max = 2)
library(lme4)
mod <- lmer(ivar2 ~ gvar1 * ivar1 + (1 | group_id), data = data)
summary(mod)
interact_plot(mod, ivar1, gvar1)
interact_plot(mod, ivar1, gvar1, modx.values = c(85, 100, 115), modx.labels = c("-1", "0", "+1"))
sim_slopes(mod, ivar1, gvar1)
modw <- lmer(ivar2 ~ gvar1 * ivar1 + (1 | group_id), data = data, weights = sampling_weight)
summary(modw)
interact_plot(modw, ivar1, gvar1)
# roughly equivalent output
interact_plot(modw, ivar1, gvar1, modx.values = c(85, 100, 115), modx.labels = c("-1", "0", "+1"))
sim_slopes(modw, ivar1, gvar1) |
Greetings, when running the program with lmer function (and formula for multilevel modeling), the output changes based on the inclusion (or exclusion) of "weights").
For example, when running the following, the mean, +1SD, -1SD will be incorrectly calculated:
Example 1:
If I remove "weights" from the code, the mean, +1SD, -1SD will be CORRECTLY calculated and the plot appears as expected. this doesn't represent the complete formula for my model. Additionally, weighting seems most appropriate.
Example 2:
Finally, if I leave the full formula and attempt to fix the mod.x values, the lines appear flipped -- that is, where -1 SD should be, +1 SD is, and vice versa, on the interaction plot. This is in contrast to both Example 2 (above) and ggplots (accounting for weights).
Example 3:
Any insight on what might be driving this discrepancy when using "weights" in the formula? Thank you for your time!
The text was updated successfully, but these errors were encountered: