1
votes

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?

2
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
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
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
Just answer it yourself, then, so you can mark it as accepted.joran
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

2 Answers

4
votes

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)
0
votes

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