cansim icon indicating copy to clipboard operation
cansim copied to clipboard

Add functionality for StatCan trend estimates

Open mountainMath opened this issue 6 years ago • 3 comments

StatCan has a standard procedure for trend estimates, @jciconsult shared his R implementation for the code. We could add this to the base package, it is useful for reproducing StatCan results. Or we could start a separate package with helper functions. Thoughts @dshkol?

Implementation below.

#jci implementation of stc trend
stc_trend<-function(x,method=1){
	weights<-c(
	-0.027,	-0.007,	0.031,	0.067,	0.136,	0.188,	0.224,	0.188,	0.136,	0.067,	0.031,	-0.007,	-0.027)
	#first do the base filter
	result1<-rep(NA,length(x))
	if(method %in% c(1,2)){
		result1<-filter(x,weights,sides=2)
		#now do first 6 cells
		for(icell in 1:6){
			cell_weights<-tail(weights,icell+6)
			cell_weights<-cell_weights/sum(cell_weights)
			numb_weights<-length(cell_weights)
			input_data<-head(x,numb_weights)
			cell_value<-sum(cell_weights*input_data)
			result1[icell]<-cell_value
		}
	}
	#now do last 6 cells
	vlength<-length(result1)
	if(method %in% c(1,3)){
		for(icell in 1:6){
		cell_weights<-head(weights,icell+6)
			cell_weights<-cell_weights/sum(cell_weights)
			numb_weights<-length(cell_weights)
			input_data<-tail(x,numb_weights)
			cell_value<-sum(cell_weights*input_data)
			result1[vlength-icell+1]<-cell_value
		}
	}
	stc_trend<-result1
}

mountainMath avatar Aug 13 '18 05:08 mountainMath

I also wonder if this trend line approach is already implemented in other R packages that specialize in trend line like e.g. the trend package.

mountainMath avatar Aug 13 '18 05:08 mountainMath

Have not found it. The:idea is to duplicate their capability in both tibble and regular and xts

from Paul Jacobson's mobile device

On Mon, Aug 13, 2018, 01:06 Jens von Bergmann, [email protected] wrote:

I also wonder if this trend line approach is already implemented in other R packages that specialize in trend line like e.g. the trend package https://cran.r-project.org/web/packages/trend/trend.pdf.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mountainMath/cansim/issues/33#issuecomment-412409517, or mute the thread https://github.com/notifications/unsubscribe-auth/AUAF-hpzr2Yn7Ktch03kv3faUrt0avmGks5uQQlFgaJpZM4V54wH .

jciconsult avatar Aug 13 '18 12:08 jciconsult

Great idea.

I think in the code above the first "if" statement should be after the base filter stage, i.e. one line lower. I made this change and got the expected results when varying method types.

pollytatouin avatar Mar 19 '19 19:03 pollytatouin