-1

I got a bunch dynamically created regressions stored in some list called regressions. Now I´d like to rename their coefficients efficiently. What I have so far is this loop that works:

for (i in 1:length(params[,1])){
names(regressions[[i]]$coefficients)[pos] <- paste(params[i,1],".lag",params[i,2],sep="")
}

I've been trying for quite a while to get this done a little more generally with the help of a function, cause this not the only list of regressions I have. However I could not get anything else to work. Here a few other tries basically based on lapply:

 correctNames <- function(reglist,namevec,pos){
 names(reglist[[i]]$coefficients)[pos] <- as.character(namevec)
}

lapply(regressions,correctNames(reglist,namevec,pos),
reglist=regressions,namevec=params[,1],pos=2)

Another try was to write a function with a for loop which also works internally as print shows but does not assign the names globally (where the regressions list is stored).

correctNames <- function(reglist,pos,namevec){
for (i in 1:length(params[,1])){
names(reglist[[i]]$coefficients)[pos] <- paste(namevec,".lag",namevec,sep="")
}
#this test proves it's work inside the function... 
print(reglist[[10]]
}

Ah, gimme a break.

Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • I didn't downvote, but I would note that you haven't said _what went wrong_ with the attempts that didn't work. – joran Sep 23 '11 at 16:33
  • True. The last approach did not work, cause when I called regressions on the console the names remained the same. That did not even surprise me I just missed HOW to assign it to the global environment in this context. The other one gave back the following error: Error in correctNames(reglist, namevec, pos) : object 'namevec' not found – Matt Bannert Sep 23 '11 at 16:51

2 Answers2

3

There's no "i" inside that first version of "correctNames" function; and you probably don't realize that you are not assigning it to "regressions", only to a copy of the regression object. Try instead:

correctNames <- function(reglist,namevec,pos){
  names(reglist$coefficients)[pos] <- as.character(namevec)
   return(reglist)                          }
newregs <- mapply(correctNames,  
                     reglist=regressions, 
                     namevec=as.character(params[,1]), 
                     MoreArgs= list( pos=2))

After seeing the note from Ramnath and noticing that the code did work but was giving flaky names for the "params" I looked at params and saw that it was a factor, and so changed the argument in the mapply call to as.character(params[,1]).

> newregs[1,1]
[[1]]
(Intercept)     log(M1) 
  -5.753758    2.178137 
IRTFM
  • 258,963
  • 21
  • 364
  • 487
1

If this is a follow up to your earlier question, then here is what I would do

coefs = plyr::ldply(regressions, coef)
coefs = transform(coefs, reg_name = paste(x, '.lag', l, sep = ""))[,-c(1, 2)]
names(coefs) = c('intercept', 'reg_coef', 'reg_name')

This gives you

 intercept reg_coef     reg_name
1 -5.753758 2.178137 log(M1).lag0
2  7.356434 7.532603      rs.lag0
3  7.198149 8.993312      rl.lag0
4 -5.840754 2.193382 log(M1).lag1
5  7.366914 7.419599      rs.lag1
6  7.211223 8.879969      rl.lag1
7 -5.988306 2.220994 log(M1).lag4
8  7.395494 7.127231      rs.lag4
9  7.246161 8.582998      rl.lag4
Community
  • 1
  • 1
Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • slick. Just need get a little more experience with the power of ldply and other `plyr` functions. I see it helps an awful lot. Still though, I keep wondering whether there is an lapply solution, too. The reason why I keep asking is that I realized I tried to solve another issue with lapply months ago before I had an R break. So what I am trying to say is, my intuition is likely to get my back to an lapply based so solution. Maybe I need to change THAT, but maybe there's an lapply solution too. – Matt Bannert Sep 23 '11 at 16:56
  • I cannot tel if you were satisfied with the mapply solution since you didn't accept it, but will note that `lapply` is more difficult to use here since you are passing two multi-valued items: the 'regressions' list and the params[,1] vector and they need to be matched up. Your for loop was doing htat with an index while mapply does that implicitly – IRTFM Sep 23 '11 at 17:15
  • Hmm. I can only accept one and only hand out one upvote. Well I guess I have to accept the mapply solution since I asked more towards that direction. Still though in my particular case Rammath was right – it was a follow up and this answer fits better. I just blew the chance to ask a good question this time by trying to make it more general. – Matt Bannert Sep 25 '11 at 19:19