Question

USING R SCRIPT a) Create a list containing the following list components, and assign the name...

USING R SCRIPT
a) Create a list containing the following list components, and assign the name ‘myportfolio’ to the list.
listname: My portfolio
stockpriceA: (122.0, 120.5, 191.7, 197.2, 199.5) stockpriceB: (58.6, 56.2, 57.9, 53.1, 54.5) stockpriceC: (102.5, 103.3, 104.5, 103.1, 102.6)
b) Create a list with names of the following components, and assign the name ‘newportfolio’ to the list.
Portfolio: My portfolio Apple: stockpriceA Samsung: stockpriceB Microsoft: stockpriceC
c) Create a list containing correlation among stockpriceA, stockpriceB, and stockpriceC and then assign the name ‘corr’ to the list.
d) Add ‘corr to ‘newportfolio’ as a new component.
e) Remove ‘corr’ from the list ‘newportfolio’.

Homework Answers

Answer #1

a)

First we will create the list of stockpriceA, stockpriceB, stockpriceC and then we will add these lists to myportfolio

Code:

stockpriceA <- list(122.0, 120.5, 191.7, 197.2, 199.5)
stockpriceB <- list(58.6, 56.2, 57.9, 53.1, 54.5)
stockpriceC <- list(102.5, 103.3, 104.5, 103.1, 102.6)
myportfolio <- list(stockpriceA,stockpriceB,stockpriceC)

print(str(myportfolio)) will print the list in string form

List of 3
 $ :List of 5
  ..$ : num 122
  ..$ : num 120
  ..$ : num 192
  ..$ : num 197
  ..$ : num 200
 $ :List of 5
  ..$ : num 58.6
  ..$ : num 56.2
  ..$ : num 57.9
  ..$ : num 53.1
  ..$ : num 54.5
 $ :List of 5
  ..$ : num 102
  ..$ : num 103
  ..$ : num 104
  ..$ : num 103
  ..$ : num 103
NULL

b)

Code:

stockpriceA <- list(122.0, 120.5, 191.7, 197.2, 199.5)
stockpriceB <- list(58.6, 56.2, 57.9, 53.1, 54.5)
stockpriceC <- list(102.5, 103.3, 104.5, 103.1, 102.6)
myportfolio <- list(stockpriceA,stockpriceB,stockpriceC)
newportfolio <- list("Portfolio"= myportfolio, "Apple"= stockpriceA, "Samsung"= stockpriceB, "Microsoft"= stockpriceC)


print(str(newportfolio)):

List of 4
 $ Portfolio:List of 3
  ..$ :List of 5
  .. ..$ : num 122
  .. ..$ : num 120
  .. ..$ : num 192
  .. ..$ : num 197
  .. ..$ : num 200
  ..$ :List of 5
  .. ..$ : num 58.6
  .. ..$ : num 56.2
  .. ..$ : num 57.9
  .. ..$ : num 53.1
  .. ..$ : num 54.5
  ..$ :List of 5
  .. ..$ : num 102
  .. ..$ : num 103
  .. ..$ : num 104
  .. ..$ : num 103
  .. ..$ : num 103
 $ Apple    :List of 5
  ..$ : num 122
  ..$ : num 120
  ..$ : num 192
  ..$ : num 197
  ..$ : num 200
 $ Samsung  :List of 5
  ..$ : num 58.6
  ..$ : num 56.2
  ..$ : num 57.9
  ..$ : num 53.1
  ..$ : num 54.5
 $ Microsoft:List of 5
  ..$ : num 102
  ..$ : num 103
  ..$ : num 104
  ..$ : num 103
  ..$ : num 103
NULL

c)

first I converted the list to data frames and after that I used correlation among them

dataA <- data.frame(matrix(unlist(stockpriceA), nrow=length(stockpriceA), byrow=T))
dataB <- data.frame(matrix(unlist(stockpriceB), nrow=length(stockpriceB), byrow=T))
dataC <- data.frame(matrix(unlist(stockpriceC), nrow=length(stockpriceC), byrow=T))
corr <- list(cor(dataB,dataA),cor(dataC,dataB),cor(dataA,dataC))

print(corr)

d)

newportfolio[length(newportfolio)+1] <- list(corr)

e)

newportfolio[length(newportfolio)] <- NULL

or

newportfolio <- newportfolio[-5] #without 5th element

Code:

stockpriceA <- list(122.0, 120.5, 191.7, 197.2, 199.5)
stockpriceB <- list(58.6, 56.2, 57.9, 53.1, 54.5)
stockpriceC <- list(102.5, 103.3, 104.5, 103.1, 102.6)
myportfolio <- list(stockpriceA,stockpriceB,stockpriceC)
#print(str(myportfolio))
newportfolio <- list("Portfolio"= myportfolio, "Apple"= stockpriceA, "Samsung"= stockpriceB, "Microsoft"= stockpriceC)
#print(str(newportfolio))
dataA <- data.frame(matrix(unlist(stockpriceA), nrow=length(stockpriceA), byrow=T))
dataB <- data.frame(matrix(unlist(stockpriceB), nrow=length(stockpriceB), byrow=T))
dataC <- data.frame(matrix(unlist(stockpriceC), nrow=length(stockpriceC), byrow=T))
corr <- list(cor(dataB,dataA),cor(dataC,dataB),cor(dataA,dataC))
newportfolio[length(newportfolio)+1] <- list("corr"=corr)
#print(newportfolio)
newportfolio <- newportfolio[-5]
#print(newportfolio)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT