On Fri, 2010-07-16 at 14:25 +1200, Peter Alspach wrote: > Tena koe Stephen > > You'll need to use loess(w[,"bkf_depths"]~w[,"measure"])) and predict > with a sufficiently dense sequence of w[,'measure'] to interpolate to > an 'accuracy' that meets your requirements. Actually, if I understand > your data correctly, it could be that simple linear interpolation > (i.e., without loess) would be sufficient (even better). > > HTH .... > > Peter Alspach
If Stephen is going to use a predict method, then it would probably be wise to stop abusing the formula interface that many R model functions have. Doing this: loess(w[,"bkf_depths"]~w[,"measure"]) is a recipe for much head scratching when predict() won't work on your fitted model with new data. Also, the above model call is somewhat obfuscated by the way it is written. This is much better: loess(bkf_depths ~ measure, data = w) Much easier to read, plus prediction works!: > pdat <- with(w, data.frame(measure = seq(min(measure), max(measure), length = > 100))) > mod <- loess(w[,"bkf_depths"]~w[,"measure"]) > predict(mod, pdat) ## Whoops, this fails [1] 0.11928879 0.33733102 0.53497599 0.71073264 0.86711027 1.00276470 [7] 1.00276470 1.11816623 1.21681583 1.29127522 1.33860899 1.36907242 [13] 1.39292078 1.40297664 1.39390373 1.37871901 1.37043942 1.38674537 [19] 1.42032404 1.44670249 1.44140783 1.41272292 1.37328779 1.30516530 [25] 1.19041829 1.03514669 1.03514669 1.00074242 0.84832562 0.62322528 [31] 0.36293723 0.06856913 -0.05847241 Warning message: 'newdata' had 100 rows but variable(s) found have 33 rows > mod2 <- loess(bkf_depths ~ measure, data = w) > head(p <- predict(mod2, pdat)) [1] 0.1192888 0.1836197 0.2465784 0.3080418 0.3678864 0.4259892 > length(p) [1] 100 Stephen, see ?approx if you want to give the interpolation a go. Either way, you'll need a reasonably fine grid (of points in measure) if you want to constrain the 0 depth locations well. wapp <- with(w, approx(measure, bkf_depths, xout = seq(5, max(measure), length = 200))) i.e. we focus on the area where we want to find 0 depth with 200 locations spread across this region. I think the line below will then find the point on the tape that is closest to 0 without being out of the water > with(wapp, x[which.min(y[y > 0])]) [1] 5.560402 HTH G > > > -----Original Message----- > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > > project.org] On Behalf Of stephen sefick > > Sent: Friday, 16 July 2010 11:33 a.m. > > To: r-help@r-project.org > > Subject: [R] loess line predicting number where the line crosses zero > > twice > > > > These data represent stream channel cross-sectional surveys. I would > > like to be able to find the measurement on the tape (measure) where > > the Bank Full Depth (bkf_depths) is 0. This will happen twice because > > the channel has two sides. I thought fitting a loess line to these > > data and then predicting the measurment number would do it. I was > > wrong. Below is my failed attempt. My naive thought is this - I > > would like to run my finger along this line and when it hits zero I > > would like to pick out the value of measure. This should happen twice. > > Any help would be greatly appreciated! > > > > w <- (structure(list(measure = c(0, 0.2, 0.4, 0.6, 0.8, 1, 1, 1.2, > > 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6, 3.8, > > 4, 4.2, 4.4, 4.6, 4.8, 4.8, 4.84, 5, 5.2, 5.4, 5.6, 5.68), bkf_depths = > > c(0, > > 0.44, 0.46, 0.66, 0.9, 1.1, 1.1, 1.2, 1.3, 1.33, 1.36, 1.36, > > 1.36, 1.38, 1.38, 1.36, 1.36, 1.38, 1.37, 1.37, 1.34, 1.32, 1.36, > > 1.35, 1.36, 1.4, 1.4, 1.3, 0.7, 0.57, 0.21, -0.05, -0.12)), .Names = > > c("measure", > > "bkf_depths"), row.names = c(32L, 1L, 2L, 3L, 4L, 5L, 29L, 6L, > > 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, > > 20L, 21L, 22L, 23L, 31L, 24L, 30L, 25L, 26L, 27L, 28L, 33L), class = > > "data.frame") > > ) > > > > plot(w[,"bkf_depths"]~w[,"measure"]) > > lines(loess(w[,"bkf_depths"]~w[,"measure"])) > > > > #this is what I tried and there should be two 0s one at the left side > > of the graph and one at the right side > > predict(loess(w[,"measure"]~w[,"bkf_depths"]), 0) > > > > > > kindest regards, > > > > > > -- > > Stephen Sefick > > ____________________________________ > > | Auburn University | > > | Department of Biological Sciences | > > | 331 Funchess Hall | > > | Auburn, Alabama | > > | 36849 | > > |___________________________________| > > | sas0...@auburn.edu | > > | http://www.auburn.edu/~sas0025 | > > |___________________________________| > > > > Let's not spend our time and resources thinking about things that are > > so little or so large that all they really do for us is puff us up and > > make us feel like gods. We are mammals, and have not exhausted the > > annoying little problems of being mammals. > > > > -K. Mullis > > > > ______________________________________________ > > R-help@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. > ______________________________________________ > R-help@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. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ R-help@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.