Stock-Data-Analysis-R

Stock Market Data Analysis & Visualization

A comprehensive R script that fetches daily adjusted closing prices for a selection of major tickers from Yahoo Finance and generates a suite of exploratory and analytical plots, including moving averages, returns, volatility, cumulative performance, and comparative normalized charts.

Author: David Nguyen

Table of Contents

  1. Project Overview
  2. Features & Plots
  3. Prerequisites
  4. Installation
  5. Usage
  6. Script Breakdown
  7. Selected Visualizations
  8. Interpreting the Outputs
  9. Extending & Customizing
  10. Data Source & Citations
  11. License

Project Overview

This repository provides a single R script (Stock_Market_Analysis.R) which:


Features & Plots

  1. Price Chart with SMAs
  2. Daily Log Returns Time Series
  3. Histogram of Daily Returns
  4. 20-Day Rolling Volatility
  5. Cumulative Returns
  6. Monthly Returns Boxplot
  7. Comparative Normalized Performance

Each ticker produces six individual plots, plus one comparative chart, for a total of 43 PNG files.


Prerequisites


Installation

  1. Clone this repository:
    git clone https://github.com/yourusername/stock-market-analysis.git
    cd stock-market-analysis
    
  2. Open R or RStudio in this folder.

Usage

In an R console or RStudio:

# Source the analysis script
source("Stock_Market_Analysis.R")

The script will:

  1. Install any missing packages.
  2. Download and process each ticker’s data.
  3. Display each plot interactively (with a white background).
  4. Save each plot as plot-1.png … plot-43.png in the project directory.
  5. Print summary statistics for each ticker’s returns.

Script Breakdown

  1. Setup

    • Defines and installs package list
    • Loads libraries
  2. Data Fetch & Preparation

    • Defines tickers & date range
    • Downloads via quantmod::getSymbols()
    • Extracts adjusted close, SMAs, returns, volatility, monthly returns
  3. Plotting Sections (per ticker)

    • Price + SMAs
    • Daily Returns
    • Histogram of Returns
    • Rolling Volatility
    • Cumulative Returns
    • Monthly Returns Boxplot
  4. Comparative Performance

    • Merges all adjusted series
    • Normalizes each to 100 at the first date
    • Plots on a single chart with legend
  5. Summary Statistics

    • Prints mean, SD, skewness, kurtosis, etc., via PerformanceAnalytics::table.Stats()

Selected Visualizations

Below are some of the 43 plots generated to visualize the stock market data better:

Apple Stock (AAPL)

Microsoft Stock (MSFT)


Interpreting the Outputs


Extending & Customizing


Data Source & Citations


License

Distributed under the MIT License. See LICENSE for details.