1 Stocks Practice with AAA

1.1 Introduction

This is unofficial; made by AAA. For the purposes of communicating the opportunity R programming offers for fast data analysis and translation into practice. This is for fast simple analysis. Even with it’s faults. I am a learning student. This is not intended to be professional,authoritative, final, or representative of reality. It however imperfect it may be, is for learning through application. Learning and applying knowledge; tidyquant I learned recently from an R Studio developer who uploaded onto youtube a tutorial in R for investors. The name of it is: R Studio - Tidy Trading: Data Science and R for Investors. And the URL is:https://www.youtube.com/watch?v=krdgh0e2t6g

*Citation for tidyquant() package:

citation("tidyquant")
## To cite package 'tidyquant' in publications use:
## 
##   Dancho M, Vaughan D (2023). _tidyquant: Tidy Quantitative Financial
##   Analysis_. R package version 1.0.7,
##   <https://CRAN.R-project.org/package=tidyquant>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {tidyquant: Tidy Quantitative Financial Analysis},
##     author = {Matt Dancho and Davis Vaughan},
##     year = {2023},
##     note = {R package version 1.0.7},
##     url = {https://CRAN.R-project.org/package=tidyquant},
##   }

Use learnr() or demo() Download and install R studio and R 4.3 version at least

#R version currently
version
##                _                                
## platform       x86_64-w64-mingw32               
## arch           x86_64                           
## os             mingw32                          
## crt            ucrt                             
## system         x86_64, mingw32                  
## status                                          
## major          4                                
## minor          3.0                              
## year           2023                             
## month          04                               
## day            21                               
## svn rev        84292                            
## language       R                                
## version.string R version 4.3.0 (2023-04-21 ucrt)
## nickname       Already Tomorrow

Check for installed.packages() for these packages. Ensure library() dir.

1.1.1 Description

Practice with Stock data analysis in R for investor opportunities and network science.

1.2 Data Collection and Importation

Import data from ‘tidyquant’ package for stock of interest.

The function complete_cases() filters unwanted data imported from the tq_get() function.

TM_A_G_0<-c()
TM_A_G_0 <- tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = c("1990-01-01"), to = c("2023-06-08"))


TM_A_G_N<-c()
TM_A_G_N<-tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = c("2023-01-01"), to = Sys.Date())

TM_A_G_N3<-tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = c("2023-03-01"), to = Sys.Date())

TM_A_G_NT<-tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = c("2023-05-01"), to = Sys.Date())


TM_A_Gtwnty<-c()
TM_A_Gtwnty<-tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = c("2020-01-01"), to = Sys.Date())


TM_A_Gtwnty$symbol<-as.factor(TM_A_Gtwnty$symbol)

PLot the graphs.

#install ggplot2 or tidyverse and tidyquant if not already done so
#

2 STOCKS TODAY

2.1 AMZN, GOOGL,TSLA,MSFT

Stocks (AMZN, GOOGL,TSLA,MSFT) 1 yr and 3 months ago approx. Graphs

3 plotly graphs of Stock data

3.1 1. Month to date approx

ggplotly(p=ggplot2::ggplot(data = TM_A_G_N, aes(x=as.Date(date),y= adjusted,fill=symbol))+geom_area(data=TM_A_G_N,aes( x=date,y=adjusted,fill=symbol),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)+facet_wrap(.~symbol,scales = "fixed"))
## `geom_smooth()` using formula = 'y ~ x'

3.2 2. Last 3 months approx

ggplotly(p=ggplot2::ggplot(data = TM_A_G_N3, aes(x=as.Date(date),y= adjusted,fill=symbol))+geom_area(data=TM_A_G_N3,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)+facet_wrap(.~symbol,scales = "fixed"))
## `geom_smooth()` using formula = 'y ~ x'

3.3 3. 2020-current

ggplotly(p=ggplot2::ggplot(data = TM_A_Gtwnty, aes(x=as.Date(date),y= adjusted,fill=symbol))+geom_area(data=TM_A_Gtwnty,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)+facet_wrap(.~symbol,scales = "fixed"))
## `geom_smooth()` using formula = 'y ~ x'

