Credit Resic Return

DR.Edirdiri Fadol Ibrahim Fadol Scientific Research Center)

2024-06-24

## Loading required package: splines
## Loading required package: RcmdrMisc
## Loading required package: car
## Loading required package: carData
## Loading required package: sandwich
## Loading required package: effects
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## The Commander GUI is launched only in interactive sessions
## 
## Attaching package: 'Rcmdr'
## The following object is masked from 'package:base':
## 
##     errorCondition
> # Load necessary libraries
> library(quantmod)
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'
The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
> library(ggplot2)
> # Step 1: Fetch historical stock prices using quantmod package
> ticker <- "AAPL"  # Example: Apple Inc.
> start_date <- "2021-01-01"
> end_date <- "2021-12-31"
> getSymbols(ticker, from = start_date, to = end_date)
[1] "AAPL"
> # Step 2: Extract adjusted closing prices
> stock_prices <- Ad(get(ticker))
> # Step 3: Calculate daily returns
> daily_returns <- diff(log(stock_prices))
> # Step 4: Calculate key statistics
> mean_return <- mean(daily_returns, na.rm = TRUE)
> volatility <- sd(daily_returns, na.rm = TRUE)
> cat("Mean Daily Return:", mean_return, "\n")
Mean Daily Return: 0.001304266 
> cat("Volatility (Standard Deviation of Daily Returns):", volatility, "\n")
Volatility (Standard Deviation of Daily Returns): 0.01577933 
> # Step 5: Visualize daily returns
> dates <- index(daily_returns)
> returns_data <- data.frame(Date = as.Date(dates), Daily_Return = as.numeric(daily_returns))
> ggplot(returns_data, aes(x = Date, y = Daily_Return)) +
+   geom_line(color = "blue") +
+   labs(title = paste("Daily Returns of", ticker),
+        x = "Date",
+        y = "Daily Returns") +
+   theme_minimal()
Warning: Removed 1 row containing missing values (`geom_line()`).

> ### Summarize Data Set: returns_data
> summary(returns_data)
      Date             Daily_Return      
 Min.   :2021-01-04   Min.   :-0.042567  
 1st Qu.:2021-04-05   1st Qu.:-0.007600  
 Median :2021-07-02   Median : 0.001478  
 Mean   :2021-07-02   Mean   : 0.001304  
 3rd Qu.:2021-09-30   3rd Qu.: 0.012423  
 Max.   :2021-12-30   Max.   : 0.052451  
                      NA's   :1          

Normality Test: ~Daily_Return

> normalityTest(~Daily_Return, test="shapiro.test", data=returns_data)

    Shapiro-Wilk normality test

data:  Daily_Return
W = 0.99126, p-value = 0.1417

Normality Test: ~Daily_Return

> normalityTest(~Daily_Return, test="shapiro.test", data=returns_data)

    Shapiro-Wilk normality test

data:  Daily_Return
W = 0.99126, p-value = 0.1417
> library(abind, pos=23)
> library(e1071, pos=24)

Numerical Summaries: returns_data

> numSummary(returns_data[,"Daily_Return", drop=FALSE], statistics=c("mean", 
+   "sd", "IQR", "quantiles"), quantiles=c(0,.25,.5,.75,1))
        mean         sd        IQR          0%          25%        50%
 0.001304266 0.01577933 0.02002334 -0.04256681 -0.007600496 0.00147839
        75%       100%   n NA
 0.01242284 0.05245127 250  1

Numerical Summaries: returns_data

> numSummary(returns_data[,"Daily_Return", drop=FALSE], statistics=c("mean", 
+   "sd", "IQR", "quantiles"), quantiles=c(0,.25,.5,.75,1))
        mean         sd        IQR          0%          25%        50%
 0.001304266 0.01577933 0.02002334 -0.04256681 -0.007600496 0.00147839
        75%       100%   n NA
 0.01242284 0.05245127 250  1

Single-Sample t-Test: Daily_Return

> with(returns_data, (t.test(Daily_Return, alternative = "two.sided", mu = 
+   0.0, conf.level = .95)))

    One Sample t-test

data:  Daily_Return
t = 1.3069, df = 249, p-value = 0.1924
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.0006612781  0.0032698099
sample estimates:
  mean of x 
0.001304266 

Single-Sample t-Test: Daily_Return

> with(returns_data, (t.test(Daily_Return, alternative = "two.sided", mu = 
+   0.0, conf.level = .95)))

    One Sample t-test

data:  Daily_Return
t = 1.3069, df = 249, p-value = 0.1924
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.0006612781  0.0032698099
sample estimates:
  mean of x 
0.001304266