Hello, I have some xy data which clearly shows a non-monotonic, peaked triangular trend. You can get an idea of what it looks like with: x<-1:20 y<-c(2*x[1:10]+1,-2*x[11:20]+42) I've tried fitting a quadratic, but it just doesn't the data-structure with the break point adequately. Is there anyway to fit a triangular or 'tent' function to my data in R? Some sample code would be appreciated; I'm not new to R, but I sometimes have difficulty understanding the model-fitting functions (finally figured out how to extrapolate with predict() today!) Thanks! -Dave Bapst, UChicago
Jonathan P Daily
2010-Dec-23 13:22 UTC
[R] Fitting a Triangular Distribution to Bivariate Data
I don't know if any specific package has a triangular distribution, but I know you can fit a model using first degree b-splines with a single knot. library(splines) ?bs x <- 1:100 y <- rnorm(100, ifelse(x <50, x, 100-x), 15) fit <- lm(y ~ bs(x, knots = 50, degree = 1)) -------------------------------------- Jonathan P. Daily Technician - USGS Leetown Science Center 11649 Leetown Road Kearneysville WV, 25430 (304) 724-4480 "Is the room still a room when its empty? Does the room, the thing itself have purpose? Or do we, what's the word... imbue it." - Jubal Early, Firefly r-help-bounces at r-project.org wrote on 12/22/2010 01:19:16 PM:> [image removed] > > [R] Fitting a Triangular Distribution to Bivariate Data > > David Bapst > > to: > > r-help > > 12/22/2010 04:04 PM > > Sent by: > > r-help-bounces at r-project.org > > Hello, > I have some xy data which clearly shows a non-monotonic, peaked > triangular trend. You can get an idea of what it looks like with: > > x<-1:20 > y<-c(2*x[1:10]+1,-2*x[11:20]+42) > > I've tried fitting a quadratic, but it just doesn't the data-structure > with the break point adequately. Is there anyway to fit a triangular > or 'tent' function to my data in R? > > Some sample code would be appreciated; I'm not new to R, but I > sometimes have difficulty understanding the model-fitting functions > (finally figured out how to extrapolate with predict() today!) > > Thanks! > -Dave Bapst, UChicago > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Jinsong Zhao
2010-Dec-23 13:50 UTC
[R] Fitting a Triangular Distribution to Bivariate Data
On 2010-12-23 2:19, David Bapst wrote:> Hello, > I have some xy data which clearly shows a non-monotonic, peaked > triangular trend. You can get an idea of what it looks like with: > > x<-1:20 > y<-c(2*x[1:10]+1,-2*x[11:20]+42) > > I've tried fitting a quadratic, but it just doesn't the data-structure > with the break point adequately. Is there anyway to fit a triangular > or 'tent' function to my data in R? > > Some sample code would be appreciated; I'm not new to R, but I > sometimes have difficulty understanding the model-fitting functions > (finally figured out how to extrapolate with predict() today!) > > Thanks! > -Dave Bapst, UChicago >Hi, you may try the following code: > library(triangle) > library(fitdistrplus) > summary(fitdist(y, "triangle", start = list(a = 1.9, b= 21.1, c = 11.5))) Fitting of the distribution ' triangle ' by maximum likelihood Parameters : estimate Std. Error a -1.400007 2.3523724 b 23.627448 1.9804026 c 13.000000 0.1107073 Loglikelihood: -62.41994 AIC: 130.8399 BIC: 133.8271 Correlation matrix: a b c a 1.00000000 -0.14537297 -0.01203898 b -0.14537297 1.00000000 -0.01439500 c -0.01203898 -0.01439500 1.00000000 HTH ... Jinsong Zhao