[R] fitting a hyperbola to data points

PIKAL Petr petr.pikal at precheza.cz
Tue Apr 9 10:35:35 CEST 2013


Hi

It seems that some linearisation can be done by
with(dat,plot(exp(Requests), log(1/Time)))

it can be fitted by

fit<-lm(log(1/Time)~exp(Requests), data=dat)
or
library(MASS)
fit.r<-lqs(log(1/Time)~exp(Requests), data=dat)

coef(fit.r)
  (Intercept) exp(Requests) 
 -7.137549922  -0.002378384 
fff<- function(x)  1/(exp(-7.137549922  -0.002378384*exp(x)))
points(dat$Requests, fff(dat$Requests), col=2)

It is not perfect and especially points
 
> dat[c(91,94),]
   Requests Time
91 5.103478 3618
94 5.262253 2908

are quite far from predicted model.

Also it is probably better to use nonlinear regression 

?nls

Regards
Petr 



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Manoj Srivastava
> Sent: Monday, April 08, 2013 6:05 PM
> To: r-help at r-project.org
> Subject: Re: [R] fitting a hyperbola to data points
> 
> On Mon, Apr 08 2013, PIKAL Petr wrote:
> 
>         Thanks for responding.
> 
> > without data we can provide just basic help.
> > fit<-lm(Time~I(1/Requests))
> > shall give you hyperbolic fit.
> 
> > You can test if your data follow this assumption by plot(1/Requests,
> > Time) which shall for straight line.
> >
> > anyway, when you want to provide data use
> > dput(your.data) and copy console output directly to your mail.
> 
>         Pardon. I had put my data on pastebin, but here is the dput:
> > dput(dat)
> structure(list(Requests = c(0.045364248295124, 0.11341062073781,
> 0.16633557708212, 0.20413911732806, 0.26462478172156, 0.31754973806587,
> 0.37047469441018, 0.42339965075449, 0.47632460709881, 0.52168885539393,
> 0.58217451978743, 0.62753876808255, 0.68802443247605, 0.73338868077118,
> 0.79387434516468, 0.8392385934598, 0.8997242578533, 0.94508850614843,
> 0.99801346249274, 1.050938418837, 1.1038633751814, 1.1567883315257,
> 1.20971328787, 1.2626382442143, 1.3155632005586, 1.3684881569029,
> 1.4214131132472, 1.4743380695915, 1.5272630259359, 1.5801879822802,
> 1.6255522305753, 1.6860378949688, 1.7389628513131, 1.7843270996082,
> 1.8448127640017, 1.897737720346, 1.9431019686412, 1.9960269249855,
> 2.056512589379, 2.1018768376741, 2.1548017940184, 2.2152874584119,
> 2.260651706707, 2.3135766630513, 2.3665016193957, 2.41942657574,
> 2.4723515320843, 2.5252764884286, 2.5782014447729, 2.6311264011172,
> 2.6764906494123, 2.7369763138058, 2.7899012701502, 2.8428262264945,
> 2.8881904747896, 2.9486761391831, 2.9940403874782, 3.1603759645603,
> 3.2057402128555, 3.266225877249, 3.3115901255441, 3.3645150818884,
> 3.4174400382327, 3.4779257026262, 3.5232899509213, 3.5762149072656,
> 3.62913986361, 3.6820648199543, 3.7349897762986, 3.7879147326429,
> 3.8408396889872, 3.8937646453315, 3.9391288936266, 3.9996145580201,
> 4.0525395143644, 4.0979037626596, 4.1583894270531, 4.2113143833974,
> 4.2566786316925, 4.317164296086, 4.3625285443811, 4.4230142087746,
> 4.4683784570698, 4.5213034134141, 4.5817890778076, 4.7330032387913,
> 4.7934889031848, 4.8388531514799, 4.9976280205129, 5.0505529768572,
> 5.1034779332015, 5.1564028895458, 5.2017671378409, 5.2622528022344 ),
> Time = c(1289L, 1262L, 1272L, 1222L, 1243L, 1259L, 1266L, 1242L, 1249L,
> 1232L, 1303L, 1236L, 1251L, 1263L, 1234L, 1226L, 1232L, 1246L, 1249L,
> 1272L, 1263L, 1247L, 1253L, 1257L, 1267L, 1262L, 1284L, 1266L, 1269L,
> 1268L, 1273L, 1261L, 1261L, 1264L, 1276L, 1283L, 1275L, 1277L, 1293L,
> 1285L, 1284L, 1289L, 1290L, 1289L, 1300L, 1292L, 1300L, 1291L, 1297L,
> 1310L, 1303L, 1311L, 1317L, 1345L, 1315L, 1327L, 1322L, 1597L, 1623L,
> 1510L, 1372L, 1429L, 1371L, 1365L, 1357L, 1366L, 1402L, 1370L, 1407L,
> 1373L, 1399L, 1420L, 1405L, 1393L, 1428L, 1394L, 1422L, 1425L, 1457L,
> 1501L, 1426L, 1539L, 1492L, 1476L, 1493L, 1580L, 1670L, 1556L, 1661L,
> 1593L, 3618L, 1903L, 1941L, 2908L)), .Names = c("Requests", "Time"),
> class = "data.frame", row.names = c(NA, -94L))
> 
> 
>         When plotting with  plot(1/Requests, Time), I still get a
> parabolic line, not a linear one. Perhaps there is a data transform I
> can do to get this into a linear mode?
> 
>         I have also plotted the data (Time vs Requests and 1/Requests)
> on a log and log-log scale, but the curve remains stubbornly  curved.
> This seems to argue against an exponential relationship, does  it not?
> 
> 
>         thanks again.
> 
>         manoj
> 
> 
> >>         I am new to R, and I suspect I am missing something simple.
> 
> >>         I have a data set that  performance data that correlates
> >> request  rate to response times
> >>         http://pastebin.com/Xhg0RaUp
> >>  There is some jitter in the data, but mostly it looks like a hockey
> >> puck curve. It does not get converted into a straight line when I
> >> tried log conversions, so it does not seem to be a power series
> relationship.
> 
> >>         My expectation is that the data will fit a curve that is a
> >> hyperbola, but I don't know how to formulate that regression. How
> >> does one fit data to a general function
> >>    AX^2 + Bxy + Cy^2 +D = 0
> 
> >>         I have tried polynomial functions and inverse functions
> >>  lm2 = lm(Time ~ Requests + I(Requests^2) +  I(Requests^3))  but
> that
> >> does not appear to be close.
> 
> >>         Any pointers appreciated.
> 
> --
> Many of us spend half our life wishing for things we could have if we
> didn't spend half our time wishing.  -- Alexander Woollcott Manoj
> Srivastava <srivasta at acm.org> <http://www.golden-gryphon.com/>
> 4096R/C5779A1C E37E 5EC5 2A01 DA25 AD20  05B6 CF48 9438 C577 9A1C



More information about the R-help mailing list