Error code() functions

My code has some errors.Anyone who can tell me what is wrong ?

My code:

# load needed packages
library(readr)
library(dplyr)
library(ggplot2)
library(forecast)
# import data
sales <- readxl::excel_format("C:/Users/jenny/Desktop/R Studio/MonthlySales1.xls")
# explore data 
str(sales)

cx <- c(0, cumsum(ifelse(is.na(x), 0, x)))
cn <- c(0, cumsum(ifelse(is.na(x), 0, 1)))
rx <- cx[(2+1):length(cx)] - cx[1:(length(cx) - 2)]
rn <- cn[(2+1):length(cx)] - cn[1:(length(cx) - 2)]
rsum <- rx / rn

head(sales, n = 5)
Classes‘tbl_df’,‘tbl’ and 'data.frame':	95 obs. of  2 variables:
  $ month: Date, format: "1982-06-12" "1982-06-19" ...
$ sales: num  14237 4520 55691 28295 23648 ...
- attr(*, "spec")=
  .. cols(
    ..   month = col_date(format = ""),
    ..   sales = col_double()
    .. )
options(repr.plot.width = 6, repr.plot.height = 3)
ggplot(sales, aes(x = month, y = sales)) + geom_line() + geom_smooth(method = 'lm') +labs(x = "Time", y = "Monthly Sales")
salesTS <- ts(sales$sales, frequency = 4, start = c(1982,1))
class(salesTS)
'ts'
options(repr.plot.width = 6, repr.plot.height = 5)
salesDecomp <- decompose(salesTS)
autoplot(salesDecomp)
# logging transform time series data

salesLog <- log(salesTS)

salesLogHW <- ets(salesLog)
salesLogHW
ets(x = salesLog)

sales1 <- sqrt(salesLogHW$dispersion)
dnorm(7, mean = coef(m1), sd = sdest)

salesLogHW <- HoltWinters(salesLog)
salesLogHW
HoltWinters(x = salesLog)

options(repr.plot.width = 6, repr.plot.height = 4)
autolayer(salesLogHW)
# forecast next year's sales
nextYearSales <- forecast(salesLogHW, h=4)
# plot
autolayer(nextYearSales)
nextYearSales



**errors**

[1] "xls"
> Classes‘tbl_df’,‘tbl’ and 'data.frame':	95 obs. of  2 variables:
**Error:** unexpected input in "Classes‘"
>   $ month: Date, format: "1982-06-12" "1982-06-19" ...
**Error:** unexpected '$' in "  $"
> $ sales: num  14237 4520 55691 28295 23648 ...
**Error:** unexpected '$' in "$"
> - attr(*, "spec")=
**Error:** unexpected '*' in "- attr(*"
>   .. cols(
**Error:** unexpected symbol in "  .. cols"
>     ..   month = col_date(format = ""),
**Error:** unexpected symbol in "    ..   month"
>     ..   sales = col_double()
**Error:** unexpected symbol in "    ..   sales"
>     .. )
**Error:** unexpected ')' in "    .. )"
> options(repr.plot.width = 6, repr.plot.height = 3)
> ggplot(sales, aes(x = month, y = sales)) + geom_line() + geom_smooth(method = 'lm') +labs(x = "Time", y = "Monthly Sales")
**Error:** `data` must be a data frame, or other object coercible by `fortify()`, not a character vector
Run `rlang::last_error()` to see where the error occurred.
> salesTS <- ts(sales$sales, frequency = 4, start = c(1982,1))
**Error** in sales$sales : $ operator is invalid for atomic vectors
> class(salesTS)
[1] "ts"
> 'ts'
[1] "ts"
> options(repr.plot.width = 6, repr.plot.height = 5)
> salesDecomp <- decompose(salesTS)
> autoplot(salesDecomp)
> # logging transform time series data
> 
> salesLog <- log(salesTS)
> 
> salesLogHW <- ets(salesLog)
> salesLogHW
ETS(M,Ad,N) 

Call:
 ets(y = salesLog) 

  Smoothing parameters:
    alpha = 0.1434 
    beta  = 1e-04 
    phi   = 0.8008 

  Initial states:
    l = 1.4663 
    b = 0.7245 

  sigma:  0.3113

     AIC     AICc      BIC 
166.4996 169.0450 176.6328 
> ets(x = salesLog)
**Error in clas**s(y) %in% c("data.frame", "list", "matrix", "mts") : 
  argument "y" is missing, with no default
> 
> sales1 <- sqrt(salesLogHW$dispersion)
**Error in sqrt**(salesLogHW$dispersion) : 
  non-numeric argument to mathematical function
> dnorm(7, mean = coef(m1), sd = sdest)
**Error in coef**(m1) : object 'm1' not found
> 
> salesLogHW <- HoltWinters(salesLog)
> salesLogHW
Holt-Winters exponential smoothing with trend and additive seasonal component.

Call:
HoltWinters(x = salesLog)

Smoothing parameters:
 alpha: 0.3636345
 beta : 0.2096302
 gamma: 0.4300818

