0

I am working on some data that has a discontinuity at the 0.5 mark of the independent variable on my x-axis. To show this, I have applied a regression line above and below the 0.5 threshold:

clip(0,0.49999,0,100)
abline(lm(score ~ demvoteshare, data = subset(Lee, demvoteshare <= 0.5)), col="red")

clip(0.5,1,0,100)
abline(lm(score ~ demvoteshare, data = subset(Lee, demvoteshare > 0.5)), col="green")

I want to add a LOWESS line as well, and have managed that for the entire scatter plot:

lines(lowess(Lee$demvoteshare, Lee$score, f = 2/3, delta = 0.01*1), col="red")

Is it possible to make this split in two, as with the regression lines? Or would that be against the purpose of the LOWESS line?

(Sorry for not having any reproducible data, and I do not know how to upload data sets here)

DespeRate
  • 25
  • 4
  • Are you asking *how* to do something or *if* you should do something? Only the former is appropriate here. The latter should be asked at a place like [stats.se]. And see [how to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on adding data to your questions. – MrFlick Mar 07 '18 at 17:07
  • @MrFlick Thank you! In my case I would like to know HOW, then. Would it be possible to split the LOWESS line? I believe this would be a decent example of discontinued data: t <- 1:100 std <- 0.01 x <- rnorm(n = length(t) - 1, sd = sqrt(std)) x <- c(0, cumsum(x)) #add in some discontinuities x[25:35] <- x[25:35] + 0.2 x[45:55] <- x[45:55] - 0.5 plot(x,t) – DespeRate Mar 07 '18 at 18:18

1 Answers1

0

You can just fit the lowess line on different subsets of your data. With your sample data

set.seed(15)
t <- 1:100 
std <- 0.01 
dd <- data.frame(x = c(0, cumsum(rnorm(n = length(t) - 1, sd = sqrt(std)))
  x=1:100)
dd$x[25:35] <- dd$x[25:35] + 0.2 
dd$x[45:55] <- dd$x[45:55] - 0.5 

you can subset subset then run the lowess

plot(t~x, data=dd)    
with(subset(dd, x>.5), lines(lowess(x, t, f = 2/3, delta = 0.01*1), col="green"))
with(subset(dd, x<=.5), lines(lowess(x, t, f = 2/3, delta = 0.01*1), col="red"))

You should get a plot that looks something like this

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295