Do you have an idea to call only one specific matrix which contain a string of characters in my list of twelve matrices ? For exemple, I have my list of twelve matrices (which represent data of genetic distance between pairs of 60 individuals, a matrix is therefore 60*60) for the 12 months of the year, and I want to extract only 3 matrices for 3 months of the year.
So for that, I want to call the matrices which contain in their name "Apr", "May" or "Jun", like this : spring <- list(list.mat[["Apr"]], list.mat[["May"]], list.mat[["Jun"]]). But because there is missing months, I want to find a function who call only the months who can possibly contain these string of charactrs in their names. I try grep function and its derivates but it doesn't work on list of matrices...
I know there is a simple way to do that but I don't know which way to look!
I don't want to make it manually because I have more than 40 list of 12 matrices !
Thanks for any replies if you know what i'm looking for
We may use Filter from base R to remove the NULL list elements
Filter(Negate(is.null), list.mat)
I am new to R and have a quite basic question I guess. I couldn't find help for my specific issue.
I have a data frame consisting of two columns. The first indicating the year and quarter (e.g. 19901, 19902, 19903 etc.). The second shows the corresponding quarterly return.
Now, I want to annualize the returns. As a result I want to have a data.frame with only a year column and the corresponding annualized return.
I know there is the function Return.annualized from the ‘PerformanceAnalytics’ package. However, this function does not calculate rolling annualized returns.
Is there a nice package or function that could solve my problem?
Any help is really appreciated. Thank you!
If you have log returns, they exhibit the nice advantage of being summable over time.
If that's the case you can simply apply a rolling window that sums the four previous quarters.
Let's assume you have a vector or list with quarterly log returns called q_ret
library('zoo')
an_ret <- rollapply(q_ret, 4, sum)
Note, that from a finance perspective, this does not hold with simple returns.
I am new to time-series analysis and have a data set with a daily time step at 5 factor levels. My goal is to use the acf function in R to determine whether there is significant autocorrelation across the response variable of interest so that I can justify whether or not a time-series model is necessary.
I have sorted the dataset by Day, and am using the following code:
acf(DE_vec, lag.max=7)
The dataset has not been converted to a time-series object…it is a vector sorted by Day.
My first question is whether the dataframe should be converted to a time-series object, or if it is also correct to sort the vector by Day?
Second, if I have a variable repeated over the 5 levels for each Day, then should I construct 5 different acf plots for each level, or would it be ok to pool over stations as was done with the code above?
Thanks in advance,
Yes, acf() will work on a data.frame class, and yes, you should compute the ACF for each of the 5 levels separately. If you pass the entire df to acf(), it will return the ACF for each of the levels.
If you are curious about the relationship across levels, then you need to use ccf() or some mutual information metric like those in the entropy or infotheo pkgs.
I am trying to reproduce an equally weighted price index. I have the prices and a matrix which tells when a stock is in and out of the index. Here is a small part of the data.
table="date, A, B,C,D,E,F
1,31/01/1998,1,1,1,1,1,0
2,28/02/1998,1,1,1,1,1,0
3,31/03/1998,1,1,1,1,1,0
4,30/04/1998,1,1,1,1,1,0
5,31/05/1998,1,1,1,1,1,0"
matrix=read.csv(text=table)
table2="date,A,B,C,D,E,F
1,05/01/98,20.56,97.40,279.70,72.85,20.33,298.00
2,06/01/98,20.56,96.50,276.20,72.90,20.22,299.90
3,14/02/98,20.84,98.45,282.50,73.75,20.70,302.80
4,15/02/98,20.90,98.50,280.70,73.65,20.71,306.50
5,09/03/98,20.58,97.00,276.20,72.95,20.25,304.00"
price=read.csv(text=table2)
The order of the stock is the same in the price and matrix data. Since I would like to multiply the matrix with the price I turned both into matrix.
as.matrix(price)
as.matrix(matrix)
as.Date[price[,1], format="%d/%m/%y"] #Error: object of type 'closure' is not subsettable
as.Date[matrix[,1], format="%d/%m/%Y"]
(1)However here I got my first problem. The dates are not recognized whether in the matrix nor if I do it before using as.Matrix(). I also tried methods proposed here (Extract month and year from a zoo::yearmon object). I need the dates for the following reason. I would like to make a loop which 1. Takes the month and year from the matrix and searches for the same months and years in the price data. 2. If same month and years are found it should multiply the row from matrix with the rows from prices. This is due to the fact that the matrix is on monthly basis and the prices are daily. And this would be my Idea of the loop:
for (matrix(%m/%Y) in price$date){
if (matrix(%m/%Y)== price(%m/%y)
c<- matrix[position of matrix(%m/%Y),] %*% price[position of price(%m/%y),]
}
(2)However I never worked with loops before and the second question is if the for loop is suitable for my problem? The desired output of the loop would be the following:
table3="date,A,B,C,D,E,F
1,05/01/98,20.56,97.40,279.70,72.85,20.33,0
2,06/01/98,20.56,96.50,276.20,72.90,20.22,0
3,14/02/98,20.84,98.45,282.50,73.75,20.70,0
4,15/02/98,20.90,98.50,280.70,73.65,20.71,0
5,09/03/98,20.58,97.00,276.20,72.95,20.25,0"
desired_c=read.csv(text=table3)
At the end however, I would like to have an equally weighted price index like this:
table4="date, price
1,05/01/98,98.168
2,06/01/98,97.276
3,14/02/98,99.248
4,15/02/98,98.892
5,09/03/98,97.396"
desired_index=read.csv(text=table4)
if I could put that in my loop that would be great. Please note that the matrix and the prices are consisting of many observations. Therefore only deleting last column is not an option.
I have tried to search this question on here but I couldn't find anything so sorry if this question has already been answered. My dataset consists of daily information for a large number of stocks (1000+) over a 10 year period. So I have read my dataset as a data frame time series where each column is a separate stock. I would like to regress each of the stock against month dummy variables capture the season variation and obtain the residuals. What I have done is the following:
for (i in 1:1000){
month.f<-factor(months(time(stockinfo[,i])))
dummy<-model.matrix(month.f)
residStock[,1]<-residuals(lm(stockinfo[,i]~dummy,na.action=na.exclude))
}
#Stockinfo is data.frame
Is this the correct way to do it?
Secondly, i would like to run a regression using the residuals as the the dependent variable and other independent variables from another data frame. What would be the best way to do this, would I have to use a for loop again?
Thank you a lot for your help.
You can create a list of stocks as follows and then use Map function and can avoid R for loop (Not tested since you didn't provide the sample data)
Assume your data is mydata with month as 1,2, you use 11 months as dummy if there are 12 months
mystock<-list("APP~","INTEL~","MICROSOFT~") # stocks with tilde sign
myresi<-Map(function(x) resi(lm(as.formula(paste(x,paste(levels(as.factor(mydata$month))[-1],collapse="+"))),data=mydata),mystock) #-1 means we are using only 11 months excluding first as base month
Say your independent var is indep1,indep2, and indep3 and dependent is dep (And assuming that dep and indep are same for each stocks)
myestimate<-Map(function(x)lm(dep~indep1+indep2+indep3,data=x),myresi)