[R] spline interpolation

David Winsemius dwinsemius at comcast.net
Sat Feb 5 17:05:37 CET 2011


On Feb 5, 2011, at 10:37 AM, Mike Marchywka wrote:
>> From: dwinsemius at comcast.net
>> To: asanramzan at yahoo.com
>> Date: Sat, 5 Feb 2011 10:24:04 -0500
>> CC: r-help at r-project.org
>> Subject: Re: [R] spline interpolation
>>
>>
>> On Feb 5, 2011, at 9:29 AM, Asan Ramzan wrote:
>>
>>> Hello R-help
>>> I have the following data for a standard curve
>>> concentration(nM),fluorescence
>>> 0,48.34
>>> 2,58.69
>>> 5,70.83
>>> 10,94.73
>>> 20,190.8
>>> 50,436.0
>>> 100, 957.9
>>>
>>> (1)Is there function in R to plot a spline.
>>
>> ?spline
>>
>>> (2)How can I interpolation,say 1000 point from 0nM-100nM and store
>>> this as a
>>> data frame of concentration,fluorescence
>>
>> ?approxfun
>
> I was waiting for some other code to finish and thought I would give  
> this a try.
> I'm still learning R, maybe you can comment on this approach
>
> # made data file
> f1<-read.table("d1.txt",sep=",")
> ss<-smooth.spline(f1$V1,f1$V2)
> # it is alwys good to look first.
> plot(ss)
> lines(ss)
> str(ss)
> ssi<-smooth.spline(f1$V2,f1$V1)
> predict(ssi,200.3456)
> predict(ssi,1:10)
> predict(ssi,1:100)$y
>

Works for me. smooth.spline() is better than spline() for noisy data.  
spline() hits every point. And there are further good examples on ? 
spline and ?smooth.spline. The splines package alos provides the bs()  
and ns() functions for B-splines and natural splines (=cubic splines)  
as terms in model formulae.  (There's no protection against  
extrapolation outside the domain)

f1 <- data.frame(x=1:100, y=log(1:100)+rnorm(100))
  plot(f1)
  ss<-smooth.spline(f1$x,f1$y)
  plot(ss, type="l")
  points(f1)
  predict(ss, 200.3456)
######
$x
[1] 200.3456

$y
[1] 8.185104

> etc
>
>
>
>>
>>> (3)How can I modify the code below so that instead of retrieving a
>>> concentration
>>> with the exact value of fluorescence, it gives me concentration for
>>> the value
>>> that is closest to that fluorescence.
>>
>> ?which.min #(although I would be using "[" rather than subset for
>> that purpose.)
>>
>>>
>>> subset(df,fluorescence==200.3456,select=concentration)
>>>
>>>
>
>> David Winsemius, MD
>> West Hartford, CT
>>
>
> 		 	   		

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list