Question

I'm having trouble plotting a quadratic model in the program R. This is my code: >...

I'm having trouble plotting a quadratic model in the program R. This is my code:

> sales <- c(3.9, 4.3, 5.4, 6.4, 8.1)
> month <- 1:5
> 
> plot(month, sales)
> 
> linear.model <- lm(sales ~ month)
> # summary(linear.model)
> abline(linear.model)
> 
> month2 <- month^2
> quadratic.model <- lm(sales ~ month + month2)
> # summary(quadratic.model)
> 
> predictedmonths <- 6:12
> predictedsales <- predict(quadratic.model, list(month = predictedmonths, month2 = predictedmonths^2))
> plot(month, predictedsales)
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

For reference, this is the question I'm trying answer:

The owner of a rapidly expanding business finds that for the first five months of the year, the sales
are $3.9, $4.3, $5.4, $6.4, and $8.1 (thousands). After plotting these figures, the owner speculates
that they follow a quadratic function. Find the least-squares quadratic polynomial fit to these data
and use it to project sales for the remaining seven months of the year.

Homework Answers

Answer #1

> sales <- c(3.9, 4.3, 5.4, 6.4, 8.1)
> month <- 1:5
>
> plot(month, sales)
>
> linear.model <- lm(sales ~ month)
> # summary(linear.model)
> abline(linear.model)
>
> month2 <- month^2
> quadratic.model <- lm(sales ~ month + month2)
> # summary(quadratic.model)
>
> predictedmonths <- 6:12
> predictedsales <- predict(quadratic.model, list(month = predictedmonths, month2 = predictedmonths^2))
>
> ## Plot of projected sales for the remaining seven months of the year
> plot(predictedmonths, predictedsales)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT