# code

### Autocorrelation Matrix in R

December 25, 2010 |

I have been simulating a lot of data lately  with various covariance (correlation) structures, and one that I have been using is the autocorrelation (or autoregressive) structure, where there is a “lag” between variables. The matrix is a v-dimension matrix of the form \begin{bmatrix} 1 & \rho & \rho^2 & \dots & \rho^{v-1}\\ \... [Read more...]

### Pretty R code in the blog

November 5, 2010 |

David Smith (alias @revodavid, see also on the Revolutions blog, here) pointed out that my R code was not easy to read (not only due to my computing skills, but mainly because of the typography I use). He suggested that I use the Pretty R tool (her... [Read more...]

### Dumping functions from the global environment into an R script file

September 15, 2010 |

Looking at a project you didn’t touch for years poses many challenges. The less documentation and organization you had in your files, the more time you’ll have to spend tracing back what you did back when the code was written. I just opened up such a project, that ... [Read more...]

### How to upgrade R on windows – another strategy (and the R code to do it)

April 23, 2010 |

Update: In the end of the post I added simple step by step instruction on how to move to the new system. I STRONGLY suggest using the code only after you read the entire post. Background If you didn’t hear it by now – R 2.11.0 is out with a bunch ... [Read more...]

### The difference between “letters[c(1,NA)]” and “letters[c(NA,NA)]“

April 22, 2010 |

In David Smith’s latest blog post (which, in a sense, is a continued response to the latest public attack on R), there was a comment by Barry that caught my eye. Barry wrote: Even I get caught out on R quirks after 20 years of using it. Compare letters[c(12,... [Read more...]

### Historical / Future Volatility Correlation Stability

April 11, 2010 |

Michael Stokes, author of the MarketSci blog recently published a thought-provoking post about the correlation between historical and future volatility (measured as the standard deviation of daily close price percentage changes). This post is intended... [Read more...]

### Correlation scatter-plot matrix for ordered-categorical data

April 7, 2010 |

When analyzing a questionnaire, one often wants to view the correlation between two or more Likert questionnaire item’s (for example: two ordered categorical vectors ranging from 1 to 5). When dealing with several such Likert variable’s, a clear presentation of all the pairwise relation’s between our variable can be ... [Read more...]

### A von Mises variate…

March 25, 2010 |

Inspired from a mail that came along the previous random generation post the following question rised : How to draw random variates from the Von Mises distribution? First of all let’s check the pdf of the probability rule, it is , for . Ok, I admit that Bessels functions can be a ... [Read more...]

### The distribution of rho…

March 21, 2010 |

There was a post here about obtaining non-standard p-values for testing the correlation coefficient. The R-library SuppDists deals with this problem efficiently. library(SuppDists) plot(function(x)dPearson(x,N=23,rho=0.7),-1,1,ylim=c(0,10),ylab="density") plot(function(x)dPearson(x,N=23,rho=0),-1,1,add=TRUE,col="steelblue") plot(function(... [Read more...]

### \pi day!

March 14, 2010 |

It’s π-day today so we gonna have a little fun today with Buffon’s needle and of course R. A well known approximation to the value of $latex \pi$ is the experiment tha Buffon performed using a needle of length,$latex l$. What I do in the next is ... [Read more...]

### A quicky..

February 22, 2010 |

If you’re (and you should) interested in principal components then take a good look at this. The linked post will take you by hand to do everything from scratch. If you’re not in the mood then the dollowing R functions will help you. An example. # Generates sample matrix ... [Read more...]

### Siegel-Tukey: a Non-parametric test for equality in variability (R code)

February 22, 2010 |

Daniel Malter just shared on the R mailing list (link to the thread) his code for performing the Siegel-Tukey (Nonparametric) test for equality in variability. Excited about the find, I contacted Daniel asking if I could republish his code here, and he kindly replied “yes”. From here on I copy ... [Read more...]

### Post hoc analysis for Friedman’s Test (R code)

February 22, 2010 |

My goal in this post is to give an overview of Friedman’s Test and then offer R code to perform post hoc analysis on Friedman’s Test results. (The R function can be downloaded from here) Preface: What is Friedman’s Test Friedman test is a non-parametric randomized block ...

### The truncated Poisson

February 21, 2010 |

A common model for counts data is the Poisson. There are cases however that we only record positive counts, ie there is a truncation of 0. This is the truncated Poisson model. To study this model we only need the total counts and the sample size. This comes from the sufficient ... [Read more...]

### Uh!

February 20, 2010 |

Didn't know this... a data 0 2 4 7+ 25 34 12 5 It's becoming clear that I have learned R in the most unstructured way...I always do it in two stages :ashamed: [Read more...]

### Barnard’s exact test – a powerful alternative for Fisher’s exact test (implemented in R)

February 7, 2010 |

(The R code for Barnard’s exact test is at the end of the article, and you could also just download it from here) About Barnard’s exact test About half a year ago, I was studying various statistical methods to employ on contingency tables. I came across a promising ...

### R> if (done=TRUE) tweet me!

December 11, 2009 |

Let’s say that you’re fitting a cumbersome model so time is not to waste over a PC staring at the screen half anxious-half bored… Then, you can always leave and go on with meetings and all your daily routine and have R notify you the results! How? We ... [Read more...]

### Create factor variables in R

December 6, 2009 |

Instead of the factor() function which usually applies after defining a vector there’s the gl() base function to do this in one step, eg freq [Read more...]

### Tactical asset allocation using blotter

November 18, 2009 |

blotter is an R package that tracks the P&L of your trading systems (or simulations), even if your portfolio spans many security types and/or currencies. This post uses blotter to track a simple two-ETF trading system. The contents of this post b... [Read more...]

### Confidence we seek…

November 18, 2009 |

Estimating a proportion at first looks elementary. Hail to aymptotics, right? Well, initially it might seem efficient to iuse the fact that . In other words the classical confidence interval relies on the inversion of Wald’s test. A function to ease the computation is the following (not really needed!). waldci [Read more...]
1 2 3