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.

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("2010-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())

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

#use Sys.Date() call function for "current" date
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,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'

ggplot(data = TM_A_G_N3, aes(x=as.Date(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="lm",se=T)+facet_wrap(symbol~.,scales = "fixed")
## `geom_smooth()` using formula = 'y ~ x'

2.2 This Month’s Stock Data

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'

2.2.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))

3 Display Warnings

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

dplyr::last_dplyr_warnings()
## list()

4 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'

4.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()
## # A tibble: 6 × 8
##   symbol date        open  high   low close    volume adjusted
##   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
## 1 TSLA   2020-01-02  28.3  28.7  28.1  28.7 142981500     28.7
## 2 TSLA   2020-01-03  29.4  30.3  29.1  29.5 266677500     29.5
## 3 TSLA   2020-01-06  29.4  30.1  29.3  30.1 151995000     30.1
## 4 TSLA   2020-01-07  30.8  31.4  30.2  31.3 268231500     31.3
## 5 TSLA   2020-01-08  31.6  33.2  31.2  32.8 467164500     32.8
## 6 TSLA   2020-01-09  33.1  33.3  31.5  32.1 426606000     32.1
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())

4.0.1 Convert Vars

TM_A_G$symbol<-as.factor(TM_A_G$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)

4.1 Preview Example Plots

4.1.1 demo example 1

This year LOESS model 2023 Stocks

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'

5 Statistical Models for Stocks Data

Using geom_smooth() ggplot2 package, and the tidyquant library R package i naddition to the standard base and tidyverse openstore of efficient tools for efficient data power handling. ## TSLA, MSFT,AMZN,and GOOGL Stock Statistical Models TSLA, MSFT,AMZN,and GOOGL Stock Statistical Models including time-series linear and non-parametric models.

5.0.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'

5.0.2 2016-2023

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/08/2023 by AAA")+facet_wrap(symbol~.,scales="fixed")
## `geom_smooth()` using formula = 'y ~ x'

5.0.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$symbol<-as.factor(TM_A_G$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   <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…

5.1 Graph plot of Adjust Value for each Stock

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

5.2 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")'

5.2.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'

5.3 Graphs and facets using ggplot()

5.3.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'

5.3.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()
## # A tibble: 6 × 8
##   symbol date        open  high   low close    volume adjusted
##   <fct>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
## 1 TSLA   2021-11-04  411.  414.  406.  410.  76192200     410.
## 2 TSLA   2021-11-05  409.  413.  403.  407.  64886400     407.
## 3 TSLA   2021-11-03  392.  405.  384.  405. 103885500     405.
## 4 TSLA   2021-11-01  382.  403.  373.  403. 168146100     403.
## 5 TSLA   2022-01-03  383.  400.  379.  400. 103931400     400.
## 6 TSLA   2021-11-02  386.  403.  382   391. 128213400     391.
TM_A_G %>% arrange(desc(volume)) %>% head()
## # A tibble: 6 × 8
##   symbol date         open   high    low  close    volume adjusted
##   <fct>  <date>      <dbl>  <dbl>  <dbl>  <dbl>     <dbl>    <dbl>
## 1 TSLA   2020-02-04  58.9   64.6   55.6   59.1  914082000    59.1 
## 2 AMZN   2010-07-23   5.30   5.96   5.29   5.94 848422000     5.94
## 3 AMZN   2010-02-01   6.16   6.24   5.69   5.94 755488000     5.94
## 4 TSLA   2020-02-05  54.9   56.4   46.9   49.0  726357000    49.0 
## 5 TSLA   2020-02-03  44.9   52.4   44.9   52    705975000    52   
## 6 TSLA   2020-12-18 223.   232.   210.   232.   666378600   232.
#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'

6 *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             <fct> TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, TSLA, TSL…
## $ 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            : Factor w/ 4 levels "AMZN","GOOGL",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ 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"

6.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")

6.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'

6.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'

## 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'

6.4 Linear Models

Data analysis:

6.4.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'