3.4 compare to ggplot2 static images (past 3 months)

ggplot(data = TM_A_G_N3, aes(x=as.Date(date),y= adjusted,fill=symbol))+geom_area(data=TM_A_G_N3,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="lm",se=T)+facet_wrap(symbol~.,scales = "fixed")
## `geom_smooth()` using formula = 'y ~ x'

3.5 plotly for This Month’s Stock Data**

ggplotly(p=ggplot2::ggplot(data = TM_A_G_NT, aes(x=as.Date(date),y= adjusted,fill=symbol))+geom_area(data=TM_A_G_NT,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="lm",se=T)+facet_wrap(symbol~.,scales = "fixed"))
## `geom_smooth()` using formula = 'y ~ x'

3.5.1 Data wrangling, estimation or omission, of variables of interest.

library("tidyverse")
library("tidyquant")


#create dummy object of a blank data frame.
TM_A_G<-c()
TM_A_G<-data.frame()



#assign the blank data frame the imported data from the tidy quant library for stock of interest
#use tq_get() and tq_get_options() from tidyquant package to import Yahoo Finance stock data

TM_A_G <- tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = "2010-01-01", to = "2023-06-07")

#convert symbol to correct variable data scale of measurement for statistics in R.
#convert symbol column to factor variable. 
TM_A_G$symbol<-as.factor(TM_A_G$symbol)


#use glimpse() to check
glimpse(TM_A_G)
## Rows: 13,394
## Columns: 8
## $ symbol   <fct> TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, T…
## $ date     <date> 2010-06-29, 2010-06-30, 2010-07-01, 2010-07-02, 2010-07-06, …
## $ open     <dbl> 1.266667, 1.719333, 1.666667, 1.533333, 1.333333, 1.093333, 1…
## $ high     <dbl> 1.666667, 2.028000, 1.728000, 1.540000, 1.333333, 1.108667, 1…
## $ low      <dbl> 1.169333, 1.553333, 1.351333, 1.247333, 1.055333, 0.998667, 1…
## $ close    <dbl> 1.592667, 1.588667, 1.464000, 1.280000, 1.074000, 1.053333, 1…
## $ volume   <dbl> 281494500, 257806500, 123282000, 77097000, 103003500, 1038255…
## $ adjusted <dbl> 1.592667, 1.588667, 1.464000, 1.280000, 1.074000, 1.053333, 1…
#graph a quick plot of the adjusted value for each symbol by each date.
TM_A_G %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_line(aes(color=symbol))

4 Display Warnings

Display warnings for possible missing data after filtering during import using tq_get()

dplyr::last_dplyr_warnings()
## list()

5 Longitudinal Stock Data importing, tidying, and graphical plotting*

#data importing and tidying
TM_A_G %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")+facet_grid(.~symbol,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

5.0.0.1 more examples:

TM_A_G <- tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = "2010-01-01", to = "2023-06-08")
TM_A_Gtwnty<-c()
TM_A_Gtwnty <- tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = "2020-01-01", to = "2023-06-08")

TM_A_Gstn<-c()
TM_A_Gstn <- tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = "2016-01-01", to = "2023-06-08")
TM_A_Gtwnty %>% head()
symbol date open high low close volume adjusted
TSLA 2020-01-02 28.30000 28.71333 28.11400 28.68400 142981500 28.68400
TSLA 2020-01-03 29.36667 30.26667 29.12800 29.53400 266677500 29.53400
TSLA 2020-01-06 29.36467 30.10400 29.33333 30.10267 151995000 30.10267
TSLA 2020-01-07 30.76000 31.44200 30.22400 31.27067 268231500 31.27067
TSLA 2020-01-08 31.58000 33.23267 31.21533 32.80933 467164500 32.80933
TSLA 2020-01-09 33.14000 33.25333 31.52467 32.08933 426606000 32.08933
TM_A_G_N<-c()
TM_A_G_N <- tq_get(x = c("TSLA","MSFT","AMZN","GOOGL"), get = "stock.prices", complete_cases = TRUE, from = "2023-01-01", to = Sys.Date())

5.0.1 Convert Vars

TM_A_G_0$symbol<-as.factor(TM_A_G_0$symbol)
TM_A_Gstn$symbol<-as.factor(TM_A_Gstn$symbol)
TM_A_Gtwnty$symbol<-as.factor(TM_A_Gtwnty$symbol)
TM_A_G_N$symbol<-as.factor(TM_A_G_N$symbol)

5.1 Preview Example Plots

5.1.1 demo example 1

This year LOESS model 2023 Stocks

5.2 ggplot

ggplot(data = TM_A_G_N3, aes(x=as.Date(date),y= adjusted),fill=symbol)+geom_area(data=TM_A_G_N3,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="lm",se=T)+facet_wrap(symbol~.,scales = "fixed")
## `geom_smooth()` using formula = 'y ~ x'

5.3 plotly

ggplot(data = TM_A_G_N, aes(x=date,y=adjusted,fill=symbol))+
  geom_area(data=TM_A_G_N,
            aes( x=date,y=adjusted, fill=symbol, color="black"), 
            show.legend = F,
            inherit.aes=F)+ geom_smooth(method="loess",se=T)+
  facet_wrap(.~symbol,scales = "fixed")
## `geom_smooth()` using formula = 'y ~ x'

6 Statistical Models for Stocks Data

Using geom_smooth() ggplot2 package, and the tidyquant library R package in addition to the standard base and **tidyverse open source tools of efficient tools for efficient data power handling.

6.1 TSLA, MSFT,AMZN,and GOOGL Stock Statistical Models

TSLA, MSFT,AMZN,and GOOGL Stock Statistical Models including time-series linear and non-parametric models.

6.1.1 Fig 1. LOESS Model of Stock adjusted value

###2020-2023

TM_A_Gtwnty %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2020-06/08/2023 by AAA")+facet_wrap(symbol~.,scales="fixed")
## `geom_smooth()` using formula = 'y ~ x'

6.1.2 2016-2023 plotly graph

TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted, fill=factor(symbol))) + geom_smooth(method="loess",se=T,aes(color=factor(symbol)))+facet_wrap(symbol~.,scales="fixed")
## `geom_smooth()` using formula = 'y ~ x'

#+geom_line(aes(color= factor(c(symbol))))+
ggtitle(label = "LOESS Model of #STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/08/2023 by AAA")
## $title
## [1] "LOESS Model of #STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/08/2023 by AAA"
## 
## attr(,"class")
## [1] "labels"

6.1.3 Data Coding and Graphical Plotting of Data

Data dictionaries are a source of metadata for many variables necessary for classification in R if used as an R object in data analysis.

…i.e. categorical vs nominal vs ordinal Categorical scales of measurement (i.e. nominal variables such as race, gender, sex, etc.) data are defined as having no “mean” in the operationalisation of variables for research. This must be done before any data analysis for correct interpretation and representation of bugs. Operationalion definitions are fundamental in empirical research design.

In R categorical data variables are defined as class factors in R. with n levels indexed by integers[1:n]

…i.e. discrete vs continuous …i.e. particle vs wave variable data scale of measurement for statistics in R.

#convert symbol column to factor variable. 
#factor
TM_A_G_0$symbol<-as.factor(TM_A_G_0$symbol)
TM_A_Gstn$symbol<-as.factor(TM_A_Gstn$symbol)
TM_A_Gtwnty$symbol<-as.factor(TM_A_Gtwnty$symbol)

#use glimpse() to check
glimpse(TM_A_G)
## Rows: 13,398
## Columns: 8
## $ symbol   <chr> "TSLA", "TSLA", "TSLA", "TSLA", "TSLA", "TSLA", "TSLA", "TSLA…
## $ date     <date> 2010-06-29, 2010-06-30, 2010-07-01, 2010-07-02, 2010-07-06, …
## $ open     <dbl> 1.266667, 1.719333, 1.666667, 1.533333, 1.333333, 1.093333, 1…
## $ high     <dbl> 1.666667, 2.028000, 1.728000, 1.540000, 1.333333, 1.108667, 1…
## $ low      <dbl> 1.169333, 1.553333, 1.351333, 1.247333, 1.055333, 0.998667, 1…
## $ close    <dbl> 1.592667, 1.588667, 1.464000, 1.280000, 1.074000, 1.053333, 1…
## $ volume   <dbl> 281494500, 257806500, 123282000, 77097000, 103003500, 1038255…
## $ adjusted <dbl> 1.592667, 1.588667, 1.464000, 1.280000, 1.074000, 1.053333, 1…

6.2 Graph plot of Adjust Value for each Stock

Graph a quick plot of the adjusted value for each symbol by each date.

6.3 2010-2023

TM_A_G %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_line(aes(color=symbol))

TM_A_G %>% ggplot(aes(x=as.Date(date),y=volume,fill=factor(c(symbol))))+geom_line(aes(color=symbol))

#adjusted vs volume for stock data
TM_A_G %>% ggplot(aes(x=adjusted,y=volume,fill=symbol))+ geom_point(aes(color=symbol))+ geom_smooth()+facet_wrap(.~symbol,scales="free")+ggtitle(label = "Adjusted(x) vs Volume (y) Stock Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

6.3.1 Data importing and tidying

#data importing and tidying
TM_A_G %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_G %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_G %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")+facet_grid(.~symbol,scales="fixed")
## `geom_smooth()` using formula = 'y ~ x'

6.4 Graphs and facets using ggplot()

6.4.1 2016-2023

TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_line(aes(color=symbol))

TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/07/2023 by AAA")+facet_grid(.~symbol,scales="fixed")
## `geom_smooth()` using formula = 'y ~ x'

6.4.2 2020-2023

Data importing and tidying

#data importing and tidying
TM_A_Gtwnty %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_Gtwnty %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_Gtwnty %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/07/2023 by AAA")+facet_grid(.~symbol,scales="fixed")
## `geom_smooth()` using formula = 'y ~ x'

#arrange data and view
TM_A_G %>% arrange(desc(adjusted)) %>% head()
symbol date open high low close volume adjusted
TSLA 2021-11-04 411.4700 414.4967 405.6667 409.9700 76192200 409.9700
TSLA 2021-11-05 409.3333 413.2900 402.6667 407.3633 64886400 407.3633
TSLA 2021-11-03 392.4433 405.1300 384.2067 404.6200 103885500 404.6200
TSLA 2021-11-01 381.6667 403.2500 372.8867 402.8633 168146100 402.8633
TSLA 2022-01-03 382.5833 400.3567 378.6800 399.9267 103931400 399.9267
TSLA 2021-11-02 386.4533 402.8633 382.0000 390.6667 128213400 390.6667
TM_A_G %>% arrange(desc(volume)) %>% head()
symbol date open high low close volume adjusted
TSLA 2020-02-04 58.86400 64.59933 55.59200 59.13733 914082000 59.13733
AMZN 2010-07-23 5.29650 5.96400 5.29000 5.94350 848422000 5.94350
AMZN 2010-02-01 6.15900 6.24300 5.69100 5.94350 755488000 5.94350
TSLA 2020-02-05 54.88400 56.39867 46.94067 48.98000 726357000 48.98000
TSLA 2020-02-03 44.91267 52.40933 44.90133 52.00000 705975000 52.00000
TSLA 2020-12-18 222.96666 231.66667 209.51334 231.66667 666378600 231.66667
#TM_A_G %>% arrange(desc()) %>% head()
TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2010-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/07/2023 by AAA")+facet_grid(symbol~.,scales="free")
## `geom_smooth()` using formula = 'y ~ x'

TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=adjusted,fill=factor(c(symbol))))+geom_smooth(method="loess",se=T)+geom_line(aes(color= factor(c(symbol))))+ggtitle(label = "LOESS Model of STOCK Data (TSLA,MSFT,AMZN,GOOGL), 01/01/2016-06/07/2023 by AAA")+facet_grid(.~symbol,scales="fixed")
## `geom_smooth()` using formula = 'y ~ x'

7 *Mutate() data with tidyverse() pkg

Use mutate function from tidyverse package in the library to create new variable column named, vol_times_adjusted.

vol_times_adjusted=c(c(volumeadjusted))) where x is n* real numbers of discrete temporal and continous multiple types of data. This is known as data triangulation in some senses of the word so to speak.

#create new variable for volume times adjusted for each observation of TM_A_G object
#use mutate function to create new column with new computed/calculated values
TM_A_G<- TM_A_G %>% mutate(vol_times_adjusted= c(c(volume)*c(adjusted)))


TM_A_Gstn<- TM_A_Gstn %>% mutate(vol_times_adjusted= c(c(volume)*c(adjusted)))


TM_A_Gtwnty<- TM_A_Gtwnty %>% mutate(vol_times_adjusted= c(c(volume)*c(adjusted)))

glimpse(TM_A_G)
## Rows: 13,398
## Columns: 9
## $ symbol             <chr> "TSLA", "TSLA", "TSLA", "TSLA", "TSLA", "TSLA", "TS…
## $ date               <date> 2010-06-29, 2010-06-30, 2010-07-01, 2010-07-02, 20…
## $ open               <dbl> 1.266667, 1.719333, 1.666667, 1.533333, 1.333333, 1…
## $ high               <dbl> 1.666667, 2.028000, 1.728000, 1.540000, 1.333333, 1…
## $ low                <dbl> 1.169333, 1.553333, 1.351333, 1.247333, 1.055333, 0…
## $ close              <dbl> 1.592667, 1.588667, 1.464000, 1.280000, 1.074000, 1…
## $ volume             <dbl> 281494500, 257806500, 123282000, 77097000, 10300350…
## $ adjusted           <dbl> 1.592667, 1.588667, 1.464000, 1.280000, 1.074000, 1…
## $ vol_times_adjusted <dbl> 448326996, 409568688, 180484846, 98684158, 11062575…
TM_A_G %>% str()
## tibble [13,398 × 9] (S3: tbl_df/tbl/data.frame)
##  $ symbol            : chr [1:13398] "TSLA" "TSLA" "TSLA" "TSLA" ...
##  $ date              : Date[1:13398], format: "2010-06-29" "2010-06-30" ...
##  $ open              : num [1:13398] 1.27 1.72 1.67 1.53 1.33 ...
##  $ high              : num [1:13398] 1.67 2.03 1.73 1.54 1.33 ...
##  $ low               : num [1:13398] 1.17 1.55 1.35 1.25 1.06 ...
##  $ close             : num [1:13398] 1.59 1.59 1.46 1.28 1.07 ...
##  $ volume            : num [1:13398] 2.81e+08 2.58e+08 1.23e+08 7.71e+07 1.03e+08 ...
##  $ adjusted          : num [1:13398] 1.59 1.59 1.46 1.28 1.07 ...
##  $ vol_times_adjusted: num [1:13398] 4.48e+08 4.10e+08 1.80e+08 9.87e+07 1.11e+08 ...
tapply(TM_A_G$vol_times_adjusted,TM_A_G$symbol,mean,na.rm=T) %>% glimpse()
##  num [1:4(1d)] 4.93e+09 2.17e+09 3.17e+09 6.68e+09
##  - attr(*, "dimnames")=List of 1
##   ..$ : chr [1:4] "AMZN" "GOOGL" "MSFT" "TSLA"
#look at tq_get_options() for arguments for stock investor data analysis
tq_get_options()
##  [1] "stock.prices"       "stock.prices.japan" "dividends"         
##  [4] "splits"             "economic.data"      "quandl"            
##  [7] "quandl.datatable"   "tiingo"             "tiingo.iex"        
## [10] "tiingo.crypto"      "alphavantager"      "alphavantage"      
## [13] "rblpapi"

7.1 Histogram distribution plots

Histogram Distribution plots of Adjusted values and Volume values of Stocks by Symbol from 2010-2023

#Base R function hist() plots of adjusted and volume data values for Stocks AMZN,TSLA,MSFT,GOOGL.


#1.2010-2023
hist( x= TM_A_G$adjusted)

hist(x= TM_A_G$volume)

#2.2016-2023
hist(x=TM_A_Gstn$adjusted)

hist(x=TM_A_Gstn$volume)

#3.2020-2023
hist(x=TM_A_Gtwnty$adjusted)

hist(x=TM_A_Gtwnty$volume)

