Dear R Users, I ask the following question in order to learn more on the use of 'assign' and 'paste' functions and for loop; otherwise what I am asking could be solved by binding the various first differences of the series using the 'ts.union' operator. The problem is: I have several variables in my dataset, which I should model dynamically - i.e., with lags of differences of the time series in the regression equation. Consequently, I used a loop (on which I got help from Sarah Goslee) to difference them. Using the same variable as in my previous post, the first differences are computed as follows: > DCred1 <- diff(Cred, difference=1) #call this the FIRST > LOOP > for(i in 2:5){ + print(assign(paste("DCred", i, sep=""), diff(get(paste("DCred", i-1, + sep="")), difference=1))) + }
NB: I converted the series to time series using 'ts' before differencing. Now after obtaining first differences, I try to use the 'assign' and 'paste' function in two 'for loops' to adjust the lengths of lagged terms (DCred1, DCred2, etc) to have the same length to be used in a regression model. My code is: for(i in 1:3){ #call this the SECOND LOOP for(j in 3:1){ print(assign(paste("Dcre", i, sep=""), get(paste("DCred", i, sep=""))[j:(136-i)])) } } NB: The length of the original series Cred (before differencing) equals 136. This is why the last term in the assign expression is (136 - i). NB: I run this loop after running the first loop which computes the first differences, so that the 'get' operator obtains DCred1, DCred2, etc from the results of the first loop. With this code, I expected to get DCred1 (whose length is 135) and adjust its length to equal that of DCred3 (which is 133) and call the result 'Dcre1'. Similarly, I intended to get DCred2 (whose length is 134) and adjust its length to 133 (the same as the length of DCred3) and call it Dcre2. Lastly, I would get DCred3 of length 133 and call it Dcre3, with length 133. When I run this code, it runs succesfully. However, when I then check the lengths of Dcre1, ..., Dcre3, I get: > length(Dcre1) [1] 135 > length(Dcre2) [1] 134 > length(Dcre3) [1] 133 This shows that my code did NOT achieve the intended outcome. Please assist. Thanks. Lexi [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.