I'm working on an unassessed homework problem from unpublished course notes of a statistics module from a second year university mathematics course.

I'm trying to plot a 2-parameter full linear model and a 1-parameter reduced linear model for the same data set. I can't figure out how to plot the 1-parameter model; all attempts so far have either given errors or a non-constant slope.

xs <- c(0,1,2)
ys <- c(0,1,1)
Data <- data.frame(x = xs, y = ys)
mf <- lm(y ~ x, data = Data) # model_full
mr <- lm(y = mean(y), data = Data) # model_reduced; this is the bit I can't get to work
plot(x, y, xlab="x", ylab="y")
abline(mf$coefficients[1], mf$coefficients[2])
abline(mr$coefficients[1], mr$coefficients[2])

To use only a single parameter, which will be a constant, you have to specify the lm formula as follows: y ~ 1. R will then fit the model, where the coefficient for this constant will be equal to the mean of y, see also this stats stackexchange answer.

  • $\begingroup$ That returns Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : 'a' and 'b' must be finite. $\endgroup$ – mjc Mar 30 at 17:15
  • 1
    $\begingroup$ That is simply caused by the fact that there is no second coefficient (mr$coefficients[2]). To draw the correct line you have to use abline(mr$coefficients[1], 0) since the first value (a) is the intercept, which is equal to the coefficient of the model, the second value (b) is equal to zero since no second parameter is fit. $\endgroup$ – Oxbowerce Mar 30 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.