0

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
Quinoba
  • 17
  • 4
  • Please provide a minimal reproducible example. – Christoph Aug 24 '20 at 17:51
  • Yes, it's possible. What isn't really possible is showing you _how_ to do it, unless we know what format your data is in, what units the times are in, what packages you are using etc. – Allan Cameron Aug 24 '20 at 17:56
  • I am really new to R so I just plotted the trendline from 1950 to 2017 – Quinoba Aug 24 '20 at 18:20
  • In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). More info can be found at Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Aug 24 '20 at 18:40

1 Answers1

0

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

Quinoba
  • 17
  • 4