Solve These Questions Using R and Explain Steps

title: “Assignment”

author: “Codes and Outputs”

output:

  word_document: default

  html_document: default

## 1

*Data Source: https://finance.yahoo.com/quote/%5EGSPC/history?period1=1136073600&period2=1264982400&interval=1mo&filter=history&frequency=1mo&includeAdjustedClose=true. We will work with the the 6th column Adj. Close (currency in USD).*

“`{r}

#reading the dataset into R

SP <- read.csv(file.choose())

adj.close <- SP$Adj.Close

#finding the 12 month rolling average starting from the 13th month

roll.avg <- c()

for (i in 13:length(adj.close))

    {roll.avg[i] <- mean(adj.close[(i-12):(i-1)])}

roll.avg

#finding the 12 month rolling variance starting from the 13th month

roll.var <- c()

for (i in 13:length(adj.close))

    {roll.var[i] <- var(adj.close[(i-12):(i-1)])}

roll.var

#plotting both 12 months rolling average and 12 months rolling variance

plot(c(1:49),roll.avg, col=”red”, xlab=”Month”, ylab=”12 months rolling average of 12 months before that”)

plot(c(1:49),roll.var,col=”blue”,xlab=”Month”,ylab=”12 months rolling variance of 12 months before that”)

“`

## 2

“`{r}

# rows 1 to 25 of SP has data from Jan 01, 2006 to Jan 01, 2008

x1 <- adj.close[1:25]

# rows 25 to 49 of SP has data from Jan 01, 2008 to Jan 01, 2010

x2 <- adj.close[26:49]

# test the hypothesis H0: mean(x1)=mean(x2) vs H1:mean(x1)<mean(x2) at 0.05 level of significance

t.test(x1,x2,alternative = “less”,mu = 0,conf.level = 0.95)

“`

*We see that the p value for the the hypothesis the monthly mean return between 2006:01 – 2008:01 is lower than 2008:01 – 2010: 01 at the 0.05 level of significance (alpha = 0.05) is 1 (>0.05). So, we fail to reject the null hypothesis H0 and conclude that the two means are equal.*

“`{r}

y1 <- roll.var[1:25]

y2 <- roll.var[26:49]

t.test(y1,y2,alternative = “less”,mu = 0,conf.level = 0.95)

“`

*We see that the p value for the the above hypothesis testing is 3.921e-05 < alpha = 0.05. So, we reject the null hypothesis H0 and conclude that the monthly rolling variance (12 months) between 2006:01 – 2008:01 is lower than 2008:01 – 2010: 01. So, it was riskier to invest in the stock in the period between 2008:01 – 2010:01, since the variance was higher for that period.*

“`{r}

# finding the ratio of monthly returns to monthly rolling variance for each month

ratio <- adj.close/roll.var

ratio

# plotting the ratios

plot(c(1:49),ratio,xlab=”Month”,ylab=”r = adj. close / 12 months rolling variance”,col=”brown”)

“`

*The values of this ratio were consistently quite high (above 0.5) for the months 21 to 26, i.e., Sep 01, 2007 to Feb 01, 2008. Also, the value of the ratio was quite high, around 0.5, for the 13th month Jan 01, 2007. So, it was optimal to invest in stocks around these periods.*

## 3

“`{r}

sample1 <- round(runif(10, min = 0, max =12))

sample2 <- round(runif(10, min = 0, max =10))

# hypothesis testing

t.test(sample1,sample2,alternative = “greater”,mu = 0,conf.level = 0.95)

“`

*The p value of the above hypothesis at the 0.05 level of significance is greater than alpha = 0.05. So, we fail to reject the null hypothesis and conclude at the 0.05 level of significance that the means of the two samples are equal.*

*The probability of type I error is equal to the alpha value we take, i.e., 0.05.To calculate the probability of type II error, we calculate the power of the test at first.*

“`{r}

power = power.t.test(n=10,delta=0,sd=1,sig.level=0.05,power=NULL,type=”two.sample”,alternative=”one.sided”)$power

type.2.error <- 1-power

type.2.error

“`

*The probability of type II error is 0.95.*

*Now, we assume that these samples were taken from a vaccine trial where the first sample showed the number of people out of 10 who developed immunity against the disease for each trial and the second group were given a placebo. Since from the above hypothesis testing, we have seen that at the 0.05 level of significance, the mean number of people who developed immunity was same for the two groups, it means that the introduction of vaccine was not quite effective in developing immunity among the people. So, I would not introduce this vaccine.*

## 4

“`{r}

# reading the dataset into R

Uganda <- read.csv(file.choose())

u1 <- Uganda[,1]

u2 <- Uganda[,2]

# H0: mean(u1)=mean(u2) vs H1: mean(u1)>mean(u2)

t.test(u1,u2,alternative = “greater”,mu = 0,conf.level = 0.95)

“`

*We see that the p value of the above t-test is 0.09501 > alpha = 0.05. So, we fail to reject the null hypothesis H0 at the 0.05 level of significance and conclude that the mean number of students whose improved their school grades did not increase after the introduction of solar powered reading lamps in Uganda or in other words, solar powered reading lamps were not effective in improving grades of students in Uganda. This inference is however purely based on the available sample.*

Share this post

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on whatsapp
WhatsApp

Related posts

Keep in touch with the trends