[R] Creating a data set within a function

Gabor Grothendieck ggrothendieck at gmail.com
Sun Aug 19 21:56:08 CEST 2007


Check out ?embed

On 8/19/07, Anup Nandialath <anup_nandialath at yahoo.com> wrote:
> Dear Friends,
>
> I'm trying to find if there is a way to automate creation of the design matrix. Suppose we are interested in say running an autoregressive model. The user inputs the following data
>
> myfunAR <- function(y, order)
> {.....
> ......
> }
>
> now here y is the data series and order represents the level of the process. In other words if order=2 then we have an AR (2) process. Now it is easy to to create the y vector within the function, but I'm not clear on how to create the design matrix.
>
> For instance if order=2 then
>
> y <- as.matrix(rnorm(100))
> ynew <- as.matrix(y[3:nrow(y),1])
> x <- as.matrix(cbind(rep(1, nrow(y)-2), y[2:(nrow(y)-1),1], y[1:(nrow(y)-2),1]))
>
> ynew and x gives me the response vector and design matrix respectively. however, I'm trying to write a general function which will accomodate any order. Hence given the user inputs y and the order, is there a way to program the creation of the x matrix automatically.
>
> The long way would be
>
> if (order=1)
> {%%%%%%%%%5}
>
> if (order=2)
> {%%%%%%%%%%%%%5}
>
> but this will force me to limit at some point.Is there an alternative way to program this??
>
> Thanks in advance
> Regards
>
> Anup
>
>
>
>
> ---------------------------------
> Building a website is a piece of cake.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list