Giter Club home page Giter Club logo

Comments (38)

rdboyes avatar rdboyes commented on June 29, 2024 4

Had this same problem, but it resolved after forcing a reinstall with:

devtools::install_github("rmcelreath/rethinking", force = TRUE)

from rethinking.

helophilus avatar helophilus commented on June 29, 2024 3
> plot(precis(m5.3))
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

> precis.plot(precis(m5.3))
Error in precis.plot(precis(m5.3)) : 
  could not find function "precis.plot"

> precis_plot(precis(m5.3))
> # BINGO!!

from rethinking.

rmcelreath avatar rmcelreath commented on June 29, 2024 2

Still cannot replicate it. But one user was able to fix it by reinstalling the package.

He suspects the issue is triggered when one of the dependencies is updated. But I've tried that, with no luck. So still hunting down the cause.

from rethinking.

torkar avatar torkar commented on June 29, 2024 2

Had the same problem, but I just closed RStudio and cleaned all variables (hidden too) and started fresh with library(rethinking). Then it worked :)

from rethinking.

sebastiansauer avatar sebastiansauer commented on June 29, 2024 2

FWIW, what helped for me is using the rstan::traceplot() function directly. I used this helper function:

my_traceplot <- function(model) {
  rstan::traceplot( model@stanfit, pars=names(model@start[[1]]))
}
my_traceplot(my_model)

from rethinking.

fsdias avatar fsdias commented on June 29, 2024 2
> plot(precis(m5.3))
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

> precis.plot(precis(m5.3))
Error in precis.plot(precis(m5.3)) : 
  could not find function "precis.plot"

> precis_plot(precis(m5.3))
> # BINGO!!

This works on R 3.4.4 (Ubuntu 18.04) run from RStudio.

from rethinking.

johnkapson avatar johnkapson commented on June 29, 2024 1

FYI - I've seen the same error when trying to plot the results of the compare(...) function, e.g.

> islands.compare <- compare(m10.10, m10.11, m10.12, m10.13, m10.14, n=1e4)
> plot(islands.compare)
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

from rethinking.

aenfield avatar aenfield commented on June 29, 2024 1

Thanks @Monkel. I've tried that too - starting from scratch w/ an empty workspace - and the problem still occurs. FWIW. All good from my end.

from rethinking.

Roger-Cox avatar Roger-Cox commented on June 29, 2024 1

I found that in RStudio, unchecking both the Rethinking and the Rstan packages and then rechecking those boxes got it working:

