1

Guided by the answer to this post:

Linear Regression with a known fixed intercept in R

I have fit an explicit intercept value to some data, but also an explicit slope, thus:

intercept <- 0.22483 
fit <- lm(I(Response1 - intercept) ~ 0 + offset(-0.07115*Continuous))

Where Response1 is my dependent variable and Continuous is my explanatory variable, both from this dataset.

I want to plot an abline for my relationship. When only the intercept has been specified the post above recommends:

abline(intercept, coef(fit))

However I have no coefficients in the model "fit" as I specified them all. Is there a way to plot the abline for the relationship I specified?

Community
  • 1
  • 1
Sarah
  • 789
  • 3
  • 12
  • 29
  • What's the purpose of fitting a model where you constrain the slope and intercept? If you do that, then you are imposing a model, which you could plot easily since you already know the slope and intercept. – Thomas Jul 01 '13 at 15:54
  • Hi Thomas, i'm fitting an intercept and slope from a separate dataset to this dataset to see how well it fits the second dataset, though maybe this isn't the correct way to do this? Anyhow, I realise that yes, I can just specify the slope in abline(intercept, -0.07115) - stupid questionm, sorry! – Sarah Jul 01 '13 at 15:59
  • Regardless of the appropriateness of the model, if you know both the slope and intercept, I have to ask again, why you don't simply pass the slope to `abline` directly? – joran Jul 01 '13 at 16:01
  • Hi Joran - yes it was a silly question! I'm overcomplicating things. – Sarah Jul 01 '13 at 16:02
  • 2
    Just answer it yourself, then, so you can mark it as accepted. – joran Jul 01 '13 at 16:04
  • 1
    Since it's based on another dataset, you could pass them programmatically based the other dataset (i.e., build the first model, then plot the second data and the `abline` based on coefs from the first dataset). – Thomas Jul 01 '13 at 16:11
  • Thanks Thomas, that's what i'm doing, i just stupidly overlooked how simple it was to fit the abline :-) – Sarah Jul 01 '13 at 17:05

2 Answers2

4

Simple solution that I overlooked. I know the slope and the intercept so I can just pass them to abline directly:

abline(0.22483, -0.07115)
Sarah
  • 789
  • 3
  • 12
  • 29
0

Based on your comment, you can do this programmatically, without having to manually put in values. Here's an example with sample data from two similar dataframes:

df1 <- data.frame(Response1=rnorm(100,0,1), Continuous=rnorm(100,0,1))
df2 <- data.frame(Response1=rnorm(100,0,1), Continuous=rnorm(100,0,1))
fit1 <- with(df1, lm(Response1 ~ Continuous))
with(df2, plot(Response1 ~ Continuous)) # plot df2 data
abline(coef(fit1)) # plot df1 model over it
Thomas
  • 43,637
  • 12
  • 109
  • 140