user6591823
user6591823

Reputation: 23

error message in R : if (nomZ %in% coded) { : argument is of length zero

I'm very new to R (and stackoverflow). I've been trying to conduct a simple slopes analysis for my continuous x dichotomous regression model using lmres, and simpleSlope from the pequod package.

My variables:

SLS - continuous DV csibdiff - continuous predictor (I already manually centered the variable with another code) culture - dichotomous moderator

newmod<-lmres(SLS ~ csibdiff*culture, data=sibdat2)
newmodss <-simpleSlope(newmod, pred="csibdiff", mod1="culture")

However, after running the simpleSlope function, I get this error message:

Error in if (nomZ %in% coded) { : argument is of length zero

I don't understand the nomZ part but I assume something was wrong with my variables. What does this mean? I don't have a nomZ named thing in my data at all. None of my variables are null class (I checked them with the is.null() function), and I didn't seem to have accidentally deleted the contents of the variable (I checked with the table() function).

If anyone else can suggest another function/package that I can do a simple slope analysis in, as well, I'd appreciate it. I've been stuck on this problem for a few days now.

EDIT: I subsetted the relevant variables into a csv file.

https://www.dropbox.com/s/6j82ky457ctepkz/sibdat2.csv?dl=0

Upvotes: 2

Views: 971

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226162

tl;dr it looks like the authors of the package were thinking primarily about continuous moderators; if you specify mod1="cultureEuropean" (i.e. to match the name of the corresponding parameter in the output) the function returns an answer (I have no idea if it's sensible or not ...)

It would be a service to the community to let the maintainers of the pequod package (maintainer("pequod")) know about this issue ...

Read data and replicate error:

sibdat2 <- read.csv("sibdat2.csv")
library(pequod)
newmod <- lmres(SLS ~ csibdiff*culture, data=sibdat2)
newmodss <- simpleSlope(newmod, pred="csibdiff", mod1="culture")

Check the data:

summary(sibdat2)

We do have some NA values in csibdiff, so try removing these ...

sibdat2B <- na.omit(sibdat2)

But that doesn't actually help (same error as before).

Plot the data to check for other strangeness

library(ggplot2); theme_set(theme_bw())
ggplot(sibdat2B,aes(csibdiff,SLS,colour=culture))+
  stat_sum(aes(size=factor(..n..))) +
  geom_smooth(method="lm")

enter image description here

There's not much going on here, but nothing obviously wrong either ...

Use traceback() to see approximately where the problem is:

traceback()
3: simple.slope(object, pred, mod1, mod2, coded)
2: simpleSlope.default(newmod, pred = "csibdiff", mod1 = "culture")
1: simpleSlope(newmod, pred = "csibdiff", mod1 = "culture")

We could use options(error=recover) to jump right to the scene of the crime, but let's try step-by-step debugging instead ...

debug(pequod:::simple.slope)

As we go through we can see this:

nomZ <- names(regr$coef)[pos_mod]
nomZ ## character(0)

And looking a bit farther back we can see that pos_mod is also a zero-length integer. Farther back, we see that the code is looking through the parameter names (row names of the variance-covariance matrix) for the name of the modifier ... but it's not there.

debug: pos_pred_mod1 <- fI + grep(paste0("\\b", mod1, "\\b"), jj[(fI + 
    1):(fI + fII)])
Browse[2]> pos_mod
## integer(0)
Browse[2]> jj[1:fI]
## [[1]]
## [1] "(Intercept)"
## 
## [[2]]
## [1] "csibdiff"
## 
## [[3]]
## [1] "cultureEuropean"
Browse[2]> mod1
## [1] "culture"

The solution is to tell simpleSlope to look for a variable that is there ...

(newmodss <- simpleSlope(newmod, pred="csibdiff", mod1="cultureEuropean"))
## Simple Slope:
##                              simple slope standard error   t-value   p.value
## Low cultureEuropean (-1 SD)    -0.2720128      0.2264635 -1.201133 0.2336911
## High cultureEuropean (+1 SD)    0.2149291      0.1668690  1.288011 0.2019241

We do get some warnings about NaNs produced -- you'll have to dig farther yourself to see if you need to worry about them.

Upvotes: 1

Related Questions