Help with error in code

Hello everybody,
I would like to ask for help.
I have this code and example data like that. But there is an error but I could not find to way to solve it.
In return of data AAM, it doesn't turn the result of meanindustry and lagmeanindustry.
I appreciate any helps. Thank you in advance.


marketreturn<-data.frame(
  date = c(20100721L,20100728L,20100804L,
           20100811L,20100818L,20000906L,20000913L,20000920L,20000927L,
           20001004L),
  marketreturn = c(NA,0.050880248,0.047496318,
                   0.045342707,-0.032027572,-0.022926617,0.031730513,0.018349414,
                   0.012520091,0.012114629)
)
firmreturn<-data.frame(
  date = c(20100721L,20100728L,20100804L,
           20100811L,20100818L,20100721L,20100728L,20100804L,20100811L,
           20100818L),
  return = c(NA,0.015548911,0.07428283,-0.111824589,
             0.020631282,0.313838977,0.327700704,0.159362155,
             0.01110589,-0.197372312),
  stockcode = as.factor(c("AAM","AAM","AAM",
                          "AAM","AAM","AAA","AAA","AAA","AAA","AAA"))
)
  
  AAM<-firmreturn %>% 
    filter(stockcode=="AAM")
  industry<-firmreturn %>% 
    filter(stockcode!="AAM")
  
  
  AAM["year"] <-  NA
  AAM["meanindustry"] <-  NA
  AAM["lagmeanindustry"] <-  NA
  AAM["marketreturn"]<- NA
  AAM["lagmarketreturn"]<-NA
  
  for (i in 1:length(AAM$date)){
    
    temp<-filter(industry,industry$date==AAM$date[i])
    
    AAM$meanindustry[i]<-mean(temp$firmreturn,na.rm = TRUE) 
    
    AAM$marketreturn[i]<-marketreturn$marketreturn[marketreturn$date==AAM$date[i]]
  }
  AAM$lagmeanindustry<-lag(AAM$meanindustry,1, na.pad=TRUE)
  AAM$lagmarketreturn<-lag(AAM$marketreturn,1, na.pad=TRUE)
  AAM$year <- as.character(AAM$date)
  AAM$year <- substr(AAM$year,1,4) %>% as.integer()

I know. But I do not know how to solve that. hic.

I don't understand the intention of the for loop...
It seems like it might be an attempt to join the tables? There are functions for that...

I want to run regression as follows,

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.