<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>R Stories</title><link>https://rstories.netlify.app/</link><description>Recent content on R Stories</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 26 May 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://rstories.netlify.app/index.xml" rel="self" type="application/rss+xml"/><item><title>Quantifying extra compensations of full-time instructors at an ideal college.</title><link>https://rstories.netlify.app/2024/05/26/quantifying-extra-compensations-of-full-time-instructors-at-an-ideal-college./</link><pubDate>Sun, 26 May 2024 00:00:00 +0000</pubDate><guid>https://rstories.netlify.app/2024/05/26/quantifying-extra-compensations-of-full-time-instructors-at-an-ideal-college./</guid><description>
&lt;p>In many higher education institutions in the U.S., a full-time faculty member typically receives a base salary for 9 or 10 months, disbursed in monthly installments over the academic year or the whole year. Nevertheless, within this framework, faculty may also accrue additional income through various avenues such as administrative allowances, service allowances, over-load compensations, over-enrollment compensations, summer teaching compensations, or other similar forms of remuneration. Among these, over-load compensations (&lt;code>OL_COMP&lt;/code>) and over-enrollment compensations (&lt;code>OE_COMP&lt;/code>) stand out as the most prevalent during academic semesters, excluding summer compensations.&lt;/p>
&lt;p>&lt;code>OL_COMP&lt;/code> come into play when faculty undertake teaching responsibilities exceeding the standard credit hours mandated for full-time faculty as outlined in their contractual agreements or institutional guidelines.&lt;/p>
&lt;p>&lt;code>OE_COMP&lt;/code> occur when class enrollments surpass the institution’s predefined regular enrollment threshold.&lt;/p>
&lt;p>With the aid of the statistical software R, publicly available class schedule data, and institutional compensation guidelines, we quantify OL_COMP and OE_COMP earned by each instructor during a semester within an exemplary higher education institution referred to as &lt;code>Ideal College.&lt;/code>&lt;/p>
&lt;div id="class-schedule-data-from-ideal-college." class="section level2">
&lt;h2>Class schedule data from &lt;code>Ideal College&lt;/code>.&lt;/h2>
&lt;p>Listed below are examples of exemplary higher education institutions that provide publicly accessible class schedule data. A significant number of these institutions utilize software developed by Ellucian Company L.P. to generate these schedules, resulting in similarities across many datasets. However, institutions retain the autonomy to customize the display of their data, with some choosing to impose restrictions&lt;/p>
&lt;ul>
&lt;li>&lt;p>&lt;a href="https://courseschedule.lipscomb.edu/ScheduleP2023SPRING.html?_gl=1*22p1u1*_up*MQ">Lipscomb University example of class schedule&lt;/a>&lt;/p>&lt;/li>
&lt;li>&lt;p>&lt;a href="https://simba.savannahstate.edu/students/schedule.aspx?term=202402">Savannah State University example of class schedule&lt;/a>&lt;/p>&lt;/li>
&lt;li>&lt;p>&lt;a href="https://benedict.edu/fall-2022-course-offerings/">Benedict College example of class schedule&lt;/a>&lt;/p>&lt;/li>
&lt;li>&lt;p>&lt;a href="https://github.com/dawit3000/P3/blob/master/Ideal_College_Fall_2021_Schedule.csv">‘Ideal College’ class schedule folder&lt;/a>&lt;/p>&lt;/li>
&lt;/ul>
&lt;p>Let’s get the class schedule data for ’Ideal College and replace original names by random names.&lt;/p>
&lt;pre class="r">&lt;code>##Get data, clean a bit, avoid using real names
library(dplyr)
schedule&amp;lt;- read.csv(&amp;quot;https://raw.githubusercontent.com/dawit3000/P3/master/Ideal_College_Fall_2021_Schedule.csv&amp;quot;)
schedule&amp;lt;-subset(schedule,schedule$INSTRUCTOR !=&amp;quot;&amp;quot; &amp;amp; schedule$ENRLD &amp;gt; 2) ## will see later that when ENRLD is 2 or less, no compensation
schedule&amp;lt;-subset(schedule, select=c(&amp;quot;INSTRUCTOR&amp;quot;,&amp;quot;SUBJ&amp;quot;, &amp;quot;NUMB&amp;quot;, &amp;quot;ENRLD&amp;quot;, &amp;quot;HRS&amp;quot; )) ## Choose only the columns you might need
## Replace original names by random names
library(randomNames)
original_names&amp;lt;-unique(schedule$INSTRUCTOR)
random_names&amp;lt;-randomNames(length(original_names)) #Random names to replace original names
for(i in 1:length(original_names))
{
schedule[schedule == original_names[i]]&amp;lt;-random_names[i]
}
head(schedule)
## INSTRUCTOR SUBJ NUMB ENRLD HRS
## 1 Fragale, Dylan ACCT 2101 34 3
## 2 Argueta, Luis ACCT 2102 33 3
## 3 Fragale, Dylan ACCT 3113 7 3
## 4 Argueta, Luis ACCT 3133 10 3
## 5 Trujillo Gallegos, Annel ACCT 4113 9 3
## 6 Nguyen, Mindy AENT 2833 27 3&lt;/code>&lt;/pre>
&lt;p>Since instructors may teach more than one class, their names may appear in the above table more than once under the variable ‘INSTRUCTOR’. We will need the variables &lt;code>INSTRUCTOR&lt;/code> (instructor name), &lt;code>ENRLD&lt;/code> (number of students enrolled in the class) and &lt;code>HRS&lt;/code> (credit hours). Let us also create here a list of the unique instructors, &lt;code>INSTR_i, i=1,2,3, ...,&lt;/code> for future use.&lt;/p>
&lt;pre class="r">&lt;code>INSTR &amp;lt;-unique(schedule[c(&amp;quot;INSTRUCTOR&amp;quot;)])
INSTR &amp;lt;- arrange(INSTR, INSTRUCTOR)
head(INSTR)
## INSTRUCTOR
## 1 Akquia, Shamika
## 2 Alemayehu, Jonaiyah
## 3 Allen, Elias
## 4 Anaya, Savannah
## 5 Anderson, Jha Nae
## 6 Andrew, Sylvie&lt;/code>&lt;/pre>
&lt;/div>
&lt;div id="guidelines-of-ideal-college-for-ol_comp-and-oe_comp" class="section level2">
&lt;h2>Guidelines of &lt;code>Ideal College&lt;/code> for &lt;code>OL_COMP&lt;/code> and &lt;code>OE_COMP&lt;/code>:&lt;/h2>
&lt;ol style="list-style-type: decimal">
&lt;li>The standard teaching load for a full-time Instructor at this college is 12 credit hours, with a recommended student enrollment range of 10 to 30 students per class.&lt;/li>
&lt;li>Classes with an enrollment (&lt;code>ENRLD&lt;/code>) of less than 3 will not qualify for &lt;code>OL_COMP&lt;/code>. It’s advised to teach such classes within the regular load or consider cancellation.&lt;/li>
&lt;li>Instructors teaching more than 12 credit hours will receive &lt;code>OL_COMP&lt;/code> for each additional credit hour if the enrollment in those extra classes is 3 or more.&lt;/li>
&lt;li>For classes with normal enrollment (10 to 30 students), compensation is calculated at a rate of &lt;code>$1000&lt;/code> per credit hour, resulting in &lt;code>OL_COMP = 1000*(#_of_credit_hours).&lt;/code>&lt;/li>
&lt;li>If a class has an enrollment in the range 3 to 9 and is considered for over-load compensation, compensation will be prorated based on the 10-student minimum requirement for standard enrollment: &lt;code>OL_COMP = (#_of_students/10)*1000*(#_of_credit_hours).&lt;/code>&lt;/li>
&lt;li>Classes with an enrollment exceeding 30 students will be considered over-enrolled, and instructors will be compensated for the additional students based on a prorated scale relative to the standard maximum enrollment of 30: &lt;code>OE_COMP = (#_of_extra_students/30)*1000*(#_of_credit_hours).&lt;/code>&lt;/li>
&lt;li>&lt;code>OL_COMP&lt;/code> calculations begin with the lowest-enrollment class with &lt;code>ENRLD&lt;/code> greater than or equal to 3 and continue up until the standard load of 12 credit hours is reached.&lt;/li>
&lt;/ol>
&lt;/div>
&lt;div id="implementing-ol_comp-and-oe_comp-guidelines-of-ideal-college" class="section level2">
&lt;h2>Implementing &lt;code>OL_COMP&lt;/code> and &lt;code>OE_COMP&lt;/code> guidelines of &lt;code>Ideal College&lt;/code>&lt;/h2>
&lt;p>Our approach involves iterating through each instructor (starting with &lt;code>i=1&lt;/code>) to compute their compensation according to the guidelines. Results for each instructor will be appended to a data frame &lt;code>df&lt;/code> using &lt;code>rbind&lt;/code>, forming the final output. We’ll calculate three key metrics for each instructor: &lt;code>OL_COMP, OE_COMP, and QOHRS (qualified overload credit hours)&lt;/code>, providing a simplified overview of compensations.&lt;/p>
&lt;pre class="r">&lt;code>library(&amp;quot;scales&amp;quot;)
df&amp;lt;-NULL
for(i in 1:nrow(INSTR))
{
output&amp;lt;-filter(schedule, grepl(INSTR[i, 1],schedule$INSTRUCTOR))
output&amp;lt;-arrange(output, desc(output$ENRLD)) # we want low ENRLD courses listed at the bottom
# Calculate Over-Enrolled Payment for each course and store the sum in a variable OE_COMP
OE_COMP_VEC&amp;lt;-&amp;quot;&amp;quot;
for(j in 1:nrow(output))
{
if(output$ENRLD[j]&amp;gt;30){OE_COMP_VEC[j]&amp;lt;-round((output$ENRLD[j]-30)*(1000/30)*output$HRS[j],2)}
}
OE_COMP&amp;lt;-round(sum(as.numeric(OE_COMP_VEC)),2) # OE_COMP available here
# Calculate total qualified overload credit hours that exceed 12 credit hours (QOHRS)
QOHRS&amp;lt;-max(0, sum(as.numeric(output$HRS)) - 12 , na.rm=TRUE)
# Calculate OL_COMP and prorate where applicable
OL_COMP&amp;lt;-0
m&amp;lt;-length(output$HRS) # so that output$HRS[m] will be the m-th credit hour
p&amp;lt;-sum(output$HRS) #Total Credit Hours
while(p&amp;gt;12 ){
OL_COMP&amp;lt;- OL_COMP + ( (min(output$ENRLD[m], 10))/10 )*(1000)* (min(output$HRS[m], p-12))
p&amp;lt;-p-output$HRS[m]
m&amp;lt;-m-1
}
# OL_COMP available here.
# Calculate total pay from OL_COMP and OE_COMP
TOTAL_COMP&amp;lt;-OL_COMP + OE_COMP
#create the i-th data-frame to contain results
INSTRUCTOR&amp;lt;-INSTR[i,1]
df_i&amp;lt;-data.frame(INSTRUCTOR, QOHRS, OL_COMP, OE_COMP, TOTAL_COMP)
## Bind by row with previous outcome to get the big &amp;#39;df&amp;#39;
df&amp;lt;-rbind(df,df_i)
}
## &amp;#39;df&amp;#39; available here with required results
df[is.na(df)]&amp;lt;-0
df&amp;lt;-subset(df, df$TOTAL_COMP !=0) # keep only instructors who are compensated
head(df, n=20)
## INSTRUCTOR QOHRS OL_COMP OE_COMP TOTAL_COMP
## 4 Anaya, Savannah 0 0 133.33 133.33
## 7 Argueta, Luis 0 0 300.00 300.00
## 9 Awtrey, Lauren 0 0 600.00 600.00
## 10 Bailey, Asre 7 7000 1066.66 8066.66
## 12 Brookman, Caleb 0 0 500.00 500.00
## 13 Brown, Daliyah 3 3000 600.00 3600.00
## 16 Cao, Kenny 2 600 5133.33 5733.33
## 17 Chau, Alexis 1 800 2866.67 3666.67
## 19 Chung, Sumana 0 0 2500.00 2500.00
## 23 Cotter, David 0 0 1333.33 1333.33
## 25 Davis, Danielle 0 0 1900.00 1900.00
## 28 Dennis, Leann 12 10800 2933.33 13733.33
## 31 Early-Harris, Caerra 0 0 300.00 300.00
## 35 Fragale, Dylan 0 0 400.00 400.00
## 36 Funk, Scarlett 0 0 2133.34 2133.34
## 39 Garner, Sarahray 4 2500 1900.00 4400.00
## 40 Garrett, Jaszymn 0 0 800.00 800.00
## 41 George, Kyle 0 0 900.00 900.00
## 46 Harness, Kandace 1 700 500.00 1200.00
## 49 Hernandez, Alexandria 0 0 766.66 766.66&lt;/code>&lt;/pre>
&lt;/div></description></item><item><title>Trial Post 2024 22</title><link>https://rstories.netlify.app/2024_test_post_r-markdown/</link><pubDate>Fri, 24 May 2024 00:00:00 +0000</pubDate><guid>https://rstories.netlify.app/2024_test_post_r-markdown/</guid><description>
&lt;div id="introduction-to-2024" class="section level2">
&lt;h2>Introduction to 2024&lt;/h2>
&lt;p>Institutional and Hedge Fund managers return from summer vacation and adjust their financial portfolios at the end of each summer, causing a selloff pressure in the market. September is considered a bad month for the Bulls [1]&lt;/p>
&lt;p>“Given that September tends to be a bad month for the market, I’m urging you to be prepared …” Jim Cramer on CNBC, September 11, 2020.&lt;/p>
&lt;p>For this and other reasons, such as election hypes, pandemic, etc., people with trading, investing or retirement portfolios may want to know how their financial portfolios (or instruments such as stocks or ETFs in their portfolios) performed over some years, months, weeks or days. They may also want to know the average monthly, yearly, weekly or daily returns, starting from some fixed time of start in the past to the present or recent time.&lt;/p>
&lt;p>Almost all portfolio managers measure performance with reference to a benchmark [3].
In this short note, we will consider the historical data of the Standard and Poor’s 500 Index (S&amp;amp;P 500, symbol=^GSPC) from Yahoo! Finance, which is widely regarded as the best gauge of large-cap U.S. equities. Other well known benchmarks include DOW-30, NASDAQ-100, and the Russell 2000 Index for small-caps.&lt;/p>
&lt;p>We will then outline a simple way to visualize or summarize monthly returns as well as average monthly returns using R. Interested readers can modify the instrument, period and length of time to their preference.&lt;/p>
&lt;p>We start by installing the R packages that will be needed to produce libraries later. For more information about one of the key packages used here, the &lt;code>tidyquant&lt;/code> package, see [2].&lt;/p>
&lt;/div>
&lt;div id="getting-and-preparing-data" class="section level2">
&lt;h2>Getting and Preparing Data&lt;/h2>
&lt;p>We will get the data for the S&amp;amp;P 500 Index, symbol = ^GSPC, from Yahoo! Finance. We will then prepare the data for visualization and/or Summarization of results as needed.&lt;/p>
&lt;/div></description></item><item><title>Applications of randomNames (an R-Package): avoid real names!</title><link>https://rstories.netlify.app/2024/05/17/applications-of-randomnames-an-r-package-avoid-real-names/</link><pubDate>Fri, 17 May 2024 00:00:00 +0000</pubDate><guid>https://rstories.netlify.app/2024/05/17/applications-of-randomnames-an-r-package-avoid-real-names/</guid><description>
&lt;div id="why-avoid-real-names" class="section level2">
&lt;h2>Why avoid real names?&lt;/h2>
&lt;p>Using real names (or other personal identities) in research or related activities such as publishing on the web or in journals, presentations, and/or demonstrations can be problematic due to privacy concerns, existing data protection laws and regulations, and ethical obligations. Examples of highly sensitive data include student academic records, health records, and financial records.&lt;/p>
&lt;p>To ensure the safety and privacy of all human subjects, many universities and other institutions establish oversight committees known as Institutional Review Boards (IRBs). The board is tasked with minimizing risk to participants. When using data collected from institutional participants, users are required to sign their proposals on the usage of the data, which, among other things, is required to include a declaration to use anonymous or pseudonymous names to avoid the use of real names.&lt;/p>
&lt;/div>
&lt;div id="the-randomnames-r-package" class="section level2">
&lt;h2>The ‘randomNames’ R-Package&lt;/h2>
&lt;p>One of my favorite R Packages is the ‘randomNames’ package which is actually simple and easy to use. It has a single function allowing users to generate random first and last names.&lt;/p>
&lt;p>Here are a few simple usage examples.&lt;/p>
&lt;p>Generate 6 random names as “Last, First”&lt;/p>
&lt;pre class="r">&lt;code>library(randomNames)
randomNames(6) &lt;/code>&lt;/pre>
&lt;pre>&lt;code>## [1] &amp;quot;Smith, Heather&amp;quot; &amp;quot;Cao, Pauline Cathrina&amp;quot; &amp;quot;Del Rosario, Emily&amp;quot;
## [4] &amp;quot;Le, Rachael&amp;quot; &amp;quot;Mahooty, Deja&amp;quot; &amp;quot;el-Muhammad, Shaakir&amp;quot;&lt;/code>&lt;/pre>
&lt;p>Generate 6 random female names as “Last, First”&lt;/p>
&lt;pre class="r">&lt;code>randomNames(6, gender = 1) &lt;/code>&lt;/pre>
&lt;pre>&lt;code>## [1] &amp;quot;al-Malak, Umaira&amp;quot; &amp;quot;Nguyen, Annabelle&amp;quot; &amp;quot;Ives, Hannah&amp;quot;
## [4] &amp;quot;al-Fahs, Anbara&amp;quot; &amp;quot;Green, Genesis&amp;quot; &amp;quot;Abraham, Jenna&amp;quot;&lt;/code>&lt;/pre>
&lt;p>Generate 6 random names with half of them male names as “Last, First”&lt;/p>
&lt;pre class="r">&lt;code>randomNames(6, gender = c(0,0,0,1,1,1)) &lt;/code>&lt;/pre>
&lt;pre>&lt;code>## [1] &amp;quot;Trevino, Austin&amp;quot; &amp;quot;Wanninger, Drake&amp;quot; &amp;quot;Cherrington, Larry&amp;quot;
## [4] &amp;quot;Lambouths, Shadenia&amp;quot; &amp;quot;Jackson, Joy&amp;quot; &amp;quot;Hoots, Emily&amp;quot;&lt;/code>&lt;/pre>
&lt;p>Generate 6 random names with three of them African Americans and the other three Whites (not Hispanic) as “Last, First”&lt;/p>
&lt;pre class="r">&lt;code>randomNames(6, ethnicity = c(3,3,3,5,5,5)) &lt;/code>&lt;/pre>
&lt;pre>&lt;code>## [1] &amp;quot;Thurman, Kevin&amp;quot; &amp;quot;Buice, Alexus&amp;quot; &amp;quot;Crowder, Emmanuelle&amp;quot;
## [4] &amp;quot;Yacovetta, Bailey&amp;quot; &amp;quot;Maloy, Kyle&amp;quot; &amp;quot;Bourbeau, Tyler&amp;quot;&lt;/code>&lt;/pre>
&lt;/div>
&lt;div id="more-example" class="section level2">
&lt;h2>More example&lt;/h2>
&lt;p>Below is an example of a file which is publicly available by X-University. It is a schedule of classes at a fall semester generated by a software by a company called Ellucian. The data has variable names such as enrollment number (ENRLD), instructor name (INSTRUCTOR), credit hours (HRS), etc, for schedule of classes. Many higher education institutions use the same software to generate class schedules hence have similar outputs.
Here you can find some examples of similar outputs: &lt;a href="https://simba.savannahstate.edu/students/schedule.aspx?term=202402">Savannah State University Spring 2024 Class Schedule&lt;/a>, &lt;a href="https://infostudent.fvsu.edu/Schedule.aspx">Fort Valley State University Class Schedule&lt;/a>, &lt;a href="https://benedict.edu/fall-2022-course-offerings/">Benedict College Class Schedule Fall 2022&lt;/a>.Some of them allow users to download the schedules as an excel or csv file. One can also use cut/paste to use the data.&lt;/p>
&lt;p>For the purpose of this demonstration, below is a schedule of spring 2024 classes by X-University; and we want to change instructor names in the schedule by random names.&lt;/p>
&lt;pre class="r">&lt;code>library(dplyr)
schedule&amp;lt;-read.csv(&amp;quot;X-College_Spring_2024_Classes_Schedule.csv&amp;quot;)
schedule&amp;lt;-subset(schedule,INSTRUCTOR !=&amp;quot;&amp;quot;) #skip classes with no instructor
schedule&amp;lt;-filter(schedule, P.of.T==1) #Session 1 Classes only
head(schedule)&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## P.of.T SUBJ NUMB TITLE HRS ENRLD MAXENRL TIMES
## 1 1 ACCT 2101 PRINCIPLES OF ACCOUNTING I 3 28 30
## 2 1 ACCT 2101 PRINCIPLES OF ACCOUNTING I 3 30 30 11:00-11:50
## 3 1 ACCT 2102 PRINCIPLES OF ACCOUNTING II 3 16 30
## 4 1 ACCT 2102 PRINCIPLES OF ACCOUNTING II 3 15 30 12:30-01:45
## 5 1 ACCT 3103 INTERM ACCOUNTING I 3 7 30 03:30-04:45
## 6 1 ACCT 4123 COST ACCOUNTING 3 9 30 05:15-06:30
## DAYS INSTRUCTOR
## 1 Haile, Brandie
## 2 M W F Lopez, Elizabeth
## 3 Haile, Brandie
## 4 T R Lopez, Elizabeth
## 5 T R Haile, Brandie
## 6 M W el-Hassan, Nawaar&lt;/code>&lt;/pre>
&lt;p>Instructor names show under the variable ‘INSTRUCTOR’ and we want to change them by random names while keeping everything else in the data unchanged.&lt;/p>
&lt;pre class="r">&lt;code>original_names&amp;lt;-unique(schedule$INSTRUCTOR)
random_names&amp;lt;-randomNames(length(original_names)) #Random names to replace original names
for(i in 1:length(original_names))
{
schedule[schedule == original_names[i]]&amp;lt;-random_names[i]
}
head(schedule)&lt;/code>&lt;/pre>
&lt;pre>&lt;code>## P.of.T SUBJ NUMB TITLE HRS ENRLD MAXENRL TIMES
## 1 1 ACCT 2101 PRINCIPLES OF ACCOUNTING I 3 28 30
## 2 1 ACCT 2101 PRINCIPLES OF ACCOUNTING I 3 30 30 11:00-11:50
## 3 1 ACCT 2102 PRINCIPLES OF ACCOUNTING II 3 16 30
## 4 1 ACCT 2102 PRINCIPLES OF ACCOUNTING II 3 15 30 12:30-01:45
## 5 1 ACCT 3103 INTERM ACCOUNTING I 3 7 30 03:30-04:45
## 6 1 ACCT 4123 COST ACCOUNTING 3 9 30 05:15-06:30
## DAYS INSTRUCTOR
## 1 Doan, Reyana
## 2 M W F Hai, Kanoa
## 3 Doan, Reyana
## 4 T R Hai, Kanoa
## 5 T R Doan, Reyana
## 6 M W Daniels, Courtney&lt;/code>&lt;/pre>
&lt;p>There we have it, all instructor names under “INSTRUCTOR” in the data are replaced by random names!&lt;/p>
&lt;/div>
&lt;div id="references" class="section level2">
&lt;h2>References&lt;/h2>
&lt;p>&lt;code>[1]&lt;/code> randomNames package was created by &lt;a href="https://github.com/dbetebenner">Damian Betebenner&lt;/a> and the repository is available in GitHub at &lt;a href="https://github.com/CenterForAssessment/randomNames/">randomNames (version 1.6-0.0)&lt;/a>.&lt;/p>
&lt;/div></description></item><item><title>Periodic and Average Periodic Returns of Financial Portfolios using R</title><link>https://rstories.netlify.app/2020-09-20-r-rmarkdown/</link><pubDate>Mon, 21 Sep 2020 00:00:00 +0000</pubDate><guid>https://rstories.netlify.app/2020-09-20-r-rmarkdown/</guid><description>
&lt;script src="https://rstories.netlify.app/rmarkdown-libs/kePrint/kePrint.js">&lt;/script>
&lt;link href="https://rstories.netlify.app/rmarkdown-libs/lightable/lightable.css" rel="stylesheet" />
&lt;div id="introduction" class="section level2">
&lt;h2>Introduction&lt;/h2>
&lt;p>Institutional and Hedge Fund managers return from summer vacation and adjust their financial portfolios at the end of each summer, causing a selloff pressure in the market. September is considered a bad month for the Bulls [1]&lt;/p>
&lt;p>“Given that September tends to be a bad month for the market, I’m urging you to be prepared …” Jim Cramer on CNBC, September 11, 2020.&lt;/p>
&lt;p>For this and other reasons, such as election hypes, pandemic, etc., people with trading, investing or retirement portfolios may want to know how their financial portfolios (or instruments such as stocks or ETFs in their portfolios) performed over some years, months, weeks or days. They may also want to know the average monthly, yearly, weekly or daily returns, starting from some fixed time of start in the past to the present or recent time.&lt;/p>
&lt;p>Almost all portfolio managers measure performance with reference to a benchmark [3].
In this short note, we will consider the historical data of the Standard and Poor’s 500 Index (S&amp;amp;P 500, symbol=^GSPC) from Yahoo! Finance, which is widely regarded as the best gauge of large-cap U.S. equities. Other well known benchmarks include DOW-30, NASDAQ-100, and the Russell 2000 Index for small-caps.&lt;/p>
&lt;p>We will then outline a simple way to visualize or summarize monthly returns as well as average monthly returns using R. Interested readers can modify the instrument, period and length of time to their preference.&lt;/p>
&lt;p>We start by installing the R packages that will be needed to produce libraries later. For more information about one of the key packages used here, the &lt;code>tidyquant&lt;/code> package, see [2].&lt;/p>
&lt;pre class="r">&lt;code>## Load Packages for the libraries that will be needed
##install.packages(c(&amp;quot;tidyquant&amp;quot;,&amp;quot;ggplot2&amp;quot;,&amp;quot;RColorBrewer&amp;quot;,&amp;quot;kableExtra&amp;quot;))
&lt;/code>&lt;/pre>
&lt;/div>
&lt;div id="getting-and-preparing-data" class="section level2">
&lt;h2>Getting and Preparing Data&lt;/h2>
&lt;p>We will get the data for the S&amp;amp;P 500 Index, symbol = ^GSPC, from Yahoo! Finance. We will then prepare the data for visualization and/or Summarization of results as needed.&lt;/p>
&lt;pre class="r">&lt;code>##Get data
library(tidyquant)
library(timetk)
symbol &amp;lt;- tq_get(&amp;quot;^GSPC&amp;quot;,from = &amp;quot;1927-12-01&amp;quot;, to = &amp;quot;2020-12-31&amp;quot;, get = &amp;quot;stock.prices&amp;quot;)
symbolname&amp;lt;-&amp;quot;^GSPC&amp;quot; #we need this for reproducible labels of our plot outputs.
##Create a tibble, tb, for ^GSPC Monthly Returns.
tb&amp;lt;-tq_transmute(data=symbol, select = adjusted,mutate_fun = periodReturn, period = &amp;quot;monthly&amp;quot;,col_rename = &amp;quot;Return&amp;quot;)&lt;/code>&lt;/pre>
&lt;p>This &lt;code>tibble&lt;/code> has 1114 rows and 2 columns and you can view the head of the data in any format you wish.&lt;/p>
&lt;pre class="r">&lt;code>library(kableExtra)
head(tb) %&amp;gt;%
kbl(caption = &amp;quot;Monthly Returns&amp;quot;) %&amp;gt;%
kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;) %&amp;gt;% kable_styling()&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic table" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-3">Table 1: &lt;/span>Monthly Returns
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
date
&lt;/th>
&lt;th style="text-align:right;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
1927-12-30
&lt;/td>
&lt;td style="text-align:right;">
0.0000000
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-01-31
&lt;/td>
&lt;td style="text-align:right;">
-0.0050963
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-02-29
&lt;/td>
&lt;td style="text-align:right;">
-0.0176437
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-03-30
&lt;/td>
&lt;td style="text-align:right;">
0.1170337
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-04-30
&lt;/td>
&lt;td style="text-align:right;">
0.0243775
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-05-31
&lt;/td>
&lt;td style="text-align:right;">
0.0126582
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>To make our work a bit easier, we create new columns of &lt;code>Month&lt;/code> and &lt;code>Year&lt;/code> from the &lt;code>date&lt;/code> column of &lt;code>tb&lt;/code> and select only the columns we want in the order of our desire. In addition to returns of each month by year, we will be interested on the average (mean) monthly returns. To that end, we will create new rows for the average monthly returns from the beginning of the data (1927) to the present year (2020).&lt;/p>
&lt;pre class="r">&lt;code>## Create new Year and Month Columns
tb$Year&amp;lt;-format(as.Date(tb$date), format = &amp;quot;%Y&amp;quot;)
tb$Month&amp;lt;-format(as.Date(tb$date), format =&amp;quot;%b&amp;quot;)
tb$Month = factor(tb$Month, levels = month.abb) #lists abbreviated months in chronological order when plotting
## Select only the columns we need
library(dplyr)
tb&amp;lt;-select(tb, 3,4,2)
## include mean return in the plots (in adition to monthly returns).
agg = aggregate(tb$Return,by = list(month=tb$Month),FUN = mean)
agg$Year&amp;lt;-&amp;quot;Average Return \n since 1927&amp;quot;
colnames(agg) &amp;lt;- c(&amp;quot;Month&amp;quot;, &amp;quot;Return&amp;quot;, &amp;quot;Year&amp;quot;)
agg&amp;lt;-select(agg, 3,1,2)
tb&amp;lt;-rbind(tb,agg)
head(tb)%&amp;gt;%
kbl(caption = &amp;quot;Monthly Returns and Average Monthly Returns&amp;quot;) %&amp;gt;%
kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;) %&amp;gt;% kable_styling()&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic table" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-4">Table 2: &lt;/span>Monthly Returns and Average Monthly Returns
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
Year
&lt;/th>
&lt;th style="text-align:left;">
Month
&lt;/th>
&lt;th style="text-align:right;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
1927
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
0.0000000
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
-0.0050963
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
-0.0176437
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
0.1170337
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.0243775
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
0.0126582
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The last 12 rows contain the average (mean) monthly returns from the start date of the data to the present year, preceded by the monthly returns of the most recent years. Since this note is written in September of 2020, the 2020 data is only for 9 months at this writing.&lt;/p>
&lt;pre class="r">&lt;code>tail(tb,n=34) %&amp;gt;%
kbl(caption = &amp;quot;Monthly Returns and Average Monthly Returns&amp;quot;) %&amp;gt;%
kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;)&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-5">Table 3: &lt;/span>Monthly Returns and Average Monthly Returns
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
Year
&lt;/th>
&lt;th style="text-align:left;">
Month
&lt;/th>
&lt;th style="text-align:right;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
2018
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
-0.0917769
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
0.0786844
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
0.0297289
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
0.0179243
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.0393135
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
-0.0657777
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Jun
&lt;/td>
&lt;td style="text-align:right;">
0.0689302
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Jul
&lt;/td>
&lt;td style="text-align:right;">
0.0131282
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Aug
&lt;/td>
&lt;td style="text-align:right;">
-0.0180916
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Sep
&lt;/td>
&lt;td style="text-align:right;">
0.0171812
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Oct
&lt;/td>
&lt;td style="text-align:right;">
0.0204318
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Nov
&lt;/td>
&lt;td style="text-align:right;">
0.0340470
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
0.0285898
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
-0.0016281
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
-0.0841105
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
-0.1251193
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.1268440
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
0.0452818
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Jun
&lt;/td>
&lt;td style="text-align:right;">
0.0183884
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Jul
&lt;/td>
&lt;td style="text-align:right;">
0.0551013
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Aug
&lt;/td>
&lt;td style="text-align:right;">
0.0700647
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Sep
&lt;/td>
&lt;td style="text-align:right;">
-0.0724850
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
0.0123258
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
-0.0011168
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
0.0041122
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.0140813
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
-0.0004602
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Jun
&lt;/td>
&lt;td style="text-align:right;">
0.0075405
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Jul
&lt;/td>
&lt;td style="text-align:right;">
0.0159214
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Aug
&lt;/td>
&lt;td style="text-align:right;">
0.0070290
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Sep
&lt;/td>
&lt;td style="text-align:right;">
-0.0106879
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Oct
&lt;/td>
&lt;td style="text-align:right;">
0.0046096
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Nov
&lt;/td>
&lt;td style="text-align:right;">
0.0074612
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
0.0129007
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;div id="visualizing-the-tidy-data" class="section level2">
&lt;h2>Visualizing the tidy data&lt;/h2>
&lt;p>We can now visualize the data to our liking.
A column plot (bar plot) of monthly returns during the most recent five years (four years and nine months since this note was written in September) with a plot of Average Monthly Return (since 1927) of each month at the bottom may be done as follows.&lt;/p>
&lt;pre class="r">&lt;code>## Plot using ggplot2
library(ggplot2)
library(scales)
g&amp;lt;-ggplot(data=tb[(length(tb$Return)-(4*12+9)+1):length(tb$Return),], aes(x=Month, y=Return))
g&amp;lt;-g+geom_col(aes(fill = Month), position = &amp;quot;dodge&amp;quot;)
g&amp;lt;-g+facet_grid(rows = vars(Year))
g&amp;lt;-g+labs(title=paste(&amp;quot;Monthly % Returns of&amp;quot;, symbolname),subtitle=&amp;quot;With % return stamped on top/bottom of each bar&amp;quot;)
g&amp;lt;-g+geom_text(aes(label = paste(round(Return*100,1), &amp;quot;%&amp;quot;), vjust = ifelse(Return &amp;gt;= 0, -0.1, 1.1)), size=3.5)
g&amp;lt;-g+scale_y_continuous(&amp;quot;Returns in Percentage&amp;quot;, labels = percent_format(),expand = expansion(mult = c(0.2, 0.2)))
g&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://rstories.netlify.app/2020-09-20-r-rmarkdown_files/figure-html/unnamed-chunk-6-1.png" width="864" />&lt;/p>
&lt;p>If we are interested in a separate plot for the Average (mean) Monthly Return of each month (from 1927 to the present day), we can select the last 12 rows of &lt;code>tb&lt;/code> and use the same code. We also need to adjust the title and labels of the axes.&lt;/p>
&lt;pre class="r">&lt;code>## Plot using ggplot2
library(ggplot2)
library(scales)
g&amp;lt;-ggplot(data=tb[(length(tb$Return)-12+1):length(tb$Return),], aes(x=Month, y=Return))
g&amp;lt;-g+geom_col(aes(fill = Month), position = &amp;quot;dodge&amp;quot;)
g&amp;lt;-g+facet_grid(rows = vars(Year))
g&amp;lt;-g+labs(title=paste(&amp;quot;Average Monthly Return of&amp;quot;, symbolname),subtitle=&amp;quot;With mean %age return since 1927 stamped on top/bottom of each bar&amp;quot;)
g&amp;lt;-g+geom_text(aes(label = paste(round(Return*100,1), &amp;quot;%&amp;quot;), vjust = ifelse(Return &amp;gt;= 0, -0.1, 1.1)), size=3.5)
g&amp;lt;-g+scale_y_continuous(&amp;quot;Average (Mean) Monthly Return&amp;quot;, labels = percent_format(),expand = expansion(mult = c(0.2, 0.2)))
g&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://rstories.netlify.app/2020-09-20-r-rmarkdown_files/figure-html/unnamed-chunk-7-1.png" width="672" />&lt;/p>
&lt;/div>
&lt;div id="summarizing-other-important-tales" class="section level2">
&lt;h2>Summarizing other important tales&lt;/h2>
&lt;p>There were several interesting market events in history. Interested readers may use codes and data to get summary of results in the format of their liking.
For example, if we are interested in the list of the fifteen worst days of the S&amp;amp;P 500 Index, we can run the following chunk.&lt;/p>
&lt;pre class="r">&lt;code>symbol2 &amp;lt;- tq_get(&amp;quot;^GSPC&amp;quot;,from = &amp;quot;1928-01-01&amp;quot;, to = &amp;quot;2020-12-31&amp;quot;, get = &amp;quot;stock.prices&amp;quot;)
tb2&amp;lt;-tq_transmute(data=symbol2, select = adjusted,mutate_fun = periodReturn, period = &amp;quot;daily&amp;quot;,col_rename = &amp;quot;Return&amp;quot;)
tb2&amp;lt;-tb2[order(tb2$Return,decreasing = FALSE),]
tb2$Return&amp;lt;-paste(round(100*(tb2$Return),1),&amp;quot;%&amp;quot;)
head(tb2, n=15) %&amp;gt;% kbl(caption = &amp;quot;Worst historical days of market&amp;quot;) %&amp;gt;% kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;)&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-8">Table 4: &lt;/span>Worst historical days of market
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
date
&lt;/th>
&lt;th style="text-align:left;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
1987-10-19
&lt;/td>
&lt;td style="text-align:left;">
-20.5 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1929-10-28
&lt;/td>
&lt;td style="text-align:left;">
-12.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020-03-16
&lt;/td>
&lt;td style="text-align:left;">
-12 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1929-10-29
&lt;/td>
&lt;td style="text-align:left;">
-10.2 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1935-04-16
&lt;/td>
&lt;td style="text-align:left;">
-10 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1929-11-06
&lt;/td>
&lt;td style="text-align:left;">
-9.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1946-09-03
&lt;/td>
&lt;td style="text-align:left;">
-9.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020-03-12
&lt;/td>
&lt;td style="text-align:left;">
-9.5 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1937-10-18
&lt;/td>
&lt;td style="text-align:left;">
-9.1 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1931-10-05
&lt;/td>
&lt;td style="text-align:left;">
-9.1 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2008-10-15
&lt;/td>
&lt;td style="text-align:left;">
-9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2008-12-01
&lt;/td>
&lt;td style="text-align:left;">
-8.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1933-07-20
&lt;/td>
&lt;td style="text-align:left;">
-8.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2008-09-29
&lt;/td>
&lt;td style="text-align:left;">
-8.8 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1933-07-21
&lt;/td>
&lt;td style="text-align:left;">
-8.7 %
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Readers who are curious to know what happened during those historical days may consult the literature. For example, the infamous day &lt;code>1987-10-19&lt;/code> happens to be what is known in market history as the Black Monday; the crashes in October of 1929 signaled the beginning of the Great Depression, see e.g. [4].&lt;/p>
&lt;p>Readers interested in similar or more interesting results that may be check using (R-) codes may consult Hirsch’s book [1].&lt;/p>
&lt;/div>
&lt;div id="references" class="section level2">
&lt;h2>References&lt;/h2>
&lt;p>&lt;code>[1]&lt;/code> Jeffrey A. Hirsch, &lt;em>Stock Trader’s Almanac 2020 (Almanac Investor Series),&lt;/em> 16th Edition, ISBN-13: 978-1119596295.&lt;/p>
&lt;p>&lt;a href="https://cran.r-project.org/web/packages/tidyquant/tidyquant.pdf">&lt;code>[2]&lt;/code> M. Dancho &amp;amp; D. Vaughan, &lt;em>Tidy Quantitative Financial Analysis,&lt;/em> The Comprehensive R Archive Network (CRAN), July 2, 2020.&lt;/a>&lt;/p>
&lt;p>&lt;a href="https://journal.r-project.org/archive/2013-2/lu-kane.pdf">&lt;code>[3]&lt;/code> Y. Lu and D. Kane, &lt;em>Performance Attribution for Equity Portfolios,&lt;/em> The R Journal, Vol. 5/2, December 2013&lt;/a>&lt;/p>
&lt;p>&lt;code>[4]&lt;/code> S. Nations, C. Grove, et al., &lt;em>A History of the United States in Five Crashes: Stock Market Meltdowns That
Defined a Nation, &lt;/em>William Morrow (Publisher); 1st Edition, June 13, 2017.&lt;/p>
&lt;/div></description></item><item><title>Elementary Mathematics using R</title><link>https://rstories.netlify.app/2020/09/20/elementary-mathematics-using-r/</link><pubDate>Sun, 20 Sep 2020 00:00:00 +0000</pubDate><guid>https://rstories.netlify.app/2020/09/20/elementary-mathematics-using-r/</guid><description>
&lt;div id="the-basics" class="section level2">
&lt;h2>The basics&lt;/h2>
&lt;p>This is a compilation of basic codes for doing elementary mathematics (college algebra, calculus)&lt;/p>
&lt;p>The best software for doing mathematics may be Mathematica, Maple, MatLab, Sage and other software. However, you can also do some math using R with the convenience of RStudio and R Markdown.&lt;/p>
&lt;pre class="r">&lt;code># install.packages(&amp;quot;mosaic&amp;quot;, &amp;quot;mosaicCalc&amp;quot;, &amp;quot;calculus&amp;quot;)&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>## Required libraries
library(mosaic)
library(mosaicCalc)
library(calculus)
library(&amp;quot;ggplot2&amp;quot;)&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>
## Given g(x)=3*x^2 -2*x +1, find g(2) ##base R, that is,no extra package is used.
g &amp;lt;- function(x){3*x^2 -2*x +1}
g(2)
## [1] 9&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>
## Given g(x)=3*x^2 -2*x +1, find g(2) ##mosaic
g&amp;lt;-makeFun(3*x^2 -2*x +1 ~ x)
g(2)
## [1] 9&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>
##Plot the function y=3x^2-x-2 on the interval [-10, 10] ## base R
curve(3*x^2 -2*x +1, -10, 10)&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://rstories.netlify.app/post/math_files/figure-html/unnamed-chunk-5-1.png" width="672" />&lt;/p>
&lt;pre class="r">&lt;code>
## Plot the function y=3x^2-x-2 on the interval [-10, 10] ##moasaic
plotFun(3*x^2 -2*x +1 ~x, x.lim=range(-10,10) )&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://rstories.netlify.app/post/math_files/figure-html/unnamed-chunk-6-1.png" width="672" />&lt;/p>
&lt;pre class="r">&lt;code>
## Plot the function y=3x^2-x-2 on the interval [-10, 10]. ##ggplot2
eq = function(x){3*x^2 -2*x +1}
g&amp;lt;-ggplot(data.frame(x=c(-10, 10)), aes(x=x))
g&amp;lt;-g+ stat_function(fun=eq)
g&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://rstories.netlify.app/post/math_files/figure-html/unnamed-chunk-7-1.png" width="672" />&lt;/p>
&lt;pre class="r">&lt;code>
## Solve the equation x^2 +x -6=0 for x.
findZeros(x^2 +x -6 ~ x, x.lim=range(-Inf, Inf))
## x
## 1 -3
## 2 2&lt;/code>&lt;/pre>
&lt;pre recho="T," message="F," warning="F">&lt;code>
## Solve a system of linear equatiioms
## solve the system: 2x + 3y = 5
## 5x - y = 7 for x and y.
A&amp;lt;-matrix(c(2,3,5,-1), nrow=2, ncol=2, byrow=TRUE) ## see ?matrix
b&amp;lt;-matrix(c(5,7), nrow=2, ncol=1, byrow=TRUE)
solve(A,b)
&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>## Find the derivative of the function f(x)= 2x^3+3x^2 +2x -5sin^2(x) +ln(x) ##mosaicCal
D(2*x^3 + 3*x^2 +2*x - 5*sin(x)^2 + log(x) ~ x)
## function (x)
## 2 * (3 * x^2) + 3 * (2 * x) + 2 - 5 * (2 * (cos(x) * sin(x))) +
## 1/x&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>## Find the derivative of the function f(x)= 2x^3+3x^2 +2x -5sin^2(x) +ln(x) ## &amp;quot;calculus&amp;quot; package
derivative(f = &amp;quot;2*x^3 + 3*x^2 +2*x - 5*sin(x)^2 + log(x)&amp;quot;, var = &amp;quot;x&amp;quot;)
## [,1]
## 2*x^3 + 3*x^2 +2*x - 5*sin(x)^2 + log(x) &amp;quot;2 * (3 * x^2) + 3 * (2 * x) + 2 - 5 * (2 * (cos(x) * sin(x))) + 1/x&amp;quot;&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>
## Given F(x)= 3*x^2 - 2*x +1, find F&amp;#39;(2) (derivative of F at x=2) ##calculus
derivative(f = &amp;quot;3*x^2 - 2*x +1&amp;quot;, var = c(&amp;quot;x&amp;quot; = 2))
## [,1]
## [1,] 10&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>
## Given F(x)= 3*x^2 - 2*x +1, find F&amp;#39;(2) (derivative of F at x=2) ##mosaicCal
g&amp;lt;- D(3*x^2 -2*x + 1 ~ x)
g(2)
## [1] 10&lt;/code>&lt;/pre>
&lt;pre class="r">&lt;code>
## Evaluate the definite integral \int_{-1}^{2}(x^2-2x+1)dx ##mosaicCal
F&amp;lt;-antiD(x^2-2*x+1 ~ x)
F(2)-F(-1)
## [1] 3&lt;/code>&lt;/pre>
&lt;p>(DRAFT=TRUE; will be updated)&lt;/p>
&lt;/div></description></item><item><title>Periodic and Average Periodic Returns of Financial Portfolios using R</title><link>https://rstories.netlify.app/2020/09/20/periodic-and-average-periodic-returns-of-financial-portfolios-using-r/</link><pubDate>Sun, 20 Sep 2020 00:00:00 +0000</pubDate><guid>https://rstories.netlify.app/2020/09/20/periodic-and-average-periodic-returns-of-financial-portfolios-using-r/</guid><description>
&lt;script src="https://rstories.netlify.app/rmarkdown-libs/kePrint/kePrint.js">&lt;/script>
&lt;link href="https://rstories.netlify.app/rmarkdown-libs/lightable/lightable.css" rel="stylesheet" />
&lt;div id="introduction" class="section level2">
&lt;h2>Introduction&lt;/h2>
&lt;p>Institutional and Hedge Fund managers return from summer vacation and adjust their financial portfolios at the end of each summer, causing a selloff pressure in the market. September is considered a bad month for the Bulls [1]&lt;/p>
&lt;p>“Given that September tends to be a bad month for the market, I’m urging you to be prepared …” Jim Cramer on CNBC, September 11, 2020.&lt;/p>
&lt;p>For this and other reasons, such as election, pandemic, etc., people with trading, investing or retirement portfolios may want to know how their financial portfolios (or instruments such as stocks or ETFs in their portfolios) performed over some years, months, weeks or days. They may also want to know the average (mean) monthly, yearly, weekly or daily returns, starting from some fixed time of start in the past to the present or recent time.&lt;/p>
&lt;p>Almost all portfolio managers measure performance with reference to a benchmark [3].
In this short note, we will consider the historical data of the Standard and Poor’s 500 Index (S&amp;amp;P 500, symbol=^GSPC) from Yahoo! Finance, which is widely regarded as the best gauge of large-cap U.S. equities. Other well known benchmarks include DOW-30, NASDAQ-100, and the Russell 2000 Index for small-caps.&lt;/p>
&lt;p>We will then outline a simple way to visualize or summarize monthly returns as well as average monthly returns using R. Interested readers can modify the instrument, period and length of time to their preference.&lt;/p>
&lt;p>We start by installing the R packages that will be needed to produce libraries later. For more information about one of the key packages used here, the &lt;code>tidyquant&lt;/code> package, see [2].&lt;/p>
&lt;pre class="r">&lt;code>## Load Packages for the libraries that will be needed
##install.packages(c(&amp;quot;tidyquant&amp;quot;,&amp;quot;ggplot2&amp;quot;,&amp;quot;RColorBrewer&amp;quot;,&amp;quot;kableExtra&amp;quot;))
&lt;/code>&lt;/pre>
&lt;/div>
&lt;div id="getting-and-preparing-data" class="section level2">
&lt;h2>Getting and Preparing Data&lt;/h2>
&lt;p>We will get the data for the S&amp;amp;P 500 Index, symbol = ^GSPC, from Yahoo! Finance. We will then prepare the data for visualization and/or summarization of results as needed.&lt;/p>
&lt;pre class="r">&lt;code>##Get data
library(tidyquant)
library(timetk)
symbol &amp;lt;- tq_get(&amp;quot;^GSPC&amp;quot;,from = &amp;quot;1927-12-01&amp;quot;, to = &amp;quot;2020-12-31&amp;quot;, get = &amp;quot;stock.prices&amp;quot;)
symbolname&amp;lt;-&amp;quot;^GSPC&amp;quot; #we need this for reproducible labels of our plot outputs.
##Create a tibble, tb, for ^GSPC Monthly Returns.
tb&amp;lt;-tq_transmute(data=symbol, select = adjusted,mutate_fun = periodReturn, period = &amp;quot;monthly&amp;quot;,col_rename = &amp;quot;Return&amp;quot;)&lt;/code>&lt;/pre>
&lt;p>This &lt;code>tibble&lt;/code> has 1114 rows and 2 columns and you can view the head of the data in any format you wish.&lt;/p>
&lt;pre class="r">&lt;code>library(kableExtra)
head(tb) %&amp;gt;%
kbl(caption = &amp;quot;Monthly Returns&amp;quot;) %&amp;gt;%
kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;) %&amp;gt;% kable_styling()&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic table" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-3">Table 1: &lt;/span>Monthly Returns
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
date
&lt;/th>
&lt;th style="text-align:right;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
1927-12-30
&lt;/td>
&lt;td style="text-align:right;">
0.0000000
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-01-31
&lt;/td>
&lt;td style="text-align:right;">
-0.0050963
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-02-29
&lt;/td>
&lt;td style="text-align:right;">
-0.0176437
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-03-30
&lt;/td>
&lt;td style="text-align:right;">
0.1170337
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-04-30
&lt;/td>
&lt;td style="text-align:right;">
0.0243775
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928-05-31
&lt;/td>
&lt;td style="text-align:right;">
0.0126582
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>To make our work a bit easier, we create new columns of &lt;code>Month&lt;/code> and &lt;code>Year&lt;/code> from the &lt;code>date&lt;/code> column of &lt;code>tb&lt;/code> and select only the columns we want in the order of our desire. In addition to returns of each month by year, we will be interested on the average (mean) monthly returns. To that end, we will create new rows for the average monthly returns from the beginning of the data (1927) to the present year (2020).&lt;/p>
&lt;pre class="r">&lt;code>## Create new Year and Month Columns
tb$Year&amp;lt;-format(as.Date(tb$date), format = &amp;quot;%Y&amp;quot;)
tb$Month&amp;lt;-format(as.Date(tb$date), format =&amp;quot;%b&amp;quot;)
tb$Month = factor(tb$Month, levels = month.abb) #lists abbreviated months in chronological order when plotting
## Select only the columns we need
library(dplyr)
tb&amp;lt;-select(tb, 3,4,2)
## include a row of average monthly return for each month (in adition to monthly returns since 1927).
agg = aggregate(tb$Return,by = list(month=tb$Month),FUN = mean)
agg$Year&amp;lt;-&amp;quot;Average Monthly Return \n since 1927&amp;quot;
colnames(agg) &amp;lt;- c(&amp;quot;Month&amp;quot;, &amp;quot;Return&amp;quot;, &amp;quot;Year&amp;quot;)
agg&amp;lt;-select(agg, 3,1,2)
tb&amp;lt;-rbind(tb,agg)
head(tb)%&amp;gt;%
kbl(caption = &amp;quot;Monthly Returns and Average Monthly Returns&amp;quot;) %&amp;gt;%
kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;) %&amp;gt;% kable_styling()&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic table" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-4">Table 2: &lt;/span>Monthly Returns and Average Monthly Returns
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
Year
&lt;/th>
&lt;th style="text-align:left;">
Month
&lt;/th>
&lt;th style="text-align:right;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
1927
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
0.0000000
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
-0.0050963
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
-0.0176437
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
0.1170337
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.0243775
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1928
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
0.0126582
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The last 12 rows contain the average (mean) monthly returns from the start date of the data to the present year, preceded by the monthly returns of the most recent years. Since this note is written in September of 2020, the 2020 data was only for 9 months at this writing.&lt;/p>
&lt;pre class="r">&lt;code>tail(tb,n=34) %&amp;gt;%
kbl(caption = &amp;quot;Monthly Returns and Average Monthly Returns&amp;quot;) %&amp;gt;%
kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;)&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-5">Table 3: &lt;/span>Monthly Returns and Average Monthly Returns
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
Year
&lt;/th>
&lt;th style="text-align:left;">
Month
&lt;/th>
&lt;th style="text-align:right;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
2018
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
-0.0917769
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
0.0786844
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
0.0297289
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
0.0179243
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.0393135
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
-0.0657777
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Jun
&lt;/td>
&lt;td style="text-align:right;">
0.0689302
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Jul
&lt;/td>
&lt;td style="text-align:right;">
0.0131282
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Aug
&lt;/td>
&lt;td style="text-align:right;">
-0.0180916
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Sep
&lt;/td>
&lt;td style="text-align:right;">
0.0171812
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Oct
&lt;/td>
&lt;td style="text-align:right;">
0.0204318
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Nov
&lt;/td>
&lt;td style="text-align:right;">
0.0340470
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2019
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
0.0285898
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
-0.0016281
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
-0.0841105
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
-0.1251193
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.1268440
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
0.0452818
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Jun
&lt;/td>
&lt;td style="text-align:right;">
0.0183884
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Jul
&lt;/td>
&lt;td style="text-align:right;">
0.0551013
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Aug
&lt;/td>
&lt;td style="text-align:right;">
0.0700647
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020
&lt;/td>
&lt;td style="text-align:left;">
Sep
&lt;/td>
&lt;td style="text-align:right;">
-0.0576663
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Jan
&lt;/td>
&lt;td style="text-align:right;">
0.0123258
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Feb
&lt;/td>
&lt;td style="text-align:right;">
-0.0011168
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Mar
&lt;/td>
&lt;td style="text-align:right;">
0.0041122
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Apr
&lt;/td>
&lt;td style="text-align:right;">
0.0140813
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
May
&lt;/td>
&lt;td style="text-align:right;">
-0.0004602
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Jun
&lt;/td>
&lt;td style="text-align:right;">
0.0075405
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Jul
&lt;/td>
&lt;td style="text-align:right;">
0.0159214
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Aug
&lt;/td>
&lt;td style="text-align:right;">
0.0070290
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Sep
&lt;/td>
&lt;td style="text-align:right;">
-0.0105286
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Oct
&lt;/td>
&lt;td style="text-align:right;">
0.0046096
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Nov
&lt;/td>
&lt;td style="text-align:right;">
0.0074612
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
Average Monthly Return
since 1927
&lt;/td>
&lt;td style="text-align:left;">
Dec
&lt;/td>
&lt;td style="text-align:right;">
0.0129007
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;/div>
&lt;div id="visualizing-the-tidy-data" class="section level2">
&lt;h2>Visualizing the tidy data&lt;/h2>
&lt;p>We can now visualize the data to our liking.
For example, a column plot (bar plot) of monthly returns during the most recent five years (four years and nine months since this note was written in September) with a plot of average monthly return (since 1927) of each month at the bottom may be done as follows.&lt;/p>
&lt;pre class="r">&lt;code>## Plot using ggplot2
library(ggplot2)
library(scales)
g&amp;lt;-ggplot(data=tb[(length(tb$Return)-(6*12)+4):length(tb$Return),], aes(x=Month, y=Return))
g&amp;lt;-g+geom_col(aes(fill = Month), position = &amp;quot;dodge&amp;quot;)
g&amp;lt;-g+facet_grid(rows = vars(Year))
g&amp;lt;-g+labs(title=paste(&amp;quot;Monthly % Returns of&amp;quot;, symbolname),subtitle=&amp;quot;With % return stamped on top/bottom of each bar&amp;quot;)
g&amp;lt;-g+geom_text(aes(label = paste(round(Return*100,1), &amp;quot;%&amp;quot;), vjust = ifelse(Return &amp;gt;= 0, -0.1, 1.1)), size=3.5)
g&amp;lt;-g+scale_y_continuous(&amp;quot;Returns in Percentage&amp;quot;, labels = percent_format(),expand = expansion(mult = c(0.2, 0.2)))
g&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://rstories.netlify.app/post/2020-09-20-r-rmarkdown_files/figure-html/unnamed-chunk-6-1.png" width="864" />&lt;/p>
&lt;p>If we are interested in a separate plot for the average monthly return of each month (from 1927 to the present day), we can select the last 12 rows of &lt;code>tb&lt;/code> and use the same code. We also need to adjust the title and labels of the axes.&lt;/p>
&lt;pre class="r">&lt;code>## Plot using ggplot2
library(ggplot2)
library(scales)
g&amp;lt;-ggplot(data=tb[(length(tb$Return)-12+1):length(tb$Return),], aes(x=Month, y=Return))
g&amp;lt;-g+geom_col(aes(fill = Month), position = &amp;quot;dodge&amp;quot;)
g&amp;lt;-g+facet_grid(rows = vars(Year))
g&amp;lt;-g+labs(title=paste(&amp;quot;Average Monthly % Return of&amp;quot;, symbolname),subtitle=&amp;quot;With AMPR stamped on top/bottom of each bar (data since 1927)&amp;quot;)
g&amp;lt;-g+geom_text(aes(label = paste(round(Return*100,1), &amp;quot;%&amp;quot;), vjust = ifelse(Return &amp;gt;= 0, -0.1, 1.1)), size=3.5)
g&amp;lt;-g+scale_y_continuous(&amp;quot;Average (Mean) Monthly Return&amp;quot;, labels = percent_format(),expand = expansion(mult = c(0.2, 0.2)))
g&lt;/code>&lt;/pre>
&lt;p>&lt;img src="https://rstories.netlify.app/post/2020-09-20-r-rmarkdown_files/figure-html/unnamed-chunk-7-1.png" width="672" />&lt;/p>
&lt;/div>
&lt;div id="summarizing-other-interesting-tales" class="section level2">
&lt;h2>Summarizing other interesting tales&lt;/h2>
&lt;p>There were several interesting market events in history. Interested readers may use codes and data to get summary of results in the format of their liking.
For example, if we are interested in the list of the fifteen worst days of the S&amp;amp;P 500 Index, we can run the following chunk.&lt;/p>
&lt;pre class="r">&lt;code>symbol2 &amp;lt;- tq_get(&amp;quot;^GSPC&amp;quot;,from = &amp;quot;1927-01-01&amp;quot;, to = &amp;quot;2020-12-31&amp;quot;, get = &amp;quot;stock.prices&amp;quot;)
tb2&amp;lt;-tq_transmute(data=symbol2, select = adjusted,mutate_fun = periodReturn, period = &amp;quot;daily&amp;quot;,col_rename = &amp;quot;Return&amp;quot;)
tb2&amp;lt;-tb2[order(tb2$Return,decreasing = FALSE),]
tb2$Return&amp;lt;-paste(round(100*(tb2$Return),1),&amp;quot;%&amp;quot;)
head(tb2, n=15) %&amp;gt;% kbl(caption = &amp;quot;Worst historical days of market&amp;quot;) %&amp;gt;% kable_classic(full_width = F, html_font = &amp;quot;Cambria&amp;quot;)&lt;/code>&lt;/pre>
&lt;table class=" lightable-classic" style="font-family: Cambria; width: auto !important; margin-left: auto; margin-right: auto;">
&lt;caption>
&lt;span id="tab:unnamed-chunk-8">Table 4: &lt;/span>Worst historical days of market
&lt;/caption>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left;">
date
&lt;/th>
&lt;th style="text-align:left;">
Return
&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left;">
1987-10-19
&lt;/td>
&lt;td style="text-align:left;">
-20.5 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1929-10-28
&lt;/td>
&lt;td style="text-align:left;">
-12.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020-03-16
&lt;/td>
&lt;td style="text-align:left;">
-12 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1929-10-29
&lt;/td>
&lt;td style="text-align:left;">
-10.2 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1935-04-16
&lt;/td>
&lt;td style="text-align:left;">
-10 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1929-11-06
&lt;/td>
&lt;td style="text-align:left;">
-9.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1946-09-03
&lt;/td>
&lt;td style="text-align:left;">
-9.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2020-03-12
&lt;/td>
&lt;td style="text-align:left;">
-9.5 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1937-10-18
&lt;/td>
&lt;td style="text-align:left;">
-9.1 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1931-10-05
&lt;/td>
&lt;td style="text-align:left;">
-9.1 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2008-10-15
&lt;/td>
&lt;td style="text-align:left;">
-9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2008-12-01
&lt;/td>
&lt;td style="text-align:left;">
-8.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1933-07-20
&lt;/td>
&lt;td style="text-align:left;">
-8.9 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
2008-09-29
&lt;/td>
&lt;td style="text-align:left;">
-8.8 %
&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left;">
1933-07-21
&lt;/td>
&lt;td style="text-align:left;">
-8.7 %
&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Readers who are curious about those historical days may consult the literature. For example, the infamous day &lt;code>1987-10-19&lt;/code> happens to be what is known in market history as the Black Monday. The crashes in October of 1929 signaled the beginning of the Great Depression. See, e.g., [4].&lt;/p>
&lt;p>Readers interested in similar or more interesting results that may be checked using (R-) codes may consult Hirsch’s book [1].&lt;/p>
&lt;/div>
&lt;div id="references" class="section level2">
&lt;h2>References&lt;/h2>
&lt;p>&lt;code>[1]&lt;/code> Jeffrey A. Hirsch, &lt;em>Stock Trader’s Almanac 2020 (Almanac Investor Series),&lt;/em> 16th Edition, ISBN-13: 978-1119596295.&lt;/p>
&lt;p>&lt;a href="https://cran.r-project.org/web/packages/tidyquant/tidyquant.pdf">&lt;code>[2]&lt;/code> M. Dancho &amp;amp; D. Vaughan, &lt;em>Tidy Quantitative Financial Analysis,&lt;/em> The Comprehensive R Archive Network (CRAN), July 2, 2020.&lt;/a>&lt;/p>
&lt;p>&lt;a href="https://journal.r-project.org/archive/2013-2/lu-kane.pdf">&lt;code>[3]&lt;/code> Y. Lu and D. Kane, &lt;em>Performance Attribution for Equity Portfolios,&lt;/em> The R Journal, Vol. 5/2, December 2013&lt;/a>&lt;/p>
&lt;p>&lt;code>[4]&lt;/code> S. Nations, C. Grove, et al., &lt;em>A History of the United States in Five Crashes: Stock Market Meltdowns That
Defined a Nation, &lt;/em>William Morrow (Publisher); 1st Edition, June 13, 2017.&lt;/p>
&lt;/div></description></item><item><title>RMarkdown + Github +Hugo + Blogdown + Netlify: Develope your own blogging site</title><link>https://rstories.netlify.app/2020/07/19/rmarkdown-github-hugo-blogdown-netlify-develope-your-own-blogging-site/</link><pubDate>Sun, 19 Jul 2020 00:00:00 +0000</pubDate><guid>https://rstories.netlify.app/2020/07/19/rmarkdown-github-hugo-blogdown-netlify-develope-your-own-blogging-site/</guid><description>
&lt;div id="the-plan" class="section level1">
&lt;h1>The Plan&lt;/h1>
&lt;p>If you are interested in building your own website where you can sometimes blog and link your blogs with other famous blogging sites such as &lt;a href="https://www.r-bloggers.com/">R-bloggers&lt;/a> and &lt;a href="https://python-bloggers.com/">PYTHON-BLOGGERS&lt;/a>, you will be very familiar with Blogdown, Hugo Themes, Github and Netlify. If you, like me, are familiar with R/RStudio and a feature in RStudio called R Markdown, which is mostly used for type-setting your code loaded works with expected outputs in file extensions like .Rmd, .md, .html, .pdf and word, then you do not have to worry about the first step. What is next, and what are those names listed in the title?&lt;/p>
&lt;/div>
&lt;div id="open-a-github-account-and-create-your-first-ripository" class="section level1">
&lt;h1>Open a Github account and create your first ripository&lt;/h1>
&lt;p>If you did not yet, open a &lt;a href="https://github.com/">GitHub account&lt;/a>. You will then need to create your first repository and copy the link of the repository &lt;a href="https://docs.github.com/en/desktop/installing-and-configuring-github-desktop/creating-your-first-repository-using-github-desktop">(More on this?)&lt;/a>&lt;/p>
&lt;p>GitHub is a version control hub where smart people who work for big tech companies develop software packages and it can be intimidating, but you can get through it.&lt;/p>
&lt;/div>
&lt;div id="pick-your-favorite-theme" class="section level1">
&lt;h1>Pick your Favorite Theme&lt;/h1>
&lt;p>For beginners, the best theme choice for the first site is probably the &lt;a href="https://github.com/yihui/hugo-lithium.git">default one&lt;/a> but there are &lt;a href="https://themes.gohugo.io/">more than 200 themes&lt;/a> that you may go over and pick. You will need
the name of the theme. For example, the default them is `yihui/hugo-lithium’.&lt;/p>
&lt;/div>
&lt;div id="create-your-first-website-building-project." class="section level1">
&lt;h1>Create your first website building project.&lt;/h1>
&lt;p>My experience is that the best instruction is given via youtube videos as opposed to written instructions that you may find by googling, unless you have time to read the book: &lt;a href="https://bookdown.org/yihui/blogdown/a-quick-example.html">“blogdown: Creating Websites with R Markdown”&lt;/a>.&lt;/p>
&lt;p>I carefully followed the steps in &lt;a href="https://www.youtube.com/watch?v=OG_i6WzRvL4">this youtube video&lt;/a>, and found it helpful the first time. Problems will come when you start executing. As you try over several themes, your R crashes or freezes several times, your Git button will sometimes disappear from your Project/RStudio, just to mention a few of the problems that may try to discourage you from continuing. So, what are you going to do when these kinds of troubles with building your first website continue to surface? Quit?&lt;/p>
&lt;p>There are plenty of resourses that will help you complete your website. So, as I continue to write this, I will concentrate on the problems I encountered and resolved.&lt;/p>
&lt;/div></description></item><item><title>About</title><link>https://rstories.netlify.app/about/</link><pubDate>Thu, 05 May 2016 21:48:51 -0700</pubDate><guid>https://rstories.netlify.app/about/</guid><description>&lt;p>Welcome. For any questions, please email: &lt;a href="mailto:aberrad@fvsu.edu">aberrad@fvsu.edu&lt;/a>&lt;/p></description></item></channel></rss>