Untitled

library(stargazer)

Please cite as: 
 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
library(help = "datasets")

# importing dataset
data(mtcars)
?stargazer
starting httpd help server ...
 done
?mtcars

# summary stats for dataset
stargazer(mtcars,
          type = "text")

============================================
Statistic N   Mean   St. Dev.  Min     Max  
--------------------------------------------
mpg       32 20.091   6.027   10.400 33.900 
cyl       32  6.188   1.786     4       8   
disp      32 230.722 123.939  71.100 472.000
hp        32 146.688  68.563    52     335  
drat      32  3.597   0.535   2.760   4.930 
wt        32  3.217   0.978   1.513   5.424 
qsec      32 17.849   1.787   14.500 22.900 
vs        32  0.438   0.504     0       1   
am        32  0.406   0.499     0       1   
gear      32  3.688   0.738     3       5   
carb      32  2.812   1.615     1       8   
--------------------------------------------
?mtcars
reg1 <- 
  lm(data = mtcars,
     formula = mpg ~ cyl)
beta1 <- cov(x=mtcars$cyl, y = mtcars$mpg)/var(x=mtcars$cyl)
beta0 <- mean(mtcars$mpg)-beta1*mean(mtcars$cy1)
Warning in mean.default(mtcars$cy1): argument is not numeric or logical:
returning NA
library(tidyquant)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
✔ PerformanceAnalytics 2.0.8      ✔ TTR                  0.24.4
✔ quantmod             0.4.28     ✔ xts                  0.14.1
── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
✖ zoo::as.Date()                 masks base::as.Date()
✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
✖ PerformanceAnalytics::legend() masks graphics::legend()
✖ quantmod::summary()            masks base::summary()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fpp3)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.1 ──
✔ tibble      3.3.0     ✔ tsibble     1.1.6
✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
✔ tidyr       1.3.1     ✔ feasts      0.4.1
✔ lubridate   1.9.4     ✔ fable       0.4.1
✔ ggplot2     3.5.2     
── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date()    masks base::date()
✖ dplyr::filter()      masks stats::filter()
✖ dplyr::first()       masks xts::first()
✖ tsibble::index()     masks zoo::index()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval()  masks lubridate::interval()
✖ dplyr::lag()         masks stats::lag()
✖ dplyr::last()        masks xts::last()
✖ tsibble::setdiff()   masks base::setdiff()
✖ tsibble::union()     masks base::union()
✖ fable::VAR()         masks tidyquant::VAR()

Attaching package: 'fpp3'

The following object is masked from 'package:PerformanceAnalytics':

    prices
# assigning the stock prices data to an object
df_daily <- tq_get("AAPL", 
       get = "stock.prices",
       from = "1992-01-01")

# aggregate to monthly
aapl_data_monthly <- df_daily %>%
  mutate(month = yearmonth(date)
         ) %>%
  group_by(month) %>%
  summarise(adjusted = mean(adjusted)
            ) %>%
  as_tsibble(index = month)

# creating a file with the data
write.csv(x= aapl_data_monthly, file= "aapl_monthly_data.csv")
# assinging portions of the data to objects 
train <- aapl_data_monthly[1:323,]   # 80% of og data
test <- aapl_data_monthly[324:404,]  # 20% of og data
# fit models
models_aapl <- model(
  train,
  ETS = ETS(adjusted),
  NAIVE = NAIVE(adjusted),
  SNAIVE = SNAIVE(adjusted),
)

# forecast
h <- nrow(test)
fc_aapl <- forecast(models_aapl, h = h)

# creating plot
autoplot(fc_aapl, train) + labs(title = "My Forecast", xlab = "Time", ylab = "Adjusted Prices")