2. #ggplot2 pkg
## [1] 2
#2010-2023
ggplot(data = TM_A_G, aes(x=date,y= adjusted,color=symbol))+geom_area(aes(fill=symbol))

#2016-2023
ggplot(data = TM_A_Gstn, aes(x=date,y= adjusted,color=symbol))+geom_area(aes(fill=symbol))

#simple faceted boxplot
TM_A_G %>% group_by(date) %>% ggplot(aes(y=adjusted,fill=symbol,color="red"))+geom_boxplot(aes(y=adjusted,fill=symbol),inherit.aes = F,show.legend = F)+facet_wrap(.~symbol, scales = "free")

7.2 2010-2023 Plots and Facets

#2010-2023
ggplot(data = TM_A_G, aes(x=date,y= adjusted,color=symbol))+geom_area(aes(fill=symbol))

#2010-2023
ggplot(data = TM_A_G, aes(x=date,y= adjusted,fill=symbol))+geom_area(data=TM_A_Gtwnty,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)
## `geom_smooth()` using formula = 'y ~ x'

#2010-2023
ggplot(data = TM_A_G, aes(x=date,y= adjusted,fill=symbol))+geom_area(data=TM_A_Gtwnty,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)+facet_wrap(.~symbol,scales = "fixed")
## `geom_smooth()` using formula = 'y ~ x'

7.3 2016-2023 Plots and Facets

#2016-2023
ggplot(data = TM_A_Gstn, aes(x=date,y= adjusted,color=symbol))+geom_area(aes(fill=symbol))

#2016-2023
ggplot(data = TM_A_Gstn, aes(x=date,y= adjusted,fill=symbol))+geom_area(data=TM_A_Gtwnty,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)
## `geom_smooth()` using formula = 'y ~ x'

#2016-2023
ggplot(data = TM_A_Gstn, aes(x=date,y= adjusted,fill=symbol))+geom_area(data=TM_A_Gtwnty,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)+facet_wrap(.~symbol,scales = "fixed")
## `geom_smooth()` using formula = 'y ~ x'

7.4 2020-2023 Plots and Facets

#2020-2023
ggplot(data = TM_A_Gtwnty, aes(x=date,y= adjusted,color=symbol))+geom_area(aes(fill=symbol))

#2020-2023
ggplot(data = TM_A_Gtwnty, aes(x=date,y= adjusted,fill=symbol))+geom_area(data=TM_A_Gtwnty,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)
## `geom_smooth()` using formula = 'y ~ x'

#2020-2023
ggplot(data = TM_A_Gtwnty, aes(x=date,y= adjusted,fill=symbol))+geom_area(data=TM_A_Gtwnty,aes( x=date,y=adjusted,fill=symbol,color="black"),show.legend = F,inherit.aes = F)+geom_smooth(method="loess",se=T)+facet_wrap(.~symbol,scales = "fixed")
## `geom_smooth()` using formula = 'y ~ x'

7.5 Linear Models

Data analysis:

7.5.1 Examples

generalized linear model of volume times adjusted stocks for each symbol by date:

…i.e…

#TM_A_G =2010-2023
#TM_A_Gstn= 2016-2023
#TM_A_Gtwnty=2020-2023
TM_A_G %>% ggplot(aes(x=as.Date(date),y=c(vol_times_adjusted),fill=symbol) )+geom_smooth(method="lm",se = T)+ggtitle(label="linear model of volume times adjusted stocks for each symbol by AAA")
## `geom_smooth()` using formula = 'y ~ x'

#2016-2020
TM_A_Gstn %>% ggplot(aes(x=as.Date(date),y=c(vol_times_adjusted),fill=symbol) )+geom_smooth(method="lm",se = T)+ggtitle(label="linear model of volume times adjusted stocks for each symbol by AAA")
## `geom_smooth()` using formula = 'y ~ x'

#2020-2023
TM_A_Gtwnty %>% ggplot(aes(x=as.Date(date),y=c(vol_times_adjusted),fill=symbol) )+geom_smooth(method="lm",se = T)+ggtitle(label="linear model of volume times adjusted stocks for each symbol by AAA")
## `geom_smooth()` using formula = 'y ~ x'