Dear R-community, I have a fitted bivariate polynomial, i.e: fit = lm(cbind(x, y)~poly(t, 15)) and I would like to determine the length of the line in the interval t [a, b]. Obviously, I could use predict and go through all the points, i.e. for (t in a:(b-1)) { length = length + sqrt((x.pred[t] - x.pred[t+1])^2 + (y.pred[t] - y.pred[t+1])^2) } but that would take very long given the amount of data I have. Do you know of any better solutions? Many thanks! Nicolas [[alternative HTML version deleted]]
I think this will do it: sum(sqrt(diff(x.pred)^2 + diff(y.pred)^2)) Michael On Tue, Nov 8, 2011 at 11:34 AM, Nicolas Schuck <nico.schuck at googlemail.com> wrote:> Dear R-community, > > I have a fitted bivariate polynomial, i.e: > > fit = lm(cbind(x, y)~poly(t, 15)) > > and I would like to determine the length of the line in the interval t > [a, b]. Obviously, I could use predict and go through all the points, i.e. > > for (t in a:(b-1)) { > length = length + sqrt((x.pred[t] - x.pred[t+1])^2 + (y.pred[t] - > y.pred[t+1])^2) > } > > but that would take very long given the amount of data I have. Do you know > of any better solutions? > > Many thanks! > Nicolas > > ? ? ? ?[[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. >
I was thinking of an analytical solution, but using diff() works great as well! thanks for help, nico On Nov 10, 2011, at 2:55 AM, R. Michael Weylandt wrote:> I think this will do it: > > sum(sqrt(diff(x.pred)^2 + diff(y.pred)^2)) > > Michael > > On Tue, Nov 8, 2011 at 11:34 AM, Nicolas Schuck > <nico.schuck at googlemail.com> wrote: >> Dear R-community, >> >> I have a fitted bivariate polynomial, i.e: >> >> fit = lm(cbind(x, y)~poly(t, 15)) >> >> and I would like to determine the length of the line in the interval t >> [a, b]. Obviously, I could use predict and go through all the points, i.e. >> >> for (t in a:(b-1)) { >> length = length + sqrt((x.pred[t] - x.pred[t+1])^2 + (y.pred[t] - >> y.pred[t+1])^2) >> } >> >> but that would take very long given the amount of data I have. Do you know >> of any better solutions? >> >> Many thanks! >> Nicolas >> >> [[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. >>