Is there a function check_rhat() in rstan package? - stan

In the following page, I find a function check_rhat(). However in the R console, there does not exist even if using rstan:::.
So, I made a similar function for diagnosis of rhats in my package, but, if there exist some function to evaluate the rhat I want to use it (if it exists).
https://betanalpha.github.io/assets/case_studies/divergences_and_bias.html

That function comes into the R session via source("stan-utility.R") and is defined here. It is not in the rstan package.

Related

could not find function "scaling_UV"

I'm attempting to scale a dataset by its unit variance using the scaling_UV() function in R from the santaR package. I have installed and loaded the santaR library but I am still being told that the scaling_UV() function could not be found.
How can I use this function or is there another function that performs the same operation? I will even perform the scaling manually if necessary.
I assume you have already used library(santaR) without errors.
If the function is exported by the package, santaR::scaling_UV() should work.
You can also try santar:::scaling_UV() (notice the three colons).
According to the documentation, the function should be present in version 1.2.3

ts_backtesting function from TSstudio is not showing in the list of functions in TSstudio package in R

I have been trying to perform the horse approach for finding the best model to run on the time series data but when I run ts_backtesting function, there is a message in console that
Error in ts_backtesting(x) : could not find function "ts_backtesting"
Can anyone please help me know why I am getting such error and how to avoid that.
I checked the latest version of the package, didn't find this function anymore
https://cran.r-project.org/web/packages/TSstudio/index.html,
instead, a function called 'train_model' can be used to do a similar thing.

"could not find function" error though function is in the package

In R, I'm getting an error "could not find function...". The function is present inside the package. Still when I run the package, getting the error.
I am getting this error in the ChainLadder package while running MackChainLadderFunctions.R. For example, the function checktriangle is present inside the package in Chainladder.R. Still, R is not able to recognize the function or call the function.
Two problems here.
function names are case-sensitive (checkTriangle, not checktriangle)
checkTriangle is not exported from the package (i.e., it's a private function intended for use within the package only), so you need ::: to access it ... try ChainLadder:::checkTriangle.
Using private functions is "at your own risk/programmer beware"; private functions are undocumented, may change in future versions, etc.. If you can find a way to do what you need to do with public functions, that is generally preferred.
AFAICT you're running into this problem because you're trying to source() (or cut-and-paste) package code in your R session. This shouldn't happen if you load the package with library("ChainLadder") and use the public functions (if it does, please edit your question to give a little more context about how you're using the package ...)

Building R package: no visible global function definition for 'subject'

I'm building an R package for the first time and am having some trouble. I am doing an R CMD Check and am getting the following error:
get.AlignedPositions: no visible global function definition for 'subject'
I am not sure what is causing this. I don't even have a "subject" variable in my code. The code is rather lengthy so I rather not paste all of it unless someone asks in a comment. Is there something specific I should look for? The only thing I can think of is that I have a line like this:
alignment <-pairwiseAlignment(pattern = canonical.protein, subject=protein.extracted, patternQuality=patternQuality,
subjectQuality=subjectQuality,type = type, substitutionMatrix= substitutionMatrix,
fuzzyMatrix=fuzzyMatrix,gapOpening=gapOpening,gapExtension=gapExtension,
scoreOnly=scoreOnly)
but subject is defined by the pairwiseAlignment function in the Biostrings package. Thank you for your help!
R spotted a function, subject, being used without a function called subject being available. One possible reason for this is explained in this discussion on R-devel. In that case code is used conditionally, e.g. if a certain package is installed we use its functionality. When checking the package on a system which does not have this package installed, we run in to these kinds of warnings. So please check if this might be the case. Alternatively, you might have made a mistake by calling subject while no function existed, e.g. subject was not a function but just an object.

Embed fix() function within .R script?

I am looking for a way to embed the fix() function within a script. Basically, here's what I'm currently doing:
I load a certain package. For example, library(PerformanceAnalytics)
I call the fix() function to edit a couple functions within the loaded package. Example, fix(VaR).
Then, using R's built-in editor, I copy-paste my function over the one originally loaded from the package.
Finally, I source in my .R script which calls the above functions I fixed and performs the computations I need.
Essentially, I'd like to streamline Step 3 above. Rather than having to manually type fix(function) and copy-paste over the original functions within the loaded package, I'd rather just have it done within a script I source.
Is there anyway to accomplish this?
FYI, I have reached out to the package's creator and loading a re-compiled version of the package with my modified code is out of the question.
Maybe source your functions and then use assignInNamespace?
EDIT #1:
The above won't work because assignInNamespace doesn't alter objects that have been exported. Instead,
put your functions in a file (foo.R)
load the package
then source(foo.R) or
sys.source(foo.R, envir=attach(NULL, name="myenv"))
Your functions will be higher up on the search list if you load them after the package, so R will find them before getting to the package's functions with the same name.
EDIT #2:
I didn't realize VaR called unexported functions in the namespace. That's why EDIT #1 doesn't work. To get it to work, you would need to explicitly reference all unexported PerformanceAnalytics functions used in VaR (e.g. change VaR.Gaussian to PerformanceAnalytics:::VaR.Gaussian).
See this post on R-devel for a couple other approaches. I couldn't quickly get Prof. Ripley's solution to work (I get the same error as in EDIT #1) and I didn't try Gabor's solution.
You can edit the body directly, discussed here:
What ways are there to edit a function in R?
You can download the packages source from CRAN. Edit the function (it will be found in PackageName/R), then install this package into R and just use it that way.
You can even change the package name in the DESCRIPTION file ... call it "PerformanceAnalytics2", then in R you just library(PerformanceAnalytics2) and use it as you would the original package.

Resources