I am trying to plot a time series model using R. So far I have the following code, but I'm getting an error located at the very bottom. Can you please explain what I'm doing wrong, and what I should be doing instead in order to avoid the error? As far as the data is concerned, the first column displays time (hour-by-hour) across 20 days, giving 504 rows of data and the six columns thereafter display the bike demand/usage at each individual hour for six different bike stations. I am looking to answer the following two questions:
1.) Predict the next 3 hours of the bike stations' demands. Discuss the predicted results.
2.) Aggregating your hourly data into daily data, choose the same 6 stations to predict their next three day’s bike demands. Discuss the predicted results.
Here is my code so far (with the error):
> movavg
Question:
I am trying to plot a time series model using R. So far I have the following code, but I'm getting an error located at the very bottom. Can you please explain what I'm doing wrong, and what I should be doing instead in order to avoid the error? As far as the data is concerned, the first column displays time (hour-by-hour) across 20 days, giving 504 rows of data and the six columns thereafter display the bike demand/usage at each individual hour for six different bike stations. I am looking to answer the following two questions:
1.) Predict the next 3 hours of the bike stations' demands. Discuss the predicted results.
2.) Aggregating your hourly data into daily data, choose the same 6 stations to predict their next three day’s bike demands. Discuss the predicted results.
Here is my code so far (with the error):
> movavg
This is AI generated Response.
Asked by: Umesh
Created at: 2025-08-01 20:45:06
Final Answer:
RenderedRaw Text
Did you like this answer?
Great job providing code and context! Let's break down your **error** and how to address it, and then I'll give some guidance for your **two questions**.
---
## 1. The Error Explained
Your error message is:
```
Error in plot.default(...) :
formal argument "type" matched by multiple actual arguments
```
This usually happens when you give the `plot()` function an argument (like `type`) that conflicts with how R interprets your data input. Here's your command:
```r
plot(BikeDemand, type = "l", col = 1, ylim = c(0, 35), main = "Types of moving averages", sub = "Oct 3 2016--Oct 23 2016", xlab = "Hours", ylab = "Bike Usage/Demand")
```
**Problem:**
`BikeDemand` is a data frame (not a single vector), and when you call `plot()` on a data frame, R tries to guess what you want. It might try to plot a matrix of scatterplots or, in some cases, treat it as a formula. The `type` argument is *not* accepted in all these call signatures, causing the error.
---
## 2. Solution: How to Plot Time Series for Each Station
### **Option 1: Plot each station individually**
Suppose your columns are: `Time, Station1, Station2, ..., Station6`.
```r
# First column is time, columns 2-7 are stations
time <- BikeDemand[[1]]
# Plot Station 1
plot(time, BikeDemand[[2]], type="l", col=2, ylim=c(0, 35),
main="Bike Demand at Station 1",
xlab="Hour", ylab="Bike Usage/Demand")
```
### **Option 2: Plot all stations in one plot**
```r
matplot(time, as.matrix(BikeDemand[,-1]), type="l", lty=1, col=1:6,
ylim=c(0, 35),
main="Bike Demand at 6 Stations",
xlab="Hour", ylab="Bike Usage/Demand")
legend("topright", legend=names(BikeDemand)[-1], col=1:6, lty=1)
```
---
## 3. Answering Your Two Questions
### **1. Predict next 3 hours of bike station demands**
You could use a simple moving average (your `movavg` function) or a more sophisticated time series model (like ARIMA from the `forecast` package).
#### **Using your moving average function:**
```r
n <- 3 # or whatever window you want
predictions <- sapply(BikeDemand[,-1], function(x) {
y <- movavg(x, n, type = "s")
# Simple way to predict: repeat last moving average value for next 3 hours
rep(y[length(y)], 3)
})
# predictions is now a 6x3 matrix: 3 predicted values for each station
```
#### **Discussion:**
With the moving average, you're assuming the immediate past trend continues. For more sophisticated modeling, use `auto.arima()` from the `forecast` package.
### **2. Aggregate hourly data into daily data, predict the next 3 days**
#### **Aggregate:**
```r
library(dplyr)
BikeDemand$Day <- as.integer((seq_len(nrow(BikeDemand)) - 1) / 24) + 1
daily_demand <- BikeDemand %>%
group_by(Day) %>%
summarise(across(-c(Time, Day), sum)) # Sum demand per day per station
```
#### **Predict next 3 days (using moving average):**
```r
daily_pred <- sapply(daily_demand[,-1], function(x) {
y <- movavg(x, n=3, type="s")
rep(y[length(y)], 3)
})
```
#### **Discussion:**
Aggregating smooths out hourly fluctuations. If trends are stable, predictions may be decent; if there's seasonality or strong trends, moving average may not predict well.
---
## **Summary of Your Issue**
- **Plot error**: Caused by using `plot()` directly on a data frame with `type="l"`.
- **Solution**: Use `matplot()` for multiple columns, or plot individual columns.
- **Prediction**: Use your moving average function for a quick forecast, or use more advanced time series models for better accuracy.
- **Aggregation**: Use `dplyr` to sum hourly to daily data.
---
**If you need more detail on forecasting (e.g., using `forecast::auto.arima()`), just ask!**
VIPSolutions focuses on clear, step‑by‑step explanations so you can learn quickly. Whether you need an AI‑generated walkthrough or a short hint to get unstuck, each solution is organized for fast reading and easy review later.
Search similar questions, compare approaches, and bookmark the best answers for revision. Our goal is simple: quick, reliable study help that feels natural—not noisy.