Julia random number generators not working - julia

New to Julia, trying to just run this example
http://timsalimans.com/gibbs-sampling-with-julia/
but I cannot use the functions "randg" as it says it is not defined. Am I missing something?

I pieced together information from http://www.johnmyleswhite.com/notebook/2012/06/21/the-great-julia-rng-refactor/ and http://www.cita.utoronto.ca/~nolta/julia/pygments/monokai/deprecated.jl.html.
randg() is deprecated. You need to use the rand() function on a Gamma object from the Distributions package.

Related

unrecognized function Nn in R

I am learning R package SimInf to simulate data-driven stochastic epidemiological models. As I was reading the documentation I came across an unrecognized funcion Nn when defining a function for epicurves. Specifically, this line:
j <- sample(seq_len(Nn(model)), 1)
Values of model are integers. My guess is that Nn selects non-negative values, however my R does not recognize this function. From documentation it does not look like they pre-defined Nn either. Can someone please tell if they know what "Nn" is for? Thank you.
A way to go is always taking the package-name and triple-":" it, such that you can find nearly all functions inside the package. Maybe you are familiar with namespacing a function via packageName::functionFrompackageTocall. The packageName::: shows (nearly) all functions defined in this package. If you do this in R-Studio with SimInf:: and SimInf:::, you will see that the latter gives much more functions. But you can only find the functions SimInf:::Nd and SimInf:::Nc, not the Nn-function. Hence you will have to go to the github-sources of the package, in this case https://github.com/stewid/SimInf .Then search for Nn the whole repository. You will see that it seems like it is always an int, but this doesn't help you since you want to get ii as a function, not as a variable. Scrolling further down in the search-results, you will find the NEWS.md-file which mentions The 'Nn' function to determine the number of nodes in a model has been replaced with the S4 method 'n_nodes'. in the https://github.com/stewid/SimInf/blob/fd7eb4a29b82a4a97f64b528bb0e78e5474aa8a5/NEWS.md file under SimInf 8.0.0 (2020-09-13). Hence having a current version of SimInf installed, it shouldn't use the method Nn anymore. If you use it in your code, replace it by n_nodes. If you find it in current package code, you can email the package-maintainer that you found a bug in his code.
TLDR: Nn is an outdated version of n_nodes

How to run a hodges-lehmann test in R or SPSS

How can I compute the Hodges-Lehmann (aligned ranks) test in R or SPSS? Is there are ready-made functions to call? I understood the formula,but if the Hodges-Lehmann test has already been implemented, there would be no need to create a new function .
I tried hodgeslehmann() in the senstrat package, but it's not what I need. It only computes Hodges-Lehmann Aligned Ranks, but couldn't give the statistical value.
An R version in a package called DescTools is documented here and also here.
Note especially that it will supply confidence intervals but only if you make use of the conf.level argument.
And there is SPSS Syntax code here, but you may prefer something that has had better open-source scrutiny.

Equivalent to R's sample(x,y,prob=) in Julia

Julia-users: is there an equivalent to R's sample(x,y,prob=) to sample from a given set of values with weighted probabilities? The rand() function is equivalent to sample(x,y), but as far as I'm aware there's no option to add probability weights... Any help appreciated!
OK - done a bit more digging and wsample from the Distributions package seems to be the answer:
using Distributions
wsample(population, weights, n)
Next time I'll look harder before posting!
wsample exists also in the StatsBase.jl package (which I am not sure is a more recent addition compared to when the question was first answered)
If you go by StatsBase.jl you can also just use "sample":
using StatsBase
sample(population, Weights(weights), n)
In both packages you can also set a random number generator and whether to take with replacement for both functions too.

Passing rng to Distributions

I am using Distributions.jl since the julia standard library does not support all necessary distributions
Within 1 special function I need the same random numbers. I am looking for a way to use always the same random number generator for this part, but don't now how to pass it to Distributions.jl
Using srand is not want I want, since then the global rng is reset.
No, it's not yet possible, due to the fact that Distributions.jl currently uses the Rmath library for this (see https://github.com/JuliaStats/Distributions.jl/issues/197), but it is on the to-do list.

caret package: Is it possible to implement my own bootstrapping method?

I am using caret package for R to select variables for my model. When using rfe command, one should pass rfeControl object, which has a method parameter. Options for this parameter are boot, cv, LOOCV and LGOCV. Since I am dealing with time series data I need to use special bootstrapping/cross-validation techniques as normal ones do not apply for time series data (otherwise distributions get corrupted etc.).
My question is how would I plug-in my own implementation of bootstrapping but still use caret rfe method, which has every other thing I need.
There isn't an easy way. If you study the code for rfe.default() you will note that in cases where method = "boot" the createResample() function is used. This is the function that generates the bootstrap samples. Similar functions are used for the other CV methods.
There is a hard way; overtake the create*() function that is most appropriate; say you want to do a block bootstrap or ME bootstrap, take over the createResample() function and use method = "boot", or if you want a special form of CV, use method = "cv" and take over createFolds().
You will need to write your own create*() function and replace the one in the caret NAMESPACE with your version. Not easy but eminently doable. Say you write your own createResample() function; first you need to note that this function creates n = times bootstrap samples returning this in a matrix with times columns and as many rows as your have samples. You need to write a custom createResample() function that returns the same object but which performs the time series bootstrapping you want to employ.
Once you have written that function you then need to get it into the caret namespace so that it is used by functions in the caret package. For this you use assignInNamespace(). Say your new bootstrapping function is called createMyResample() and it is your workspace, to insert this into the caret namespace do:
assignInNamespace("createResample", createMyResample, ns = "caret")
Sorry I can't be more specific but you don't say how you want the bootstrap/CV to be performed nor what R code you want to use to do the resampling. If you provide further details on how you would do the resampling I will take another look and see if I can help you write your create*() function.
Failing all of this, contact Max Kuhn, the author and maintainer of caret; he may be able to advice further or at least you can suggest this feature as a wish-list for a future version.

Resources