-1

I want to use curve to make lines for the models below to show each model’s mean predicted WEIGHT across values of AVFOOD. However, I don't really understand the curve function and how to code them.

m0.f <- lm(WEIGHT ~ 1, data = foxes)
m1.f <- lm(WEIGHT ~ AVFOOD, data = foxes)
m2.f <- lm(WEIGHT ~ AVFOOD + GSIZE, data = foxes)
m3.f <- lm(WEIGHT ~ AVFOOD + GSIZE + AREA, data = foxes)

1 Answers1

0

Obviously, we don't have your data, so I have created a set with the same names as your own to demonstrate (see below).

I don't think curve is well suited to this problem. For a start, the regression lines will all be straight here, so abline would be better for the first two models. (It won't work for the last two models because it won't know how to handle the extra regressors).

If you want to use curve for the last two models, you need to create functions that output values for WEIGHT given AVFOOD (probably at the mean of the other regressors)

m0.f <- lm(WEIGHT ~ 1, data = foxes)
m1.f <- lm(WEIGHT ~ AVFOOD, data = foxes)
m2.f <- lm(WEIGHT ~ AVFOOD + GSIZE, data = foxes)
m3.f <- lm(WEIGHT ~ AVFOOD + GSIZE + AREA, data = foxes)

f2 <- function(x) predict(m2.f, newdata = data.frame(AVFOOD = x, 
                                                     GSIZE = mean(foxes$GSIZE)))
f3 <- function(x) predict(m3.f, newdata = data.frame(AVFOOD = x, 
                                                     GSIZE = mean(foxes$GSIZE),
                                                     AREA = mean(foxes$AREA)))

Then your plot might be something like:

plot(foxes$AVFOOD, foxes$WEIGHT)
abline(m0.f)
abline(m1.f, col = "red")
curve(f2, add = TRUE, col = "blue")
curve(f3, add = TRUE, col = "green")

enter image description here


Made up data

set.seed(1)

foxes <- data.frame(AVFOOD = rnorm(100, 100, 10),
                    GSIZE = runif(100, 5, 10),
                    AREA = sqrt(rnorm(100, 500, 50)))

foxes$WEIGHT <- with(foxes, 0.1* AVFOOD + GSIZE - 0.1* AREA + rnorm(100, 0, 5))

head(foxes)
#>      AVFOOD    GSIZE     AREA   WEIGHT
#> 1  93.73546 6.337541 22.85846 14.10635
#> 2 101.83643 6.093226 22.33992 16.07872
#> 3  91.64371 7.583984 22.00219 14.19986
#> 4 115.95281 6.344753 21.29629 14.57208
#> 5 103.29508 5.905842 20.63073 17.65003
#> 6  91.79532 7.592881 21.12440 20.39111

Created on 2022-10-21 with reprex v2.0.2

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87