VIPSolutions logo ✨ VIPSolutions

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:36:52

Final Answer:

Rendered Raw Text
Did you like this answer?
Let's break down your problem and answer your questions step by step. --- ## **The Error Explained** Your error is: ``` Error in plot.default(...) : formal argument "type" matched by multiple actual arguments ``` This happens because you are calling: ```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") ``` **Explanation:** `BikeDemand` is a data frame with 7 columns (time + 6 stations). When you call `plot(BikeDemand, ...)`, the `plot()` function dispatches to `plot.data.frame()`. This function does *not* accept the `type` argument (which is for plotting lines in `plot.default`), leading to a conflict. --- ### **How to Fix** #### **Option 1: Plot Each Station Individually** If you want to plot time series of the 6 stations, you should plot each column (each station) **against time**. ```r # Suppose time is in the first column, stations in columns 2-7 matplot(BikeDemand[[1]], BikeDemand[,-1], type = "l", ylim = c(0, 35), col = 1:6, lty = 1, main = "Types of moving averages", sub = "Oct 3 2016--Oct 23 2016", xlab = "Hours", ylab = "Bike Usage/Demand") legend("topright", legend = colnames(BikeDemand)[-1], col = 1:6, lty = 1) ``` - `matplot()` is designed to plot multiple columns against a single x-axis. - `BikeDemand[[1]]` is your time column. - `BikeDemand[,-1]` are your 6 station columns. #### **Option 2: Plot a Single Station** If you want to plot, for example, station 1: ```r plot(BikeDemand[[1]], BikeDemand[[2]], type = "l", ylim = c(0, 35), main = "Station 1 Bike Demand", xlab = "Hours", ylab = "Bike Usage/Demand") ``` --- ## **Answering Your Questions** ### **1. Predict the Next 3 Hours of Each Station** You can use a simple moving average, ARIMA, or other time series models. For illustration, let's use the built-in `forecast` package: #### **Using ARIMA (auto.arima) for Each Station** ```r library(forecast) # Predict next 3 hours for each station preds <- list() for (i in 2:7) { station_ts <- ts(BikeDemand[[i]], frequency = 24) # hourly, daily seasonality fit <- auto.arima(station_ts) forecast_vals <- forecast(fit, h = 3) print(paste("Station", colnames(BikeDemand)[i], "next 3 hours:")) print(forecast_vals$mean) preds[[colnames(BikeDemand)[i]]] <- forecast_vals } ``` **Discussion:** After running this code, look at the printed `forecast_vals$mean` for each station. These are your predictions for the next 3 hours. --- ### **2. Aggregating Hourly Data into Daily Data, Predict Next 3 Days** Aggregate to daily: ```r # Remove time column, aggregate by day (24 hours per day) daily_data <- aggregate(BikeDemand[,-1], by = list(rep(1:(nrow(BikeDemand)/24), each=24)), FUN = sum) # Remove grouping column daily_data <- daily_data[,-1] # Now, daily_data has 20 rows (days) x 6 columns (stations) # Predict next 3 days for each station for (i in 1:6) { station_ts <- ts(daily_data[[i]], frequency = 7) # daily, weekly seasonality fit <- auto.arima(station_ts) forecast_vals <- forecast(fit, h = 3) print(paste("Station", colnames(daily_data)[i], "next 3 days:")) print(forecast_vals$mean) } ``` **Discussion:** The printed `forecast_vals$mean` for each station gives your 3-day forecasts. --- ## **Summary** - Your plotting error is due to using `plot()` on a data frame with the `type` argument; use `matplot()` for multiple series. - For forecasting, use `auto.arima()` from the `forecast` package for a quick approach, both for hourly and aggregated daily data. - Interpret the forecast output to discuss expected demands. --- **If you need more information on interpreting the forecasts or want to use your custom moving average function for prediction, let me know!**

📢 Instructions:

  • Ask anything relatd to above question only.
  • For new Question goto :

Study Help, Fast Answers

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.