#
remove(list = ls())homework6
Set Up
Importing
# importing package
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
?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
--------------------------------------------
Task 1: Bivariate Regression & Slope Calculation
# creating a bivariate regression for the data on the variables mpg & cyl
reg1 <- lm(data = mtcars,
formula = mpg ~ cyl)
# printing the regression
reg1
Call:
lm(formula = mpg ~ cyl, data = mtcars)
Coefficients:
(Intercept) cyl
37.885 -2.876
# printing the summary stats for the regression (note the slope is -2.876)
summary(reg1)
Call:
lm(formula = mpg ~ cyl, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.9814 -2.1185 0.2217 1.0717 7.5186
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.8846 2.0738 18.27 < 2e-16 ***
cyl -2.8758 0.3224 -8.92 6.11e-10 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 3.206 on 30 degrees of freedom
Multiple R-squared: 0.7262, Adjusted R-squared: 0.7171
F-statistic: 79.56 on 1 and 30 DF, p-value: 6.113e-10
round(sum(reg1$residuals), 15)[1] -4e-15
# calculating the slope using the formula to prove it is the same
beta1 <- cov(mtcars$mpg, mtcars$cyl)/var(mtcars$cyl)
# printing the slope (also -2.876)
beta1[1] -2.87579
Creating Data to Import to Excel
# load packages
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("META",
get = "stock.prices",
from = "1992-01-01")
# aggregate to monthly
meta_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= meta_data_monthly, file= "meta_monthly_data.csv")# assinging portions of the data to objects
train <- meta_data_monthly[1:128,] # 80% of og data
test <- meta_data_monthly[129:160,] # 20% of og dataTask 2: Fitting Models
# fit models
models_meta <- model(
train,
ETS = ETS(adjusted),
NAIVE = NAIVE(adjusted),
SNAIVE = SNAIVE(adjusted),
)
# forecast
h <- nrow(test)
fc_meta <- forecast(models_meta, h = h)
# creating plot
autoplot(fc_meta, train) + labs(title = "My Forecast", xlab = "Time", ylab = "Adjusted Prices")
Note
Recommendations:
The fact that the adjusted stocks that we charted in Excel seems to be trending upwards, coupled with an positive (although small) slope and ETS model, makes me believe that investing in META could be a good idea.
However, although stock prices are accelerating upwards, the data also has trends of sharp downwards spikes such as in around 2023 and most recently in early 2025, so buying stock could come with a risk.