[R] approx function wierd result
David Winsemius
dwinsemius at comcast.net
Tue Apr 6 17:16:16 CEST 2010
On Apr 6, 2010, at 10:42 AM, marta rufino wrote:
> Dear R-list members,
>
> I am calculating the linear extrapolation for a data set, using the
> function
> found in Hmisc.
>
> x=c(0.0265,-0.0003,0.0142,0.0263,0.0634,0.1145,0.2504)
> y=c(58,107,152,239,362,512,724)
> x1=0.0393216
> approxExtrap(x,y,x1, method="linear")
> approx(x,y,x1)
>
> #to see what is happening:
> plot(x, y, typ="o")
> abline(v=x1, col=8)
>
>
> Which gives x=0.03 and y=163, instead of the expectable which would
> be close
> to y=300.
> Any ideia why this is happening or how to sort this out?
Have you tried:
plot(x,y)
lines(x, approxfun(x,y)(x))
It would clue you in to the fact that your x argument was not ordered
and that the "next x" after 0.0263 is actually x[1] == 0.0265. Now try:
> approxfun(x,y)(0.0263+seq(0,.04, by=0.001) )
[1] 239.00000 64.59079 72.82927 81.06775 89.30623 97.54472
105.78320 114.02168 122.26016
[10] 130.49864 138.73713 146.97561 155.21409 163.45257 171.69106
179.92954 188.16802 196.40650
[19] 204.64499 212.88347 221.12195 229.36043 237.59892 245.83740
254.07588 262.31436 270.55285
[28] 278.79133 287.02981 295.26829 303.50678 311.74526 319.98374
328.22222 336.46070 344.69919
[37] 352.93767 361.17615 364.64188 367.57730 370.51272
And I think it will be clearer what is happening.
In retrospect it would have been clearer to do this initially:
> plot(x,y)
> lines(seq(0.02, 0.2, by= 0.001), approxfun(x,y)(seq(0.02, 0.2, by=
0.001)) )
--
David.
>
> Thank you vey much in advance,
> Best wishes,
> Marta
>
> --
> LNEG_VS_Poli_CMYK_300dpi
>
> Laboratório Nacional de Energia e Geologia, I.P.
>
> Estrada da Portela, Zambujal - Alfragide
> Apartado 7586, 2720-866 Amadora
> www.lneg.pt
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list