[R] Estimating and predicting using "segmented" Package

Melwin win at comets.de
Fri Dec 20 09:34:50 CET 2013


I don't know if this is still relevant for you, but probably for someone  
else.
I ran into the same problem an found two dirty workarounds that can help  
in many cases (but not all). Both can also be combined:

1. repeat the call to "segmented" with different seeds and different  
initial estimates of the breakpoints (psi). Wrap this with "try" to catch  
the error:
<code>
	res= NULL
	while (class(res)[1]!="segmented")
	{	
		some_seed= runif(1) #you may also systematically vary this
		some_psi = runif(2) #you may also systematically vary this.
			#Better tailor this to the distribution of x values in your_data
		model_lm = lm(y~x, data=your_data)

		res = try(segmented(obj=model_lm, seg.Z=~x, model=TRUE,  
psi=list(x=some_psi),
                   control=seg.control(seed=some_seed)), silent=TRUE)
	}
</code>


2. insert dummy points between your actual data to reduce the chance of  
"only 1 datum in an interval". When you supply these dummy data with low  
weights, their effect on the outcome should be low:
<code>
	model_lm = lm(y~x, data=data_with_dummyentries,  
weights=data_with_dummyentries$weight)
	res=segmented(obj=model_lm, seg.Z=~x, model=TRUE, psi=list(x=c(10,20)))
</code>

Hope it helps,

Melwin



More information about the R-help mailing list