-2

I'm trying to plot two regression on the same plot. So where the first regression ends, the second should continue and since the second regression line has a different slope it produces a kink.

I'm new to ggplot and I'm getting the error: "Error: Aesthetics must either be length one, or the same length as the dataProblems:gdpless15"

Here is a link to the R file and the dataset needed for this. dataset and code

I'm essentially doing a ols regression on the variable gdp on hap. The variable gdp is subsetted into two parts < 15000 and > 15000. Then I want to plot the regression line of < 15000 and when it gets to >15000 it will plot the regression line from the second regression on the same plot.

I think I'm having most trouble with using ggplot2 to plot the two regressions together.

The graph I'm trying to reproduce

This is the version of my code I feel is closest to getting what I want.

library(foreign)
library(ggplot2)

   gdpless15 <- isspmacro$gdp[(isspmacro$gdp < 15000) ] <- subset(isspmacro,gdpless15)
gdpless15 <-gdp < 15000 
gdpmore15 <- gdp > 15000

combinedreg <- ggplot() + 
    geom_point(data=less15, aes(x=gdpless15, y=hap)) + 
    geom_smooth(data=less15, aes(x=gdpless, y=hap), fill="blue",colour="darkblue", size=1) + 
    geom_point(data=more15, aes(x=gdpmore15, y=hap)) + 
    geom_smooth(data=more15, fill="red",colour="red", size=1) 

combinedreg <- ggplot() + 
    geom_point(data=less15, aes(x=gdpless15, y=hap)) +   
    geom_smooth(data=less15, aes(x=gdpless, y=hap), fill="blue",colour="darkblue", size=1) + 
    geom_point(data=more15, aes(x=gdpmore15, y=hap))+ 
    geom_smooth(method=lm) + geom_point(shape=1)



plot(combinedreg)
jaylin
  • 3
  • 2
  • 1
    Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Dec 20 '15 at 21:52
  • Please provide problem in minimal reproducible form, i.e. so that anyone else can *easily* copy it from your post & paste it into their session & see the results. All library statements & inputs must be provided & if large they need to be cut down to the minimal size that will still illustrate problem. Post output of `dput(whatever)` to show input data reproducibly. For more info on how to pose a question see (1) http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example (2) http://stackoverflow.com/help/mcve (3) http://stackoverflow.com/help/how-to-ask – G. Grothendieck Dec 20 '15 at 21:53

1 Answers1

5

Note: It is much preferrable that you use an example or toy dataset to illustrate your problem so that it is reproducible.

What you are trying to do is known as broken-stick regression. You can do this easily by using one regression and not two.

# example data
library(faraway)
data(savings)

# create your broken stick regression, predict results
lhs <- function(x) ifelse(x < 35,35-x,0)
rhs <- function(x) ifelse(x < 35,0,x-35)
gb <- lm(sr ~ lhs(pop15) + rhs(pop15), savings)
savings$p <- predict(gb, savings)

# plot
library(ggplot2)
ggplot(savings, aes(x= pop15, y= sr) ) + geom_point() +
  geom_line(data= savings, aes(x= pop15, y=p), colour= "blue") +
  # marker of break
  geom_segment(aes(x=35, y= 5, xend= 35, yend= 15), colour= "red", linetype= "dashed")

enter image description here

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59
  • Thanks. I will look into broken stick regression. I'm a bit confused on
    lhs <- function(x) ifelse(x < 35,35-x,0) rhs <- function(x) ifelse(x < 35,0,x-35) I assume for lhs you want the data to be everything < 35 but what is the 35-x and x-35 in rhs for?
    – jaylin Dec 20 '15 at 23:54
  • @jaylin if I am not mistaken, the regression lines are different depending on whether the value is lesser than or greater than the break point, so he's using those to determine that. – ytk Dec 21 '15 at 08:00
  • @jaylin Consider the limit from both sides as `lhs/rhs(pop15)` approaches the breakpoint (35). Both need to equal the same value for the regression lines actually meet at the breakpoint. – alexwhitworth Dec 21 '15 at 17:00
  • After looking at it some more it makes sense. You helped me more than I realized at first. after looking at the faraway package I realize contains micro data on happiness that I needed for my project. Thank you so much. – jaylin Dec 22 '15 at 00:03