I want to see how a model performs when I make the variable 'year' piecewise linear. I know there are automatic methods to define this within the model and to look for the best cut-point. Still, I prefer making a piecewise variable as it is more transparant to me and in addition, I think the solution to this problem can help on other occasions as well.
So I want to make variables defined like
year1997up<-0
year1997up[year>1997]<-year[year>1997]-1997
year1997up[year<=1997]<-rep(0,sum(year<=1997))
year1997down<-0
year1997down[year<1997]<-year[year<1997]-1995
year1997down[year>=1997]<-rep(2,sum(year>=1997))
So that year is piecewise divided with cut-point 1997.
I want to do this for all years from 1997 till 2011 and to automate this process, I wrote a function:
piece.var.fun<-function(up,down,i,data){
within(data,{
up<-0
up[year>=i]<-year[year>=i]-i
up[year<i]<-rep(0,sum(year<i))
down<-0
down[year<=i]<-year[year<=i]-1995
down[year>i]<-rep(i-1995,sum(year>i))
})
}
test.dataset<-piece.var.fun(up="year2000up",down="year2000down",data=StartM,i=2000)
The idea was to use this function in combination with mapply on vectors containing the names I want, the variables are just called up and down instead of year2000up and year2000down. This way, I can't use it to make the variables for different years, as they are all named the same.
So, how can I use a function like this and make the name of the variables include the the year?