Pat8
Pat8

Reputation: 1036

Plotting linear regression line of a calculation

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

Upvotes: 1

Views: 436

Answers (2)

user10191355
user10191355

Reputation:

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

Upvotes: 1

dylanjm
dylanjm

Reputation: 2101

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)

Upvotes: 1

Related Questions