cwatson / brainGraph

Graph theory analysis of brain MRI data
166 stars 48 forks source link

Error get.resid() function: x is not data.table #19

Closed tfblanken closed 4 years ago

tfblanken commented 4 years ago

Hi Chris,

Im following your user's guide to construct a structural covariance network. Very helpful, thanks. Unfortunately I get stuck at the 'model residuals' part, specifically:

all.dat.resids <- get.resid(lhrh, covars=covars, exclude.cov='Group')

gives an error: Error in setkeyv(x, cols, verbose = verbose, physical = physical) : x is not a data.table

I've looked into the function, running it step by step. And it seems that the issue arises when using the melt() function: DT.m <- melt(DT.cov, id.vars = names(covars), variable.name = "region")

which gives a warning message: Warning message: In melt(DT.cov, id.vars = names(covars), variable.name = "region") : The melt generic in data.table has been passed a data.frame and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(DT.cov). In the next version, this warning will become an error.

And I think this leads to the problem that DT.m is not a data.table. Hence, when submitting it to setkey(DT.m, region, Study.ID), an error message is given: Error in setkeyv(x, cols, verbose = verbose, physical = physical) : x is not a data.table

I've looked this warning up online, and it seems indeed some things have changed in using the melt() function. I guess this needs to be adjusted in your get.resid() function as well.

Best, Tessa

tfblanken commented 4 years ago

I got it to work temporarily by replacing DT.m <- melt(DT.cov, id.vars = names(covars), variable.name = "region") for:

DT.m <- reshape2::melt(DT.cov, id.vars = names(covars), variable.name = "region") DT.m <- as.data.table(DT.m)

But now get a new error: Error in get_lm_vars(covars, exclude.cov, ...) : could not find function "get_lm_vars"

I cannot find the function get_lm_vars myself either. I'm not sure whether I'm doing anything wrong?

cwatson commented 4 years ago

Hi Tessa, It seems my previous reply didn't go through... I have a few suggestions:

  1. Make sure that covars is a data.table; I suspect yours is a data.frame, which is why you see the setkey error in your first post. The warning will probably still show up but that is OK; it will be taken care of for the next release.
  2. If you really wanted to edit the code, you could download the get_resids.R file from the repo and source it in your scripts. This is less desirable, though.

Try that and let me know if it worked. Chris

tfblanken commented 4 years ago

Hi Chris,

  1. Thank you so much, you are right! I repeatedly checked the structure of all other elements, but forgot to check covars. Unfortunately it still doesn't work, but it has something to do with the variable types of my Study.ID (integer) and Group (factor). I was wondering, is there some reproducable example and example data that I can go through and see the exact dimensions/ types/ etc. as a reference?

  2. I did not want to edit the code! I just wanted to check where in the code the error occurred.

Thanks a lot for your help.

cwatson commented 4 years ago

I don't have a reproducible example or data; the better solution would be better checking within the code (that is, my code and not the users'); I will include a check for a data.table in the next version.

I definitely recommend against Study.ID being an integer. At the least, it should be a fixed-width character string of integers. Here is how you can convert that:

n <- covars[, max(Study.ID)]
covars[, Study.ID := formatC(Study.ID, width=floor(log10(n) + 1), flag='0')]

Note that the value for the flag argument is the number 0 surrounding by (single or double) quotation marks. This issue has popped up w/ other users and when v3.0.0 finally gets released it will be handled by the package (i.e., it will detect when the column is integer/numeric and convert it to zero-padded character; such as "001", "002", ..., "100").

RE the Group variable: it should be OK if it is a factor; did you see a related error/warning there?

tfblanken commented 4 years ago

Thanks, Chris.

I have worked through your User Guide's example, line by line. In some cases it would be nice however to see the structure of an object, in order to see where the problem potentially occurs. In any case, thanks a lot for all your help.

I have now made sure that Study.ID is a fixed-width character string of integers. I do however still get warnings. I'll again will check within your code to hopefully locate where it goes wrong. If I can identify the mistake and hopefully solve it, I'll let you know!

cwatson commented 4 years ago
  1. Which objects are you talking about, specifically? There is an example for covars in chapter 5.1, and I have tried to describe the outputs of many functions. While Study.ID is not a fixed-width character vector there, it should be; that is only because I hadn't noticed and didn't check closely before but will do so for the next update.
  2. The warnings can almost always be safely ignored. It really depends on the specific warning.

Since it seems to be working now, I will close the issue. Please re-open it if you still have issues.