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


str(lmout), which shows you the structure oflmoutput, and which probably already contains some interesting values for you. Perhapslmout$fitted.valuesis of special interest for you?