Skip to content
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

residuals not exported in variancePartition #18

Closed
kelshmo opened this issue Jul 28, 2020 · 3 comments
Closed

residuals not exported in variancePartition #18

kelshmo opened this issue Jul 28, 2020 · 3 comments

Comments

@kelshmo
Copy link

kelshmo commented Jul 28, 2020

metadata <- data.frame(
  batch = c("1", "2", "1", "2"),
  diagnosis = c("dx", "dx", "ct", "ct"),
  stringsAsFactors = FALSE,
  row.names = c("S1", "S2", "S3", "S4")
)

counts <- matrix(
  sample(0:100, size = 12),
  ncol = 4,
  dimnames = list(c("ENSG1", "ENSG2", "ENSG5"), c("S1", "S2", "S3", "S4"))
)

DGE <- edgeR::DGEList(counts)
gene_expression <- edgeR::calcNormFactors(DGE)

output <- variancePartition::voomWithDreamWeights(
  counts = gene_expression,
  formula =  ~ diagnosis + (1|batch),
  data = metadata,
  quiet = TRUE
)
#> Memory usage to store result: >2.7 Kb
#> Dividing work into 3 chunks...
#> 
#> Total:1 s

compute_residuals <- variancePartition::dream(exprObj = output, 
                                              formula = ~ diagnosis + (1|batch),
                                              data = metadata, 
                                              computeResiduals = TRUE)
#> Dividing work into 3 chunks...
#> 
#> Total:1 s
extract_residuals <- variancePartition::residuals(compute_residuals, gene_expression)
#> Error: 'residuals' is not an exported object from 'namespace:variancePartition'

Created on 2020-07-28 by the reprex package (v0.3.0)

When variancePartition is not specified:

metadata <- data.frame(
  batch = c("1", "2", "1", "2"),
  diagnosis = c("dx", "dx", "ct", "ct"),
  stringsAsFactors = FALSE,
  row.names = c("S1", "S2", "S3", "S4")
)

counts <- matrix(
  sample(0:100, size = 12),
  ncol = 4,
  dimnames = list(c("ENSG1", "ENSG2", "ENSG5"), c("S1", "S2", "S3", "S4"))
)

DGE <- edgeR::DGEList(counts)
gene_expression <- edgeR::calcNormFactors(DGE)

output <- variancePartition::voomWithDreamWeights(
  counts = gene_expression,
  formula =  ~ diagnosis + (1|batch),
  data = metadata,
  quiet = TRUE
)
#> Memory usage to store result: >2.7 Kb
#> Dividing work into 3 chunks...
#> 
#> Total:1 s

compute_residuals <- variancePartition::dream(exprObj = output, 
                                              formula = ~ diagnosis + (1|batch),
                                              data = metadata, 
                                              computeResiduals = TRUE)
#> Dividing work into 3 chunks...
#> 
#> Total:1 s
extract_residuals <- residuals(compute_residuals, gene_expression)
#> Error in fitted.MArrayLM(object): Object contains contrasts rather than coefficients, so fitted values cannot be computed.

Created on 2020-07-28 by the reprex package (v0.3.0)

Interestingly, there is the same error message when variancePartition is loaded here in the script:

metadata <- data.frame(
  batch = c("1", "2", "1", "2"),
  diagnosis = c("dx", "dx", "ct", "ct"),
  stringsAsFactors = FALSE,
  row.names = c("S1", "S2", "S3", "S4")
)

counts <- matrix(
  sample(0:100, size = 12),
  ncol = 4,
  dimnames = list(c("ENSG1", "ENSG2", "ENSG5"), c("S1", "S2", "S3", "S4"))
)

DGE <- edgeR::DGEList(counts)
gene_expression <- edgeR::calcNormFactors(DGE)

output <- variancePartition::voomWithDreamWeights(
  counts = gene_expression,
  formula =  ~ diagnosis + (1|batch),
  data = metadata,
  quiet = TRUE
)
#> Memory usage to store result: >2.7 Kb
#> Dividing work into 3 chunks...
#> 
#> Total:1 s

compute_residuals <- variancePartition::dream(exprObj = output,
                                              formula = ~ diagnosis + (1|batch),
                                              data = metadata,
                                              computeResiduals = TRUE)
#> Dividing work into 3 chunks...
#> 
#> Total:1 s
library(variancePartition)
#> Loading required package: ggplot2
#> Loading required package: limma
#> Loading required package: foreach
#> Loading required package: scales
#> Loading required package: Biobase
#> Loading required package: BiocGenerics
#> Loading required package: parallel
#> 
#> Attaching package: 'BiocGenerics'
#> The following objects are masked from 'package:parallel':
#> 
#>     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
#>     clusterExport, clusterMap, parApply, parCapply, parLapply,
#>     parLapplyLB, parRapply, parSapply, parSapplyLB
#> The following object is masked from 'package:limma':
#> 
#>     plotMA
#> The following objects are masked from 'package:stats':
#> 
#>     IQR, mad, sd, var, xtabs
#> The following objects are masked from 'package:base':
#> 
#>     anyDuplicated, append, as.data.frame, basename, cbind, colnames,
#>     dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep,
#>     grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
#>     order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
#>     rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
#>     union, unique, unsplit, which, which.max, which.min
#> Welcome to Bioconductor
#> 
#>     Vignettes contain introductory material; view with
#>     'browseVignettes()'. To cite Bioconductor, see
#>     'citation("Biobase")', and for packages 'citation("pkgname")'.
#> 
#> Attaching package: 'variancePartition'
#> The following object is masked from 'package:limma':
#> 
#>     classifyTestsF

extract_residuals <- residuals(compute_residuals, output$E)
#> Error in fitted.MArrayLM(object): Object contains contrasts rather than coefficients, so fitted values cannot be computed.

Created on 2020-07-28 by the reprex package (v0.3.0)

@GabrielHoffman
Copy link
Owner

GabrielHoffman commented Jul 28, 2020

I am working on this. In the meantime you can access the residuals with compute_residuals$residuals

@GabrielHoffman
Copy link
Owner

This issue is fixed in v1.19.13.

Now residuals(compute_residuals, gene_expression) works, but note that variancePartition::residuals(compute_residuals, gene_expression) does not.

I think this is an issue with R rather than this package. variancePartition uses 3 types of functions:

  1. Most functions in variancePartition are custom, stand-alone functions like dream and fitExtractVarPartModel.

  2. Other functions are originally defined in limma and are redefined here for the results of dream, such as topTable.

  3. Finally, residuals is defined by limma by the standard R package stats. So I a generic version of residuals that determines the code to run based on the object passed to it. The problem is that stats::residuals is a generic S4 function, while limma::residuals.MArrayLM is an S3 function. This led to the wrong version of residuals being called and led to the error above. This part is now resolved.

However, due to this issue, variancePartition does not export residuals in a way that is accessible by :: or ::: operators, since that would override the builtin S3/S4 versions of residuals. So unfortunately, variancePartition::residuals is not accessible

@kelshmo
Copy link
Author

kelshmo commented Jul 29, 2020

Fixed in 1.19.13. Thanks.

@kelshmo kelshmo closed this as completed Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants