Quantcast

formula version of sunflowerplot() fails when axis label specified

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

formula version of sunflowerplot() fails when axis label specified

Gerrit Eichner
Hello, R-help,

does anybody have already a work-around for the problem that the formula
version of sunflowerplot() throws an error when provided with a value for
xlab (or ylab) different from NULL:

> sunflowerplot( Sepal.Length ~ Sepal.Width, data = iris, xlab = "A")
Error in model.frame.default(formula = Sepal.Length ~ Sepal.Width, data = iris,  :
   variable lengths differ (found for '(xlab)')

And are you -- the one with the work-around -- willing to share it? :)

  Best regards  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: formula version of sunflowerplot() fails when axis label specified

sina rueeger
Hi Gerrit

I did the following: access the function via graphics:::sunflowerplot.formula, and then commented some lines to see, where the problem lies. I guess that it is the chunk with "mf <- eval(m, parent.frame())", but to be honest, I do not know why. My workaround is as follows:

sunflowerplot.formula2 <- function (formula, data = NULL, xlab = NULL, ylab = NULL, ...,
    subset, na.action = NULL)
{

    if (missing(formula) || (length(formula) != 3L))
        stop("formula missing or incorrect")

## >> replaced all the part where "m" is defined, and write instead:
    mf <- model.frame(formula, data)

    require(stats, quietly = TRUE)
    if (NCOL(mf) != 2L)
        stop("'formula' should specify exactly two variables")
    if (is.null(xlab))
        xlab <- names(mf)[2L]
    if (is.null(ylab))
     ylab <- names(mf)[1L]

    sunflowerplot(mf[[2L]], mf[[1L]], xlab = xlab, ylab = ylab,
        ...)
}

I know, the solution is cheap and not as sophisticated as the original function, but it does the job (for your example).

Best,
Sina

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: formula version of sunflowerplot() fails when axis label specified

David Carlson
In reply to this post by Gerrit Eichner
It seems to be a bug when you specify an x-label.

# No xlab= works fine, but uses variable names to label x and
# y axes

> sunflowerplot(Sepal.Length~Sepal.Width, data=iris)

# These all throw the error message
> sunflowerplot(Sepal.Length~Sepal.Width, data=iris, xlab="A")
> sunflowerplot(Sepal.Length~Sepal.Width, data=iris, xlab="")
> sunflowerplot(Sepal.Length~Sepal.Width, data=iris, xlab="Sunflowers")

The work around would be to set up the plot and with plot() and then
add the sunflowerplot

> x <- range(iris$Sepal.Width)
> y <-  range(iris$Sepal.Length)
> plot(x, y, type="n", xlab="A", ylab="")
> sunflowerplot(Sepal.Length~Sepal.Width, data=iris, add=TRUE)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Gerrit Eichner
> Sent: Wednesday, June 27, 2012 6:17 AM
> To: R-Help
> Subject: [R] formula version of sunflowerplot() fails when axis label
> specified
>
> Hello, R-help,
>
> does anybody have already a work-around for the problem that the
> formula
> version of sunflowerplot() throws an error when provided with a value
> for
> xlab (or ylab) different from NULL:
>
> > sunflowerplot( Sepal.Length ~ Sepal.Width, data = iris, xlab = "A")
> Error in model.frame.default(formula = Sepal.Length ~ Sepal.Width, data
> = iris,  :
>    variable lengths differ (found for '(xlab)')
>
> And are you -- the one with the work-around -- willing to share it? :)
>
>   Best regards  --  Gerrit
>
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
> [hidden email]   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: formula version of sunflowerplot() fails when axis label specified

David Carlson
In reply to this post by Gerrit Eichner
Or just avoid the formula version:

> with(iris, sunflowerplot(Sepal.Width, Sepal.Length, xlab="A"))


----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Gerrit Eichner
> Sent: Wednesday, June 27, 2012 6:17 AM
> To: R-Help
> Subject: [R] formula version of sunflowerplot() fails when axis label
> specified
>
> Hello, R-help,
>
> does anybody have already a work-around for the problem that the
> formula
> version of sunflowerplot() throws an error when provided with a value
> for
> xlab (or ylab) different from NULL:
>
> > sunflowerplot( Sepal.Length ~ Sepal.Width, data = iris, xlab = "A")
> Error in model.frame.default(formula = Sepal.Length ~ Sepal.Width, data
> = iris,  :
>    variable lengths differ (found for '(xlab)')
>
> And are you -- the one with the work-around -- willing to share it? :)
>
>   Best regards  --  Gerrit
>
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner                   Mathematical Institute, Room 212
> [hidden email]   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: formula version of sunflowerplot() fails when axis labelspecified

Gerrit Eichner
In reply to this post by sina rueeger
Thank you, Sina,
thank you, David,

I knew how to access the code of sunflowerplot.formula() and that
sunflowerplot() does work in the default version, i.e., w/o formula (but
your suggestions triggered my ambition ;-)).

Below is my -- slightly commented -- version (the search for which was
inspired by Sina's approach) which results from studying the code of
various other plotting functions with formula interface (e.g.,
boxplot.fomula(), stripchart.formula, and, in particular, plot.formula().)

  Hth and best regards  --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
[hidden email]   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner
---------------------------------------------------------------------

sunflowerplot.formula <- function( formula, data = NULL,
                                    xlab = NULL, ylab = NULL, ...,
                                    subset, na.action = NULL) {
  if(missing(formula) || (length(formula) != 3L))
   stop("formula missing or incorrect")

  m <- match.call(expand.dots = FALSE)
  if (is.matrix(eval(m$data, parent.frame())))
   m$data <- as.data.frame(data)

  m$... <- NULL
  m$xlab <- m$ylab <- NULL   # New: Deleting xlab and ylab from m to
                             # avoid hindrance of the computation of
                             # the model frame below.
  m$na.action <- na.action

  require(stats, quietly = TRUE)
  m[[1L]] <- as.name("model.frame")
  mf <- eval(m, parent.frame())     # Here used to lie the problem.

  if (NCOL(mf) != 2L)
   stop("'formula' should specify exactly two variables")
  if (is.null(xlab))
   xlab <- names(mf)[2L]
  if (is.null(ylab))
    ylab <- names(mf)[1L]
  sunflowerplot(mf[[2L]], mf[[1L]],
                xlab = xlab, ylab = ylab, # New: To make sure that possibly
                                          # user defined labels are used.
                ...)
  }

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Adjusting length of series

Lekgatlhamang, lexi Setlhare
In reply to this post by Gerrit Eichner


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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adjusting length of series

PIKAL Petr
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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adjusting length of series

Lekgatlhamang, lexi Setlhare
Thanks a lot Peter. It is a learning process for me.

 

________________________________
 From: Petr PIKAL <[hidden email]>

Cc: R-Help <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Adjusting length of series

Lekgatlhamang, lexi Setlhare
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" <[hidden email]>
To: Petr PIKAL <[hidden email]>
Cc: R-Help <[hidden email]>
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 <[hidden email]>

Cc: R-Help <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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]]


______________________________________________
[hidden email] 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]]


______________________________________________
[hidden email] 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.
Loading...