precis( m5.3 )
Mean StdDev 5.5% 94.5%
a 9.69 0.20 9.36 10.01
bR -0.13 0.28 -0.58 0.31
bA -1.13 0.28 -1.58 -0.69
sigma 1.44 0.14 1.21 1.67
plot( precis(m5.3) )
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'
precis.plot()
Error in precis.plot() : could not find function "precis.plot"
precis_plot()
Error in precis_plot() : argument "x" is missing, with no default
precis_plot(m5.3)
Error in precis_plot(m5.3) :
no slot of name "output" for this object of class "map"
detach("package:rethinking", unload=TRUE)
library("rethinking", lib.loc="C:/R-3.5.1/library")
rethinking (Version 1.59)
plot( precis(m5.3) )
Error in UseMethod("vcov") :
no applicable method for 'vcov' applied to an object of class "map"
detach("package:rstan", unload=TRUE)
Error: package ‘rstan’ is required by ‘rethinking’ so will not be detached
library("rstan", lib.loc="C:/R-3.5.1/library")
detach("package:rethinking", unload=TRUE)
detach("package:rstan", unload=TRUE)
library("rstan", lib.loc="C:/R-3.5.1/library")
rstan (Version 2.17.3, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
library("rethinking", lib.loc="C:/R-3.5.1/library")
rethinking (Version 1.59)
plot( precis(m5.3) )

from rethinking.

steffengreup avatar steffengreup commented on June 29, 2024 1

So I just ran into this issue with the plot( coeftab ) example from chapter 5.1. Strange, because when I ran the same code yesterday, everything worked as expected. This also happens on a fresh R session with rethinking as the only library call.

Apparently something goes wrong with setMethod.

> getMethod("plot", "coeftab")
Error in getMethod("plot", "coeftab") : 
  no method found for function 'plot' and signature coeftab

> plot(coeftab(modelD_A, modelD_M, modelD_AM), par = c("b_a", "b_m"))
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

I can fix it by manually setting the method:

> setMethod( "plot" , "coeftab" , function(x,y,...) coeftab_plot(x,y,...) )
> getMethod("plot", "coeftab")
Method Definition:

function (x, y, ...) 
coeftab_plot(x, y, ...)

Signatures:
        x         y    
target  "coeftab" "ANY"
defined "coeftab" "ANY"

Now it will work:

plot(coeftab(modelD_A, modelD_M, modelD_AM), par = c("b_a", "b_m"))

Funny thing, when I tried to show the error using the reprex package there was no problem...

from rethinking.

manuelhaussmann avatar manuelhaussmann commented on June 29, 2024

I've had the same error and was also able to fix it by reinstalling the package.

Unfortunately I don't remember which dependencies I updated lately, so I can't be of any help in that regard.

from rethinking.

aenfield avatar aenfield commented on June 29, 2024

FWIW, I also have seen the same error with the results of compare, like johnkapson. Reinstalling didn't fix it, and I don't see that there's a compare.plot, so I don't think I can use something like the precis.plot workaround.

from rethinking.

manuelhaussmann avatar manuelhaussmann commented on June 29, 2024

Not sure if this helps in tracking down the issue, but it might help you as a temporary workaround @aenfield . As long as you start a R session with an empty workspace (or at least nothing rethinking related in it) and only then run your code from the chapters everything should work as desired.

If you want to resuse your earlier session, then start R with R --no-restore and load the workspace after the library:

library(rethinking)
load(".RData")

This should give you the desired result. (At least in my case it does)

from rethinking.

finiterank avatar finiterank commented on June 29, 2024

Any progress on this? Or at least a workaround for plotting compare outputs?

I am really enjoying the book, by the way. Thank you for writing it, @rmcelreath. Long time looking for something like this.

from rethinking.

paulc00 avatar paulc00 commented on June 29, 2024

For me,plot() methods for precis, compare, and map2stan work fine if I am running code within RStudio but they don't even show up as object methods within the R Kernel on a Jupyter notebook.

@rmcelreath, you may have more luck replicating this issue in Jupyter. Thanks for a brilliant course by the way.

from rethinking.

rmcelreath avatar rmcelreath commented on June 29, 2024

I still haven't been able to replicate it.

I do have a theory that involves the namespace. In my development version, I've made some namespace changes that might fix it, if the Universe is kind.

I'll see if I can find time to upload it to a development branch, so people can try it out. I'm unfortunately traveling a lot lately, so may not find time this week. But next week I'll get to it.

from rethinking.

paulc00 avatar paulc00 commented on June 29, 2024

Thanks Richard.

For anyone that's really desperate, here is a super ugly workaround: You can copy the setMethod("plot"... definition from the relevant class file into your notebook or source after you've loaded the rethinking library and before you call plot for the class.

from rethinking.

loudermilk avatar loudermilk commented on June 29, 2024

rmcelreath - here are some more clues:


> library(rethinking)
> plot(coeftab(m6.11, m6.12, m6.13, m6.14))
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'
> precis.plot(coeftab(m6.11, m6.12, m6.13, m6.14))
Error: could not find function "precis.plot"

after devtools::install_github("rmcelreath/rethinking", force = TRUE) all plots look beautiful!

also, interesting is calling methods(plot) -- the rethinking plot functions are missing the dot notation (I've never noticed this before, so I don't know if it is important or not):

> methods(plot)
 [1] plot.acf*                       plot,ANY,ANY-method             plot,coeftab,ANY-method        
 [4] plot,color,ANY-method           plot,compareIC,ANY-method       plot.correspondence*           
 [7] plot.data.frame*                plot.decomposed.ts*             plot.default                   
[10] plot.dendrogram*                plot.density*                   plot.ecdf                      
[13] plot.factor*                    plot.formula*                   plot.function                  
[16] plot.ggplot*                    plot,git_repository,ANY-method  plot.gtable*                   
[19] plot.hclust*                    plot.histogram*                 plot.HoltWinters*              
[22] plot.isoreg*                    plot.lda*                       plot.lm*                       
[25] plot.loo*                       plot,map2stan,ANY-method        plot.mca*                      
[28] plot.mcmc*                      plot.mcmc.list*                 plot.medpolish*                
[31] plot.mlm*                       plot.ppr*                       plot.prcomp*                   
[34] plot,precis,ANY-method          plot.princomp*                  plot.profile*                  
[37] plot,profile.mle,missing-method plot.profile.nls*               plot.raster*                   
[40] plot.ridgelm*                   plot.shingle*                   plot.spec*                     
[43] plot,stanfit,missing-method     plot.stepfun                    plot.stl*                      
[46] plot.table*                     plot.trellis*                   plot.ts                        
[49] plot.tskernel*                  plot.TukeyHSD*                 
see '?methods' for accessing help and source code

from rethinking.

MitchEppley avatar MitchEppley commented on June 29, 2024

Just a quick note: I had this problem today after the 'loo' package updated.
Reinstalling 'rethinking' cleared the problem.
Thanks for a great set of lecture videos. ME

from rethinking.

franzbischoff avatar franzbischoff commented on June 29, 2024

@rmcelreath , same problem here... I tried to reinstall all packages, and no luck:

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
> packageDescription("rethinking")
Package: rethinking
Type: Package
Title: Statistical Rethinking book package
Version: 1.59
Date: 2016-6-24
Author: Richard McElreath
Maintainer: Richard McElreath <[email protected]>
Imports: coda, MASS, mvtnorm, loo
Depends: rstan (>= 2.10.0), parallel, methods, stats, graphics
Description: Utilities for fitting and comparing models
License: GPL (>= 3)
Built: R 3.3.2; ; 2017-01-09 20:37:35 UTC; unix
RemoteType: github
RemoteHost: https://api.github.com
RemoteRepo: rethinking
RemoteUsername: rmcelreath
RemoteRef: master
RemoteSha: a309712d904d1db7af1e08a76c521ab994006fd5
GithubRepo: rethinking
GithubUsername: rmcelreath
GithubRef: master
GithubSHA1: a309712d904d1db7af1e08a76c521ab994006fd5

-- File: /usr/local/lib/R/site-library/rethinking/Meta/package.rds
> plot(precis(fit))
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

@MitchEppley, here is my 'loo' version:

> packageDescription("loo")
Package: loo
Type: Package
Title: Efficient Leave-One-Out Cross-Validation and WAIC for Bayesian Models
Version: 1.0.0
Date: 2016-12-15
Authors@R: c(person("Aki", "Vehtari", email = "[email protected]", role = c("aut")),
           person("Andrew", "Gelman", email = "[email protected]", role = c("aut")),
           person("Jonah", "Gabry", email = "[email protected]", role = c("cre", "aut")),
           person("Juho", "Piironen", role = c("ctb")), person("Ben", "Goodrich", role = c("ctb")))
Maintainer: Jonah Gabry <[email protected]>
URL: http://mc-stan.org/, https://groups.google.com/forum/#!forum/stan-users
BugReports: https://github.com/stan-dev/loo/issues
Description: Efficient approximate leave-one-out cross-validation (LOO) using Pareto smoothed
           importance sampling (PSIS), a new procedure for regularizing importance weights. As a
           byproduct of the calculations, we also obtain approximate standard errors for estimated
           predictive errors and for the comparison of predictive errors between models. We also
           compute the widely applicable information criterion (WAIC).
License: GPL (>= 3)
LazyData: TRUE
Depends: R (>= 3.1.2)
Imports: graphics, matrixStats (>= 0.50.0), parallel, stats
Suggests: knitr, rmarkdown, testthat
VignetteBuilder: knitr
RoxygenNote: 5.0.1
NeedsCompilation: no
Packaged: 2016-12-15 22:35:09 UTC; jgabry
Author: Aki Vehtari [aut], Andrew Gelman [aut], Jonah Gabry [cre, aut], Juho Piironen [ctb], Ben
           Goodrich [ctb]
Repository: CRAN
Date/Publication: 2016-12-16 08:33:56
Built: R 3.3.2; ; 2017-01-09 00:37:28 UTC; unix

-- File: /usr/local/lib/R/site-library/loo/Meta/package.rds 

from rethinking.

franzbischoff avatar franzbischoff commented on June 29, 2024

As @jabbermonkey said, the ugly way to correct plot for precis is:

setMethod( "plot" , "precis" , function(x,y,...) precis_plot(x,y,...) )

Here you can find the other setMethod's to copy into your code.

from rethinking.

chuymtz avatar chuymtz commented on June 29, 2024

I tried many suggestions and eventually found that loading rstan worked.
`

library(rethinking)
Loading required package: rstan
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.12.1, packaged: 2016-09-11 13:07:50 UTC, GitRev: 85f7a56811da)
For execution on a local, multicore CPU with excess RAM we recommend calling
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
Loading required package: parallel
rethinking (Version 1.59)
Warning messages:
1: package 'ggplot2' was built under R version 3.3.2
2: package 'StanHeaders' was built under R version 3.3.2
library(rstan)
`

Then I call the plot function on precis.

from rethinking.

peymanrah avatar peymanrah commented on June 29, 2024

I am receiving the same error when I am trying to plot ROC curve on H2O framework.

orig.pred = h2o.predict(dl_orig,test)
orig.perf = h2o.performance(dl_orig, test)

h2o.auc(orig.perf)
plot(orig.perf, type = "roc") # Plot ROC curve
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'

from rethinking.

 avatar commented on June 29, 2024

This problem has reappeared, possibly with the update of RStan for R version 3.4.3, rethinking version 1.59

> plot(precis(m5.3))
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'
> precis.plot(m5.3)
Error in precis.plot(m5.3) : could not find function "precis.plot"
> plot.precis(m5.3) 
Error in plot.precis(m5.3) : could not find function "plot.precis"

When I reinstalled "package rethinking" to try to correct the error, I got a warning that "rethinking" does not [yet] exist for R version 3.4.3.
However, I recognized the plot for m5.3 in "Rethinking Statistics" as identical to the default plot for a stan object in RStan. So I tried
rstan::plot(m5.3)
and that worked fine.
I suspect that plot(precis(5.3)) actually just calls the rstan;;plot function for a precis object, and that in the course of the update of R to 3.4.3, and subsequently of rstan for R 3.4.3, the link for the call got broken.
In the meantime, just calling stan::plot() on the precis object seems to work fine.
I haven't tried "compare" or "maptostan" yet. I haven't got to those points in the book yet.
Larry Hunsicker

from rethinking.

 avatar commented on June 29, 2024

rstan::plot() works fine to plot a compare object, too. Not too surprising, as it is obviously a version of the same plotting function in rstan used for plotting a precis object. Also works for plot{coeftab)).

