I am trying to remove the trends by differencing this logarithmically transformed time series. It contains two columns about COVID-19 Cases in the United States: one column being the number of cases and the other column being the date [https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_time_series]

enter image description here

I tried differencing my logarithmically transformed time series as shown below; however, the lines look very odd. R gives me a warning message Warning message: In log(casesDifferenced) : NaNs produced, and I think this is due to the log transformation because when I try to plot my values without the log transformation, the plot is fine.

Could someone explain what is happening here? Does this second plot do an adequate job of removing the trends by taking the first difference?

casesDifferenced <- diff(dat$new.cases, differences=1)
newDate <- dat$date[2:length(dat$date)]
plot(as.Date(newDate,"%m.%d.%y"), log(casesDifferenced), type = "l",
     xlab = "Date", ylab = expression(log("Differenced Cases")),
     main = "Differenced Time Series with Log Transformation d = 1")

enter image description here


1 Answer 1


When you difference, you will quite often get zero or negative values, which the log function will complain about. Usually it is better to log first, then difference.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.