Esinede1
Esinede1

Reputation: 25

How draw a loess line in ts plot

I tried hours to figure out how I can make my loess line work. The problem is I do not know much (lets say near nothing). I only have to use R for one course in university. I created a fake table the real table is for download here

I have to make a timeline plot that worked surprisingly well. But now I have to add two loess lines with different spans. My Problem is I don't know how the command really works. I mean I know it should be something like loess(..~.., data=..). The step where I'm stuck is marked with "WHAT BELONGS HERE" in the given code below.

table <- structure(list(
  Months = c("1980-06", "1980-07", "1980-08", "1980-09", 
             "1980-10", "1980-11", "1980-12", "1981-01"), 
  Total = c(75000, 70000, 60000, 73000, 72000, 71000, 76000, 71000)),
  .Names = c("Monts", "Total of Killed Pigs"), 
  row.names = c(NA, 4L), class = "data.frame")

ts.obj <- ts(table$`Total of Killed Pigs`, start = c(1980, 1), frequency = 2)

plot(ts.obj)

trend1 <- loess(# **WHAT BELONGS HERE?**, data = table, span =1)

predict1 <- predict(trend1)

lines(predict1, col ="blue")

That is my original code:

obj <- read.csv(file="PATH/monthly-total-number-of-pigs-sla.csv", header=TRUE, sep=",")

ts.obj <- ts(obj$Monthly.total.number.of.pigs.slaughtered.in.Victoria..Jan.1980...August.1995, start = c(1980, 1), frequency = 12)

plot(ts.obj)

trend1 <- loess (WHAT BELONGS HERE?, data = obj, span =1)

predict1 <- predict (trend1) lines(predict1, col="blue")

Upvotes: 0

Views: 412

Answers (2)

AkselA
AkselA

Reputation: 8856

We can do away with the data argument as the time series is univariate (just one variable).

The formula ts.obj ~ index(ts.obj) can be read as

value as a function of time

as ts.obj will give you the values, and index(ts.obj) will give you the time index for those values, and the tilde ~ specifies that the first is a function of, or dependent on, the other.

library(zoo) # for index()

plot(ts.obj)

trend1 <- loess(ts.obj ~ index(ts.obj), span=1)
trend2 <- loess(ts.obj ~ index(ts.obj), span=2)
trend3 <- loess(ts.obj ~ index(ts.obj), span=3)

pred <- sapply(list(trend1, trend2, trend3), predict)

matlines(index(ts.obj), pred, lty=1, col=c("blue", "red", "orange"))

zoo isn't strictly required. If you replace index(ts.obj) with as.numeric(time(ts.obj)) you should be fine, I think.

enter image description here

Upvotes: 1

SCDCE
SCDCE

Reputation: 1643

In case you were wanting to go with ggplot2:

 library(ggplot2)
 library(dplyr)

 table <- structure(list(
   Months = c("1980-06", "1980-07", "1980-08", "1980-09", 
              "1980-10", "1980-11", "1980-12", "1981-01"), 
   Total = c(75000, 70000, 60000, 73000, 72000, 71000, 76000, 71000)),
   .Names = c("Months", "Total"), 
   row.names = c(NA, 8L), class = "data.frame")

Change to proper dates:

 table <- table %>% mutate(Months = as.Date(paste0(Months,"-01")))

Plot:

 ggplot(table, aes(x=Months, y=Total)) + 
   geom_line() +
   geom_smooth(span=1, se= FALSE, color ="red") +
   geom_smooth(span=2, se= FALSE, color ="green") +
   geom_smooth(span=3, se= FALSE) +
   theme_minimal()

enter image description here

Upvotes: 0

Related Questions