0

I am new to R and trying to do a coursework about factor analysis with it.

I have two data sets FundReturn(120 rows, 14 columns) and Factors(120 rows, 30 columns), I want to do a one-factor regression for all the possible pairs of factors and funds, starting with the first 60 observations. With the parameters estimated, I want to calculate the predicted value for the 61st fund return with the 61st value of the factor. Then the estimation window is expanded one observation bigger and new parameters are estimated with the updated sample, then the predicted value for 62rd fund return is calculated, so on so forth. Totally 60 predictions will be made, stored in Predictions=array(1,dim=c(60,30,14)), so I can compare them with the realized values.

The following is the code I used and produced this error: Error in Predictions[p, fa, fu] <- coeff[1, p, fa, fu] + coeff[2, p, fa, : replacement has length zero

Can anyone spot the problem? Your help is very appreciated.

Predictions=array(1,dim=c(60,30,14))

coeff=array(1,dim=c(3,60,30,14))

v1<- 1:30
v2<- 1:60
v3<- 1:14
for(fu in v3){
  for (fa in v1){ 
    for (p in v2){
      y1=FundReturn[1:(59+p),fu]
      x1=Factors[1:(59+p),fa]
      Model<-lm(y1 ~ x1 + lag(y1))
      coeff[1:3,p,fa,fu]=Model[["coefficients"]]
      Predictions[p,fa,fu]= coeff[1,p,fa,fu]+coeff[2,p,fa,fu]*Factors[60+p,fa]+coeff[3,p,fa,fu]*FundReturn[59+p,fu]
    }
  } 
}
  • "replacement of length 0" implies one of the objects on the right hand side is not returning a number, i'd suggest setting `fu` `fa` and `p` to the values where the error starts and evaluating `coeff`, `Factors`, and `FundReturn` there to see which is returning NA – Michael Bird Jul 16 '18 at 10:57
  • It appears that it stopped right at the beginning, so p , fa , fu are all 1, actually I'm quite sure the data sets have no NA, and and I can see that the first 3 entries of coeff was calculated successfully. Could the problem be the format of the data? Factors and FundReturn are xts and coeff is called as Large array. – Harry.H Jul 16 '18 at 12:53
  • with `p=1`, `fa=1`, and `fu=1`, does `coeff[1,p,fa,fu]+coeff[2,p,fa,fu]*Factors[60+p,fa]+coeff[3,p,fa,fu]*FundReturn[59+p,fu]` return a single number? – Michael Bird Jul 16 '18 at 12:57
  • As you haven't provided `FundReturn` or `Factors`, I tried making my own with `FundReturn <- as.data.frame(matrix(rnorm(120*14),nrow = 120,ncol=14)); Factors <- as.data.frame(matrix(rnorm(120*30),nrow = 120,ncol=30))` . After than your code ran to completion. This suggests the issue lies with your `FundReturn` of `Factors`. Are you sure of their dimensions? – Michael Bird Jul 16 '18 at 13:02
  • it returns this Data: numeric(0) Index: numeric(0) – Harry.H Jul 16 '18 at 13:02
  • so it's attempting to index something it can't find. try `dim(FundReturn)` and `dim(Factors)` to check if you really have 120 rows, 14 cols and 120 rows, 30 columns respectively. – Michael Bird Jul 16 '18 at 13:04
  • dim(FundReturn) [1] 120 14 > dim(Factors) [1] 120 30 – Harry.H Jul 16 '18 at 13:08
  • Have you tried copying my definitions (about 4 comments up) for `FundReturns` and `Factors` as an alternative to yours? If you can copy my definitions and run your code it shows the problem comes from your dataset. – Michael Bird Jul 16 '18 at 13:10
  • 1
    It is running! I added as.data.frame in front of my original definition, FundReturn <- as.data.frame(xts(x = X1[, 2:15], order.by = dates)) . It seems I can't use xts. Thank you so much!! – Harry.H Jul 16 '18 at 13:19
  • You're welcome! Next time you get stuck, if you can make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) then people can help you much quicker and spot that your data (that wasn't provided) was not in the right format :) – Michael Bird Jul 16 '18 at 13:24
  • Yeah, next time I will make sure I do that. Another question is how can I view the results in coeff ? It seems I can't use View() – Harry.H Jul 16 '18 at 13:36
  • `coeff` has 4 dimensions, `View()` is good for looking at tables (aka 2 dimensions). You can use `View` if you only want to look at a slice of `coeffs` eg `View(coeffs[1,1,,]`. But this is not much use. visualising 4 dimensions is tricky. – Michael Bird Jul 16 '18 at 13:43
  • 1
    Being able to view a slice is good for now, thank you again for your help – Harry.H Jul 16 '18 at 13:48

0 Answers0