Statistics and R

If you have written many functions, it would be more convenient to build up a package. It is also wonderful to share the tools you created by developing a R package. I will introduce some easy steps to achieve this goal. Preparation We need to creat a project for the new package. Creat a new project Click these butthons in your Rstudio. File > New project > New Dictionary > R package
2022-01-18
1 min read
R
There is a package Parallel detectCores(): return how many cores exist on your PC. cl <- makeCluster(np): initialize a cluster mc with np processes. Apply Operations using Clusters: clusterCall, clusterEvalQ, clusterApply, clusterExport, clusterMap, parLapply, parSapply, parApply, parRapply, parCapply stopCluster(cl): release the occupied cores. An example Check how many cores exist on my PC. library(parallel) detectCores() ## [1] 12 Define a function. sim <- function(x){ rnorm(1, mean = x) } Use parallel to speed up the calculation.
2022-01-08
1 min read
Steps for i=1,2,,ni=1,2,\dots,n Sample candidate value xix^i from a proposal distribution q(xixi1)q(x^i|x^{i-1}) Compute acceptance probability: α=p(xi)q(xi1xi)p(xi1)q(xixi1)\alpha=\frac{p(x^i)q(x^{i-1}|x^i)}{p(x^{i-1})q(x^i|x^{i-1})} 3. Compute r=min(1,α)r=min(1,\alpha) Sample UUnif(0,1)U\sim Unif(0,1) xi={xi1,amp;ult;rxi,amp;ur x^i=\begin{cases} x^{i-1}, &u<r \\x^i, &u\geq r\end{cases} An example
2022-01-07
1 min read