from rethinking.

rmcelreath avatar rmcelreath commented on June 29, 2024

rstan::plot() is a standard generic function. It is calling the rethinking methods, not any rstan code.

Still never had this problem occur on my machines. If I could ever replicate it, I might be able to prevent it.

from rethinking.

samuelko avatar samuelko commented on June 29, 2024

A method to cause the problem on my computer
(1) Create a map-object.
(2) Save workspace, restart R-studio
(3) plot(precis(map_object))

Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'

To fix the problem:
(1) Clear all map-objects from the "global environment"
(2) Save workspace and restart R-Studio

Clue about what is happening:

  • When I load the rethinking packedge and have a map-object in the global environment, then the loading process is going very fast, it seems to not really load the stuff.

  • When I load the rethinking packedge and not have any map-objects, then the loading time is longer, it seems to actually load the stuff correctly.

from rethinking.

mzakariaCERN avatar mzakariaCERN commented on June 29, 2024

I don't think this is a package specific bug. I am getting the same issues with package(pROC)

probs <- predict(credit_boost10, credit_test, type = "prob")
pred <- ROCR::prediction(probs[, 2], credit_test$default)
perf_dt_10 <- ROCR::performance(pred, 'tpr', 'fpr')
plot(perf_dt_10)

The solution was to clear variables, re install package. It works on with the code. But Knit still crashes

