[R] downsampling

Jan M. Wiener mail at jan-wiener.net
Mon Jul 27 14:42:33 CEST 2009


Dear Philipp and R-Users,

thank you very much for the help.

However, both approx() and spline() seem to select the number of
required data points from the original data (at the correct positions,
of course) and ignore the remaining data points, as the following
example demonstrates:

> a= c(1,0,2,1,0)

> approx(a,n=3)
$x
[1] 1 3 5

$y
[1] 1 2 0

Essentially, what approx has done (spline does the same) is to simply
select the first, third, and fifth entry (as we want to downsample a 5
point vector into a three point vector). The second and fourth data
point are completely ignored. This can result in quite dramatic changes
of your data, if the data points selected by approx() or spline() happen
to be outliers and if you downsample data by a rather strong factor.

Best,
Jan



Philipp Pagel wrote:
> On Fri, Jul 24, 2009 at 03:16:58AM -0600, Warren Young wrote:
>   
>> Michael Knudsen wrote:
>>     
>>> On Fri, Jul 24, 2009 at 9:32 AM, Jan Wiener<jan.wiener at tuebingen.mpg.de> wrote:
>>>
>>>       
>>>> x=sample(1:5, 115, replace=TRUE)
>>>>
>>>> How do I downsample this vector to 100 entries? Are there any R
>>>> functions or packages that provide such functionality.
>>>>         
>>> What exactly do you mean by downsampling?
>>>       
>> It means that the original 115 points should be treated as a
>> continuous function of x, or t, or whatever the horizontal axis is,
>> with new values coming from this function at 100 evenly-spaced
>> points along this function.
>>     
>
> There probably is a proper function for that and some expert will
> point it out. Until then I'll share my thoughts:
>
> # make up some data
> foo <- data.frame(x= 1:115, y=jitter(sin(1:115/10), 1000))
> plot(foo)
>
> # use approx for interpolation
> bar <- approx(foo, n=30)
> lines(bar, col='red', lwd=2)
>
> # or use spline for interpolation
> bar <- spline(foo, n=30)
> lines(bar, col='green', lwd=2)
>
> # or fit a loess curve
> # had to play with span to make it look ok
> model <- loess(y~x, foo, span=1/2)   
> x <- seq(1, 115, length.out=30)
> bar <- predict(model, newdata=data.frame(x=x, y=NA))
> lines(x, bar, col='blue', lwd=2)
>
>
> Jan, does that help a little?
>
> cu
> 	Philipp
>
>   


-- 
Dr. Jan M. Wiener
Centre for Cognitive Science
University of Freiburg, Institute of Computer Science and Social Research (IIG)
Friedrichstr. 50, D-79098 Freiburg, GERMANY
-
e-mail: mail at jan-wiener.net
phone: ++49 (0)761 203 4951
url: www.jan-wiener.net




More information about the R-help mailing list