# Regression line Scatterplot R (Time Series)

I am trying to do an interrupted time series analysis, while my knowledge about this is quite limited, though I dig deeper and deeper every day.

I am interested if the price of a certain wine is influenced by a treatment especially on a short-term basis (level).

Example from a dataset called Pavie:

Lot closed        price Treatment Time since treatment
54 2014-11-20 00:00:00  268.         0                      0
55 2015-03-21 00:00:00  309.         0                      0
56 2015-03-28 00:00:00  247          0                      0
57 2015-06-18 00:00:00  247          0                      0
58 2015-06-18 00:00:00  247          0                      0
59 2015-06-18 00:00:00  247          0                      0
60 2015-09-19 00:00:00  334.         1                      1
61 2015-10-17 00:00:00  288.         1                      2
62 2015-10-17 00:00:00  309.         1                      3
63 2015-12-16 00:00:00  309.         1                      4
64 2016-03-19 00:00:00  329.         1                      5
65 2016-06-04 00:00:00  412.         1                      6


Using the whole data set to produce a scatterplot, implement a vertical line to show the treatment and add a regression line.

plot(Pavie$$Lot closed, Pavie$$price)

abline(v=as.numeric(Pavie$$Lot closed[59]), lwd=2, col='red') tsPavie <- lm( Pavie$$price ~ Pavie$$Lot closed + Pavie$$Treatment + Pavie$Time since treatment, data=Pavie ) lines( Pavie$$Lot closed, tsPavie$$fitted.values, col="steelblue", lwd=2 )  This gives me the following plot: 2 questions: Why is the pre-treatment regression line straight, and post-intervention isn't? Where is my mistake (either in coding or thinking)? The treatment happened on 2015-06-29, how do I code such a line, that the treatment is exactly specified (at this day, there is no price given)? ## 1 Answer Before the treatment, both of the other variables besides the date are constant (0), so of course the model output will also be constant if it doesn't recognise the Date as relevant. A simple fix would be to add a new variable 'Auctions since start' commencing at 0 and growing by 1 every auction and using that instead of the date. There might be another problem with your model, which is the fact that your variable 'Time since treatment' is actually 'Auctions since treatment'. I'm not sure that's what you want. I've tried to reproduce your example here: N = 80 Pavie = data.frame(lot_closed = sort(sample(seq(as.Date('2009/01/01'), as.Date('2020/12/31'), by="day"), N))) treatment = as.Date('2015-06-29') n_before = sum(Pavie$lot_closed < treatment)
n_after = N - n_before

#dummy price
Pavie$price = c(rnorm(n_before, mean = 275, sd = 30), 275 + cumsum(rnorm(n=n_after, mean=5, sd=30))) #create model variables Pavie$$x = 1:N Pavie$$treatment = c(rep(0,n_before), rep(1, n_after)) Pavie$$months_since_treat = pmax(0, (Pavie$$lot_closed - treatment)/30) #fit model tsPavie <- lm(Pavie$$price ~ Pavie$$x + Pavie$$months_since_treat + Pavie$$treatment, data=Pavie) #plot plot(Pavie$$lot_closed, Pavie$$price) abline(v=treatment, lwd=2, col='red') lines(Pavie$$lot_closed, tsPavie$$fitted.values, col="steelblue", lwd=2) tsPavie  This gives me a plot just like yours: The coefficients can help you have an idea on the impact of the treatment: Coefficients: (Intercept) Pavie$$x Pavie$$months_since_treat 267.7564 0.1911 2.8658 Pavie$treatment
-7.1927