Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Generate data from a segmented model and plot it
- a = 2
- b = 0.125
- d = 2
- c = a*d^(3/2) - b*d
- x = c(0:50)/10
- set.seed(12345)
- y = (x<d)*a*x^(3/2) + (x>=d)*(b*x+c) + rnorm(51,0,0.5)
- # Function to calculate sum of squared distance from observed value
- ss = function(parameters, y) {
- a = parameters[1]
- b = parameters[2]
- d = parameters[3]
- c = a*d^(3/2) - b*d
- sum((y - ((x<d)*a*x^(3/2) + (x>=d)*(b*x + a*d^(3/2) - b*d)))^2)
- }
- sol = optim(c(1.5,0.1,1.5), ss, y=y)
- sol$par
- #[1] 2.05963514 0.04598089 2.01290367
Add Comment
Please, Sign In to add comment