from rethinking.

dxander avatar dxander commented on June 29, 2024

rethinking_rcode5point5error
rethinking_rcode5point5fixed

from rethinking.

corriebar avatar corriebar commented on June 29, 2024

I noticed that I always get the error when using eg.g plot(coeftab(mod1, mod2) inside my session but the plot worked just fine when I knittered the Rmarkdown. Reinstalling the package, restarting the session etc did not help for me (I had the impression that the problem was that Rstudio always automatically loaded my old workspace). What helped for me was detaching all packages:

lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE)

from rethinking.

pluviosilla avatar pluviosilla commented on June 29, 2024

I have to reinstall the rethinking library every time I start RStudio, and sometimes even within a single session to plot the output of precis().

QUES. I don't understand the recommendation that we access the plot method directly with precis.plot(). How do we specify the model when using this workaround?

from rethinking.

TrentBrick avatar TrentBrick commented on June 29, 2024

I have run into the same issue suddenly. No problems with the package before now. Suddenly at Chapter 11 with the Hurricane dataset, I can't plot the traces of MCMC or run postcheck(model).

from rethinking.

lpslot avatar lpslot commented on June 29, 2024

Had this same issue pop up recently. At first a re-install of rethinking seemed to fix it. Not today. I did a complete "Update" of all packages in RStudio, and that seemed to fix plot for both precise and compare.

