Hi I have a follow up question, relating to subsetting to list items. After using the list and min(sapply()) method to adjust the length of the variables, I specify a dynamic regression equation using the variables in the list. My list looks like this:
Dcr<- list(Dcre1=DCred1,Dcre2=DCred2,Dcre3=DCred3,Dbobc1=DBoBC1,Dbobc2=DBoBC2,Dbobc3=DBoBC3,...) By specifying the list items with names, I thought I could end by referencing them (or subsetting the list) as, eg., Dcr$Dcre1 and get DCred1, Dcr$Dbobc1 and get DBoBC1, etc so that the explanatory variables of the equation can be easily associated with their respective original names. This way, I would avoid specifying the list as Dcr<-list(Dcr1, Dcr2, Dcr, 3..., Dcr15) and then subsetting the list using Dcr[[1]][1:29], Dcr[[[2]][1:29], ..., Dcr[[15]][1:29] because the list has many variables (15) and referencing the variables with numbers makes them lose their original names. When I specify the list as Dcr<- list(Dcr1, Dcr2, ..., Dcr15), then the regression equation specified as: # Regression regCred<- lm(Dcr[[1]][1:29]~Dcr[[2]][1:29]+Dcr[[3]][1:29]+Dcr[[4]][1:29]+Dcr[[5]][1:29]+Dcr[[6]][1:29]+...) runs without problems - the results are shown here below: Call: lm(formula = Dcr[[1]][1:29] ~ Dcr[[2]][1:29] + Dcr[[3]][1:29] + Dcr[[4]][1:29] + Dcr[[5]][1:29] + Dcr[[6]][1:29]) Residuals: Min 1Q Median 3Q Max -86.293 -33.586 -9.969 40.147 117.965 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 81.02064 13.28632 6.098 3.21e-06 *** Dcr[[2]][1:29] -0.97407 0.11081 -8.791 8.20e-09 *** Dcr[[3]][1:29] -0.27950 0.05899 -4.738 8.95e-05 *** Dcr[[4]][1:29] -0.07961 0.04856 -1.639 0.115 Dcr[[5]][1:29] -0.07180 0.05515 -1.302 0.206 Dcr[[6]][1:29] -0.01562 0.02086 -0.749 0.462 But when I specify the list with names as shown above, then the equation does not run - as shown by the following error message > # Regression > regCred<- lm(Dcr[[1]][1:29]~Dcr[[2]][1:29]+Dcr[[3]][1:29]+Dcr[[4]][1:29]+ + Dcr[[5]][1:29]+Dcr$Dbobc3) Error in model.frame.default(formula = Dcr[[1]][1:29] ~ Dcr[[2]][1:29] + : variable lengths differ (found for 'Dcr$Dbobc3') > Dcr[[5]][1:29]+Dcr$Dbobc3[1:29]) Error: unexpected ')' in "Dcr[[5]][1:29]+Dcr$Dbobc3[1:29])" NB: In the equation with error message, only the last term is specified by referencing its name (ie., Dcr$Dbobc3[1:29]. Also note that the error occurs whether I append '[1:29]' to Dcr$Dbobc or not. How do I resolve this? Thanks. Lexi ----- Original Message ----- From: "Lekgatlhamang, lexi Setlhare" <lexisetlh...@yahoo.com> To: Petr PIKAL <petr.pi...@precheza.cz> Cc: R-Help <r-help@r-project.org> Sent: Friday, June 29, 2012 1:24 AM Subject: Re: [R] Adjusting length of series Thanks a lot Peter. It is a learning process for me. ________________________________ From: Petr PIKAL <petr.pi...@precheza.cz> Cc: R-Help <r-help@r-project.org> Sent: Thursday, June 28, 2012 6:20 PM Subject: Re: [R] Adjusting length of series Hi I use R for quite a long time and as I remember I did not use such assign paste i loop yet. Insted of such construct with polluting environment with plenty of objects named something(i)somethingelse it is always advisable to use lists. When you want to shorten variables to some common length (by cutting some portion of it) you can do it easily by: lll<-list(a=1:10, b=1:9, c=1:8) lll $a [1] 1 2 3 4 5 6 7 8 9 10 $b [1] 1 2 3 4 5 6 7 8 9 $c [1] 1 2 3 4 5 6 7 8 shortest variable in lll min(sapply(lll,length)) sapply(lll,"[",1:min( min(sapply(lll,length)))) a b c [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 [4,] 4 4 4 [5,] 5 5 5 [6,] 6 6 6 [7,] 7 7 7 [8,] 8 8 8 Regards Petr > > 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. [[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. [[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.