Search code examples
rtime-seriestrendline

Two trendlines for the same series


Hello I have an annual series with the gdp per capita from 1950 to 2017, and I want to plot two trendlines for different periods on the same scatterplot to show the change in the Balance Growth Path, one trendline will be for the year 1950 to 1977 and the other one from 1995 to 2016. Is this possible?

gdp_mex_ts<-ts(data$log_pib_mex, start = 1950, end =2017 )
trend<-lm(data$log_pib_mex~data$ï..Year)
plot(gdp_mex_ts,type= "o")
abline(trend)

I want to add the two different trendlines(1950 to 1977 & 1995 to 2016) on that scatterplot to compare them.

here is an example of the data:

head(data)

 ï..Year log_pib_us log_pib_mex
1    1950   10.49388    9.699450
2    1951   10.52621    9.766078
3    1952   10.55047    9.802166
4    1953   10.58458    9.764262
5    1954   10.59741    9.858383
6    1955   10.64326    9.926464

Solution

  • I solved it using the next code:

    gdp_mex_ts<-ts(data$log_pib_mex, start = 1950, end =2017 )
    df1<-subset(data, date <= 1977)
    trend1<-lm(df1$log_pib_mex~df1$date)
    df2<-subset(data, date>=1995)
    trend2<-lm(df2$log_pib_mex~df2$date)
    
    plot(gdp_mex_ts,type= "o", xaxt = "n")
    axis(1, at = seq(1950, 2017, by = 5), las=2)
    abline(trend1, col="red")
    abline(trend2, col="blue")
    

    enter image description here