1

I am trying to plot a linear regression line for the following problem. If the first column is the number of dogs staying in one room, and the second column represents the amount of food each dog can grab, what are the estimated amounts of food each dog can grab when there are 10 dogs and 15 dogs, respectively, in the room? I need to write a function to compute the estimated values y, a vector, for a given x, a vector. Draw the actual values with type “o” points and the estimated values with type “+” points. You also need to draw the regression line.)

Hint use below:

lmout <- lm (y ~ x)

intercept <- lmout[1]$coefficients[[1]]
constant <- lmout[1]$coefficients[[2]]

I don't know what I need to calculate based on the question. I don't understand what is wanted if the given matrix looks like below:

  Number of dogs in a room Amount of food each dog can grab
1                        8                               12
2                       20                               15
3                       10                                2

The question asks to calculate what are the estimated amounts of food each dog can grab when there are 10 and 15 dogs, respectively in each room? What I have so far is plotting the values of the matrix and regression line.

rownames = c("1","2","3") #Declaring row names
colnames = c("Number of dogs in a room", "Amount of food each dog can grab") #Declaring column names

v <- matrix(c(8,12,20,15,10,2), nrow = 3, byrow=TRUE, dimnames = list(rownames,colnames))

print(v) # Prints the matrix of the data

# Data in vector form
x <- c(8,20,10)
y <- c(12,15,2)

# calculate linear model
lmout <- lm (y ~ x)
# plot the data
plot(x,y, pch =19)
# plot linear regression line
abline(lmout, lty="solid", col="royalblue")

# Function
func <- function(lmout,x){

  intercept <- lmout[1]$coefficients[[1]]
  constant <- lmout[1]$coefficients[[2]]

  regline2 <- lm(intercept, constant) 
  abline(regline2, lty="solid", col="red")

}

print(func(lmout,x))
1
  • 1
    Maybe you'd like to try taking a look at str(lmout), which shows you the structure of lm output, and which probably already contains some interesting values for you. Perhaps lmout$fitted.values is of special interest for you? Commented Jun 28, 2019 at 20:17

2 Answers 2

1

It sounds like you want the predicted values of food for 10 and 15 dogs per room. You can do that with predict. First I'll turn the matrix into a dataframe to make things a little easier:

# Turn you matrix into a dataframe.
df <- data.frame(dogs = v[,1], food = v[,2])

I can then compute my model and predictions based on the model:

# Compute the linear model.
lmout <- lm(food ~ dogs, df)

# Create a dataframe with new values of `dogs`.
df_new <- data.frame(dogs = c(10, 15))

# Use `predict` with your model and the new data.
df_new$food <- predict(lmout, newdata = df_new)

#### PREDICTIONS OUTPUT ####

  dogs      food
1   10  8.096774
2   15 11.040323

Now I can plot the data and new data with the regression line.

plot(df$dogs, df$food, pch = 21)
abline(lmout, lty="solid", col="royalblue")
points(df_new$dogs, df_new$food, pch = 3, col = "red")

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

@jay.sf thanks for the link. I'm not sure if telling somone how to use predict is problematic. Pretty much every tutorial does the same thing. But I definitely understand that I should be more careful.
1

Since this sounds like homework I'll show you how to do this just using the built in functions in R. You'll have to build your own functions to do this dynamically. If you're teacher wants you to do it from scratch, remember:

yhat = beta0 + beta1 * x # No LaTeX Support here?

dog_dat <- data.frame("dogs_room" = c(8, 20, 10), "food" = c(12, 15, 2))
dog.lm <- lm(dogs_room ~ food, data = dog_dat)

plot(dog_dat$food, dog_dat$dogs_room)
points(dog_dat$food, fitted.values(dog.lm), col = "red")
abline(dog.lm)

Created on 2019-06-28 by the reprex package (v0.2.1)

4 Comments

I don't understand what calculation needs to be in the function? yhat = ?
@888 It's your coefficients from your linear model. beta0 = (Intercept) and beta1 = x
Then what is x in yhat= intercept + x * ? is it yhat = intercept + x * constant ?
@888 sorry, I could see how that would be confusing. both the betas are the constant coefficients of your model. x is your data in this case "dogs in a room". yhat is the estimate of food.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.