Page 1 of 1

Error when attempting predLines

Posted: Fri Jul 21, 2017 1:28 pm
by GerineLodder
Disclaimer: I am not very experienced in either R or multilevel analyses.
I am currently wrestling my way through the R2MLwin manual, examples online etc to familiarize myself with the package.
I try to apply the info in the manual to my own data.
So far, I have been able to google myself out of any other issues, but I am stuck with this one.
I hope you can provide some insights.

I am trying out the following formula:

Code: Select all

Form4 <-vicAlloC3 ~ 1 + kiva + (1 + kiva |mlwinschool) + (1|mlwinclass) + (1|mlwinID)

startingvaluesM4 <- list(FP.b = model4IGLS@FP, 
                         FP.v=model4IGLS@FP.cov,
                         RP.b=list(0.024,0.001,0.001,0.08,1.78),
                         RP.v = model4IGLS@RP.cov)

model4b <- runMLwiN(Formula = Form4, data = evaldata,
                    estoptions = list(EstM = 1, 
                                      startval=startingvaluesM4,
                                      resi.store.levs =  3)
                         )                         
                         
predLines(model4b, xname = "kiva", lev = 3,
          probs = c(0.025, 0.975), legend = FALSE)
I get the following error message:
Error in valid.viewport(x, y, width, height, just, gp, clip, xscale, yscale, :
invalid 'xscale' in viewport
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In order(as.numeric(x)) : NAs introduced by coercion
4: In split(as.numeric(x)[ord], gg[ord]) : NAs introduced by coercion
5: In min(x) : no non-missing arguments to min; returning Inf
6: In max(x) : no non-missing arguments to max; returning -Inf
7: In min(x) : no non-missing arguments to min; returning Inf
8: In max(x) : no non-missing arguments to max; returning -Inf
I have already tried the following things (I realize that these probably don't make sense, but as I am a novice in R, these are the trial and error things that made sense to me):

1. Changing the predictor does not help (I thought the problem might be that kiva is a factor)
2. Only running the model with 2 levels instead of 3 does not help
3. If I use the example from the manual, I do get the right figure (thus: it is not something with my R version or R2MLwin version or whatever)
4. Doing this stuff at level 2 instead of level 3 does not help.
5. Googling the error does not help (it seems to be a very general error)

Please note that I use the starting values because IGLS gives me a starting value of 0 for one of the variances and a negative one for another one.
It might have something to do with that?

I would appreciate your input very much!

Re: Error when attempting predLines

Posted: Mon Jul 24, 2017 9:26 am
by ChrisCharlton
I would suggest initially checking the stored residual chains (model4b@resi.chains$resi_lev3) to see whether there is anything unusual there (for example missing data). You can also obtain the mean and SD for each residual parameter by running:

Code: Select all

summary(model4b@resi.chains$resi_lev3)

Re: Error when attempting predLines

Posted: Mon Jul 24, 2017 1:03 pm
by GerineLodder
Thank you for your reply.
In this first inspection, I don't see odd patterns such as missing data. Do you have any other suggestions?

Re: Error when attempting predLines

Posted: Mon Jul 24, 2017 1:43 pm
by ChrisCharlton
If you can replicate the problem with a publicly available data set then I should be able to determine where the error is coming from. Assuming that this isn't possible the following code should match what it is doing to calculate the data for the graph:

Code: Select all

resi.chains <- model4b@resi.chains$resi_lev3
chains <- model4b@chains
categrv <- model4b@data$mlwinschool
selected <- unique(categrv)
    
RP.pos <- grep("RP3_var_", colnames(chains))
rpx.names <- sub("RP3_var_", "", colnames(chains)[RP.pos])

FP.pos <- grep("FP_", colnames(chains))
fp.names <- sub("FP_", "", colnames(chains)[FP.pos])

tval <- matrix(0, nrow(model4b@data), nrow(chains))
    
for (j in selected) {
  for (i in 1:length(fp.names)) {
    fpxdata <- model4b@data[categrv == j, fp.names[i]]
    if (is.factor(fpxdata)) {
      fpxdata <- as.integer(fpxdata) - 1
    }
    tval[categrv == j, ] <- tval[categrv == j, ] + fpxdata %o% chains[, FP.pos[i]]
  }
  for (i in 1:length(rpx.names)) {
    rpxdata <- model4b@data[categrv == j, rpx.names[i]]
    if (is.factor(rpxdata)) {
      rpxdata <- as.integer(rpxdata) - 1
    }
    tval[categrv == j, ] <- tval[categrv == j, ] + rpxdata %o% resi.chains[, paste0("u_", i-1, "_", j)]
  }
}
    
quants <- apply(tval, 1, function(x) quantile(x, c(probs[1], 0.5, probs[2])))
tval.med <- quants[2,]
tval.low <- quants[1,]
tval.up <- quants[3,]

pred.min <- min(tval.low)
pred.max <- max(tval.up)
pred.diff <- pred.max - pred.min
x <- model4b@data$kiva
x.min <- min(x)
x.max <- max(x)
If you run this and inspect the calculated variables then it may be possible to see where it is going wrong.

Re: Error when attempting predLines

Posted: Thu Jul 27, 2017 1:00 pm
by GerineLodder
I have to be honest: I cannot read all of this.

I get an error message again:

Code: Select all

> quants <- apply(tval, 1, function(x) quantile(x, c(probs[1], 0.5, probs[2])))
Error in quantile.default(x, c(probs[1], 0.5, probs[2])) : 
  object 'probs' not found
Called from: quantile.default(x, c(probs[1], 0.5, probs[2]))

Re: Error when attempting predLines

Posted: Thu Jul 27, 2017 2:17 pm
by ChrisCharlton
Sorry - I missed a parameter from the function definition:

Code: Select all

probs = c(0.025, 0.975)
If you add this somewhere before the problematic line then it should work.

Alternatively you could change the line:

Code: Select all

quants <- apply(tval, 1, function(x) quantile(x, c(probs[1], 0.5, probs[2])))
to:

Code: Select all

quants <- apply(tval, 1, function(x) quantile(x, c(0.025, 0.5, 0.975)))
The code is basically creating the data for the plot, that is then passed onto the xyplot function in lattice package, which I think is where your error is coming from.

Re: Error when attempting predLines

Posted: Fri Aug 04, 2017 9:53 am
by GerineLodder
Hi Chris,

I was able to run this syntax now. Can you indicate what you want me to check exactly? As indicated, it is not quite clear to me what I should be looking for here...

Re: Error when attempting predLines

Posted: Fri Aug 04, 2017 10:32 am
by ChrisCharlton
You need to look at the values that were just calculated and see whether they contain any usual values, i.e. NA, etc. The function is just using these to call the xyplot function from the lattice package as follows:

Code: Select all

xyplot(tval.med ~ x, prepanel = function(x, y, ...) {
      list(xlim = c(x.min, x.max), ylim = c(pred.min, pred.max))
    }, groups = categrv, panel = function(x, y, groups, ...) {
      col <- Rows(trellis.par.get("superpose.line"), 1:length(selected))$col
      j <- 1
      for (i in selected) {
        xg <- x[which(groups == i)]
        ypred <- y[which(groups == i)]
        ypred.low <- tval.low[which(groups == i)]
        ypred.up <- tval.up[which(groups == i)]
        panel.xyplot(x = sort(xg), y = ypred[order(xg)], col = col[j], type = "l", ...)
        panel.xyplot(x = sort(xg), y = ypred.low[order(xg)], col = col[j], type = "l", lty = 3, ...)
        panel.xyplot(x = sort(xg), y = ypred.up[order(xg)], col = col[j], type = "l", lty = 3, ...)
        j <- j + 1
      }
    }, ylab = "ypred", xlab = "kiva", ...)

Re: Error when attempting predLines

Posted: Fri Aug 04, 2017 12:12 pm
by GerineLodder
There are no missing values in quants, resi.chains, chains or X.
predmin = 1.71; predamx = 2.83

If I run the syntax you indicated here, I get the following error message:

Re: Error when attempting predLines

Posted: Fri Aug 04, 2017 12:46 pm
by ChrisCharlton
Your original error message referred to x, so could you also check x.min and x.max? You could also try manually plotting tval.med by x.