0

I am trying to run a function which calculates the marginal effects for different mixed effects models, based on two different main predictors (var1 vs. var2). The original code can be found here: https://stats.idre.ucla.edu/r/dae/mixed-effects-logistic-regression/. Below is a reproducible example:

I create a dataframe (ex):

time <- seq(from = 1, to = 500, by =1)
var1 <- factor(sample(0:1, 500, replace = TRUE))
var2 <- factor(sample(0:1, 500, replace = TRUE))
var3 <- sample(1:500, 500, replace = TRUE)
group <- rep(1001:1005, 500)
out <- sample(0:1, 500, replace = TRUE)
group <- as.factor(group)

ex <- data.frame(time,var1,var2,var3,group,out)

Run the models:

m1a <- glmer(out ~ time + var1 +  (1|group), data=ex, family  = binomial(link = "logit"), nAGQ = 1,
            control = glmerControl(calc.derivs = FALSE))

m1b <- glmer(out ~ time + var2 + (1|group), data=ex, family = binomial(link = "logit"), nAGQ = 1, 
             control = glmerControl(calc.derivs = FALSE))

Create subsets of the data with only the predictors for complete cases:

sub1a <- na.omit(ex[, c("time", "var1", "group")])
sub1b <- na.omit(ex[, c("time", "var2", "group")])

I cannot attach my data frame, ex, because R says var1 and var2 are masked. Therefore, the only way I know to refer to the variables is using $. However, every function I create produces a wrong or null result. I first tried:

marg <- function(v1, v2, d, m) {

  biprobs <- lapply(levels(v1), function(var) {
    v2[ ] <- var
    lapply(time, function(ti) {
      d$time <- ti
      predict(m, newdata = d, type = "response")
    })

  })

  plotdat <- lapply(biprobs, function(X) {
    temp <- t(sapply(X, function(x) {
    c(M=mean(x), quantile(x, c(.25, .75)))
    }))
    temp <- as.data.frame(cbind(temp,time))
    colnames(temp) <- c("PP", "Lower", "Upper", "Dayssince")
    return(temp) 
  })

  plotdat <- do.call(rbind, plotdat)

}

result1 <- marg(ex$var1, sub1a$var1, sub1a, m1a)

Although this creates a data frame, it produces the same predicted probabilities for each level of var1 (0 vs. 1) at a given time (1-500), which is not what I want. So then I tried:

    marg <- function(v, d, m) {

  biprobs <- lapply(levels(ex$v), function(var) {
    d$v[ ] <- var
    lapply(time, function(ti) {
    d$time <- ti
      predict(m, newdata = d, type = "response")
    })

  })

  .....
}

result2 <- marg(var1,sub1a, m1a)

This produces a null result. I also tried, which produces a null result:

    marg <- function(d1,v,d2,m) {

  biprobs <- lapply(levels(d1$v), function(var) {
    d2$v[ ] <- var
    lapply(time, function(ti) {
      d2$time <- ti
      predict(m, newdata = d2, type = "response")
    })

  })

 ......

}

result3 <- marg(ex,var1,sub1a,m1a)

I also tried creating a new object to input directly into the function:

v1 <- ex$var1

marg <- function(d, m) {

biprobs <- lapply(levels(v1), function(var) {
.....
})

  })

That also produces a null result. How do I refer to different variables in an unattached data frame?? The code works with direct inputs, so it's a matter of correctly defining the function arguments. I appreciate any help!

llsabang
  • 143
  • 1
  • 10
  • Is this what you are looking for? https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value – MrFlick Mar 24 '20 at 17:03
  • Thanks for the info. I tried using [ ] or [[ ]] instead of $, but it didn't work. – llsabang Mar 24 '20 at 17:44
  • What do you mean by "produces a null result"? I get as result of `result2` a data frame with dim == 100,4. – jay.sf Mar 26 '20 at 08:29
  • If I run the entire function with the plotdat, the result in my Global Environment is result2 | NULL (empty). However, I'm pretty sure this is because the result for the first part, biprobs, is 0. Would you mind posting what you did? Is it exactly the same? – llsabang Mar 27 '20 at 16:58

0 Answers0