Using Zillow Housing Data, I am trying to replicate an R code which is available here. I have carefully followed the R code but got an error when trying to fit the prophet model. The error is "model <- prophet(train) Error : Exception: variable does not exist; processing stage=data initialization; variable name=t; base type=double (in 'string', line 75, column 2 to column 14) Error in if (m$stan.fit$return_code != 0) { : argument is of length zero"
Here is my code:
library(prophet)
library(tidyverse)
# Data preprocessing
# Assuming 'median_value' is your target variable
# Make sure to adjust the column names based on your data
REIT_1997$ZipCode <- as.factor(REIT_1997$ZipCode)
# Extract relevant columns for time series regression
# Assuming columns from '1997-01-31' to '2023-09-30' are your monthly median values
ts_data <- REIT_1997 %>%
select(ZipCode, starts_with("1997"), starts_with("1998"), starts_with("1999"), starts_with("2000"), starts_with("2001"),
starts_with("2002"), starts_with("2003"), starts_with("2004"), starts_with("2005"), starts_with("2006"),
starts_with("2007"), starts_with("2008"), starts_with("2009"), starts_with("2010"), starts_with("2011"),
starts_with("2012"), starts_with("2013"), starts_with("2014"), starts_with("2015"), starts_with("2016"),
starts_with("2017"), starts_with("2018"), starts_with("2019"), starts_with("2020"), starts_with("2021"),
starts_with("2022"), starts_with("2023")) %>%
pivot_longer(cols = -ZipCode, names_to = "Date", values_to = "MedianValue")
# mutate(Date= as.Date(Date_, "%Y.%m.%d"))
# Check for NAs in ts_data
if (any(is.na(ts_data))) {
print("ts_data contains NAs.")
} else {
print("ts_data does not contain NAs.")
}
# Remove NAs
ts_data <- ts_data %>%
drop_na()
# Check for NAs in ts_data
if (any(is.na(ts_data))) {
print("ts_data contains NAs.")
} else {
print("ts_data does not contain NAs.")
}
# Time series split
set.seed(123)
ts_data$Date <- as.Date(ts_data$Date)
str(ts_data)
head(ts_data)
prophet_data <- ts_data %>%
select(ds = Date, y = MedianValue) # Adjust column names based on your data
train <- prophet_data %>% filter(ds<ymd("2020-01-01"))
test <- prophet_data %>% filter(ds>=ymd("2020-01-01"))
# Fitting the Prophet Model
model <- prophet(train)
# Making Future Dataframe
future <- make_future_dataframe(model, periods = 365) # Adjust the number of periods as needed
# Generating Forecast
forecast <- predict(model, future)