Skip to content

Add functionality for StatCan trend estimates #33

@mountainMath

Description

@mountainMath

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions