# Particle filter in R – trivial code example

I am looking for a simple code example of how to run a Particle Filter in R. The pomp package appears to support the state space math bit, but the examples are a little tricky to follow programmatically for a simple OO developer such as myself, particularly how to load the observed data into a pomp object.

Lets say I have a csv file with 1 column of noisy data as input, and I would like to run it through a Particle Filter in order to hopefully clean it up, with the output being the estimations, to another csv file.

 y <- read.csv("C:/Dev/VeryCleverStatArb/inputData.csv", header=FALSE)
#CSV to Pomp object ???
#Run Particle Filter
#Write estimates to csv.


The main difficulty with the examples is loading csv data into a pomp object.

A very simple state space model should be good enough for now.

Any ideas for the R-curious?

-
This might be useful for anyone looking into pairs trading, or algorithmic trading in general, where these is an economic relationship surrounded by noise. –  user1180428 Feb 1 '12 at 3:00
IMHO you are better off coding the filter yourself... –  Dr G Feb 5 '12 at 18:33
Awesome! Samples / tips / pointers for everyone viewing this? An alternate solution's better than no solution. –  user1180428 Feb 10 '12 at 14:23
@user1180428: I've edited my answer, which may now provide a possible alternative for you. –  Wayne Sep 29 '12 at 14:22

## migrated from stackoverflow.comFeb 1 '12 at 14:23

This question came from our site for professional and enthusiast programmers.

EDIT: It seems that most particle filter packages are gone now. However, I have been playing with LaplacesDemon (a Bayesian MCMC package) and it does have the PMC (Population Monte Carlo) function which implements PMC, which is a type of particle filter. Maybe too much machinery for a quick particle filter kind of thing, but a package well worth learning.

You can find package and tutorials at CRAN.

ORIGINAL: To be honest, in the simplest case, pomp is hard to use. It's very flexible for anything you might want to do, but it's like using a space ship to go to the grocery store.

Have you tried looking at Kalman filters (if your data might satisfy assumptions of the Kalman filter), including base functions tsSmooth and StructTS (univariate only), and package dlm? I'd also take a look at loess and other smoothers.

I hope I'm wrong and someone hops on here with a quick, "Here's how to do it for simple univariate data such as you have with some modest assumptions." I'd love to be able to use the package myself.

-
Been there hacked that. Unfortunately, a simple moving average seems to nail a useable signal better than the Kalman filter in this, and many other examples -- Kalman: link , SMA: link The data is stationary, is so far as a Dickey Fuller p < 0.01. Maybe I'm doing it wrong. A script to run a particle filter on this data, and other pairs trading candidates would be awesome (I think). –  user1180428 Feb 3 '12 at 13:18