from rethinking.

ZhichuanLi avatar ZhichuanLi commented on June 29, 2024

Had this same issue today when I try to execute R code 1.30 plot(compare(m1,m2,m3,m4,m5)) in RStudio. Reinstall package and other suggestions doesn't works for me.

from rethinking.

 avatar commented on June 29, 2024

IT WORKS THANK YOU I LOVE YOU !

from rethinking.

gsgxnet avatar gsgxnet commented on June 29, 2024

I am having the same issue here with R code 5.10 /5.11 plot( coeftab(m5.1,m5.2,m5.3), par=c("bA","bM") )
Environment is generally R 4.0.3, R-Studio 1.4 and newest tidyverse loaded beside rethinking.
I went into debugger by changing the code to:
coeftab_5123 <- coeftab(m5.1,m5.2,m5.3, se=TRUE, digits = 3)
browser(); plot( coeftab_5123 , par=c("bA","bM") )

using the additional parameters se=TRUE, digit=3 for trial and error, but they do neither help nor worsen.

point of failure is at function:
xy.coords(x, y, xlabel, ylabel, log) in namespace gr.devices

details when arriving there in the debugger:

str(y)
Formal class 'coeftab' [package "rethinking"] with 6 slots
  ..@ coefs : num [1:8, 1:3] 0 -0.568 0.788 NA 0.097 0.11 0.078 NA 0 NA ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:8] "a" "bA" "sigma" "bM" ...
  .. .. ..$ : chr [1:3] "m5.1" "m5.2" "m5.3"
  ..@ se    : num [1:8, 1:3] 0.0974 0.11 0.078 NA NA ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:8] "a" "bA" "sigma" "bM" ...
  .. .. ..$ : chr [1:3] "1" "NA" "3"
  ..@ nobs  : int [1:3] 50 50 50
  ..@ AIC   : num(0) 
  ..@ digits: num 3
  ..@ width : num 7

the xy.coords functions fails at its last statement:
list(x = as.double(x), y = as.double(y), xlab = xlab, ylab = ylab)

within there at:

debugging in: as.double(y)
debug: standardGeneric("as.numeric", .Primitive("as.double"))
Browse[6]> s
exiting from: as.double(y)
Fehler in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

Using a work around similar to what @steffengreup suggests in the last comment

coeftab_plot( coeftab_5123 , par=c("bA","bM") )

works!

probably same reason:

getMethod("plot", "coeftab")
Fehler in getMethod("plot", "coeftab") : 
  Keine Methode für Funktion 'plot' und Signatur coeftab gefunden

from rethinking.

alesitoide avatar alesitoide commented on June 29, 2024

FWIW, I also have seen the same error with the results of compare, like johnkapson. Reinstalling didn't fix it, and I don't see that there's a compare.plot, so I don't think I can use something like the precis.plot workaround.

I know this is old, but may be useful for someone with the same problem.
I had that issue first with precis (fixed using precis_plot) and later with compare. This was not fixed by reinstalling. So this time I went here:
https://github.com/rmcelreath/rethinking/blob/master/R/compare.r

and ran the instruction that begins on line 126.
After that, I could plot the results just using plot()

from rethinking.

mrwenjie avatar mrwenjie commented on June 29, 2024

Thank you @gsgxnet! The method works for me as well! My environment is Rstudio 1.41717 R 4.1.1

from rethinking.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.