Coefficients:
         [,1]
a   3.8519386
b  -0.1149221
s1 -0.6216912
s2 -0.3063579
s3 -1.0720958
s4 -0.3704739
> HoltWinters(x = salesLog)
Holt-Winters exponential smoothing with trend and additive seasonal component.

Call:
HoltWinters(x = salesLog)

Smoothing parameters:
 alpha: 0.3636345
 beta : 0.2096302
 gamma: 0.4300818

Coefficients:
         [,1]
a   3.8519386
b  -0.1149221
s1 -0.6216912
s2 -0.3063579
s3 -1.0720958
s4 -0.3704739
> 
> options(repr.plot.width = 6, repr.plot.height = 4)
> autolayer(salesLogHW)
**Error:** Objects of type HoltWinters not supported by autolayer.
Run `rlang::last_error()` to see where the error occurred.
> # forecast next year's sales
> nextYearSales <- forecast(salesLogHW, h=4)
> # plot
> autolayer(nextYearSales)
mapping: x = ~x, y = ~y, level = ~level, ymin = ~ymin, ymax = ~ymax 
geom_forecast: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 
> nextYearSales
        Point Forecast     Lo 80    Hi 80      Lo 95
1992 Q1       3.115325 1.2080498 5.022601  0.1983992
1992 Q2       3.315737 1.2321049 5.399368  0.1290970
1992 Q3       2.435076 0.1306404 4.739513 -1.0892543
1992 Q4       3.021776 0.4553214 5.588231 -0.9032777
           Hi 95
1992 Q1 6.032251
1992 Q2 6.502376
1992 Q3 5.959407
1992 Q4 6.946830

> 

tibble::tribble(
        ~Month, ~sales,
  "2019-01-02",    56L,
  "2019-01-05",     1L,
  "2019-01-09",    66L,
  "2019-01-12",    12L,
  "2019-01-16",    15L,
  "2019-01-19",    45L,
  "2019-01-23",     8L,
  "2019-01-26",    56L,
  "2019-01-30",    23L,
  "2019-02-02",    58L,
  "2019-02-06",    98L,
  "2019-02-09",     1L,
  "2019-02-13",     5L,
  "2019-02-16",    45L,
  "2019-02-20",    21L,
  "2019-02-23",    44L,
  "2019-02-27",     4L,
  "2019-03-02",     4L,
  "2019-03-06",    78L,
  "2019-03-09",    22L,
  "2019-03-13",     3L,
  "2019-03-16",     6L,
  "2019-03-20",     8L,
  "2019-03-23",    45L,
  "2019-03-27",    16L,
  "2019-03-30",    45L,
  "2019-04-03",    12L,
  "2019-04-06",    44L,
  "2019-04-10",    10L,
  "2019-04-13",     4L,
  "2019-04-17",    78L,
  "2019-04-20",    11L,
  "2019-04-24",     3L,
  "2019-04-27",     6L,
  "2019-05-01",     8L,
  "2019-05-04",    45L,
  "2019-05-08",    18L,
  "2019-05-11",     3L,
  "2019-05-15",     5L,
  "2019-05-18",     7L,
  "2019-05-22",    13L,
  "2019-05-25",    12L,
  "2019-05-29",     4L,
  "2019-06-01",    16L,
  "2019-06-05",     3L,
  "2019-06-08",     6L,
  "2019-06-12",     8L,
  "2019-06-15",    45L,
  "2019-06-19",    21L,
  "2019-06-22",    15L,
  "2019-06-26",     4L,
  "2019-06-29",     2L,
  "2019-07-03",    13L,
  "2019-07-06",    10L,
  "2019-07-10",     7L,
  "2019-07-13",     5L,
  "2019-07-17",     3L,
  "2019-07-20",     6L,
  "2019-07-24",     8L,
  "2019-07-27",    45L,
  "2019-07-31",     2L,
  "2019-08-03",     4L,
  "2019-08-07",     5L,
  "2019-08-10",     2L,
  "2019-08-14",     9L,
  "2019-08-17",     4L,
  "2019-08-21",     2L,
  "2019-08-24",     3L,
  "2019-08-28",     1L,
  "2019-08-31",     2L,
  "2019-09-04",     1L,
  "2019-09-07",    66L,
  "2019-09-11",     2L,
  "2019-09-14",     6L,
  "2019-09-18",     3L,
  "2019-09-21",     1L,
  "2019-09-25",    16L,
  "2019-09-28",     1L,
  "2019-10-02",    20L,
  "2019-10-05",    14L,
  "2019-10-09",     5L,
  "2019-10-12",    13L,
  "2019-10-16",     2L,
  "2019-10-19",     2L,
  "2019-10-23",    78L,
  "2019-10-26",     4L,
  "2019-10-30",    23L,
  "2019-11-02",     8L,
  "2019-11-06",     6L,
  "2019-11-09",    12L,
  "2019-11-13",     1L,
  "2019-11-16",     2L,
  "2019-11-20",     4L,
  "2019-11-23",    16L,
  "2019-11-27",    13L
  )

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.