[R] extracting coefficients from ar() output

David Winsemius dwinsemius at comcast.net
Fri Jun 17 22:19:59 CEST 2016


> On Jun 17, 2016, at 11:34 AM, T.Riedle <tr206 at kent.ac.uk> wrote:
> 
> Thank you very much.
> Here is the error message.
> 
>> WriteXLS(coefar,ExcelFileName = "R.xls",SheetNames="test")
> Error in WriteXLS(coefar, ExcelFileName = "R.xls", SheetNames = "test") : 
>  'x' must be the name of a data frame, the name of a list of data frames, a data frame object, a list object of data frames.

sos::findFn("writeXLS")

So after tracking down the package that has writeXLS (also named writeXLS), I see that the example in the help page quotes the names of dataframe objects and the help page says the x values should be _character_ (rather than R symbol/names). You didn't provide a character vector as the first argument which, since it was unnamed, was therefore matched to the 'x' parameter.

-- 
David.
> 
> -----Original Message-----
> From: David Winsemius [mailto:dwinsemius at comcast.net] 
> Sent: 17 June 2016 19:27
> To: T.Riedle
> Cc: peter dalgaard; R-help at r-project.org
> Subject: Re: [R] extracting coefficients from ar() output
> 
> 
>> On Jun 17, 2016, at 11:23 AM, T.Riedle <tr206 at kent.ac.uk> wrote:
>> 
>> Thank you very much, Peter. I played a bit and found a solution. 
>>> rollingarmaols<-rollapply(data,width=36,function(data) 
>>> ar(data,order.max=1,method="ols"))
>>> coefar<-apply(rollingarmaols, 1, getElement, "ar")
>>> head(coefar,50)
>> [1] 0.9430692 0.9140253 0.9236898 0.9426744 0.9465110 0.9318470 
>> 0.9033054 0.9206048 0.9243736 0.9129082 [11] 0.9181811 0.9350779 
>> 0.9464205 0.9410245 0.9335568 0.9201928 0.8869414 0.8320984 0.8185671 
>> 0.7989182 [21] 0.7454876 0.6388364 0.6797046 0.6704642 0.7077033 
>> 0.8895698 0.8755445 0.8965050 0.8969068 0.8891385 [31] 0.9284835 
>> 0.9628297 0.9674624 0.9524462 0.9423693 0.9629843 0.9996613 1.0000295 
>> 0.9845222 0.9877242 [41] 0.9582863 0.9596756 0.9415847 0.9471677 
>> 0.9447052 0.9324048 0.9171082 0.8928825 0.9133751 0.9203662
>> 
>> I am trying to export the data to Excel using WriteXLS:
>>> WriteXLS(coefar, ExcelFileName = "R.xls", SheetNames = test)
> 
> Just a guess (since you didn't include the error message) but do you have a length-1 character vector named `test`. I thought not. So try using SheetNames = 'test'
> 
> --
> David.
>> 
>> Unfortunately, it doesn't work. How can I export the data to Excel?
>> 
>> -----Original Message-----
>> From: peter dalgaard [mailto:pdalgd at gmail.com]
>> Sent: 16 June 2016 18:49
>> To: William Dunlap
>> Cc: T.Riedle; R-help at r-project.org
>> Subject: Re: [R] extracting coefficients from ar() output
>> 
>> 
>>> On 16 Jun 2016, at 17:07 , William Dunlap via R-help <r-help at r-project.org> wrote:
>>> 
>>> help(ar) should tell you how to get the coefficients.  If, like me, 
>>> you don't read help files, you can use str() to look at the structure 
>>> of ar's output.
>> 
>> Also notice that the output of rollapply is not an ar object. More likely a list of them, so  try rollingarma[[i]]$ar or maybe lapply(rollingarma, function(x)x$ar) or sapply(rollingarma, "[[", "ar") or...
>> 
>>> 
>>>> str(a <- ar(sin(1:30), aic=TRUE))
>>> List of 14
>>> $ order       : int 2
>>> $ ar          : num [1:2] 1.011 -0.918
>>> $ var.pred    : num 0.0654
>>> $ x.mean      : num 0.00934
>>> $ aic         : Named num [1:15] 61.215 53.442 0 0.985 2.917 ...
>>> ..- attr(*, "names")= chr [1:15] "0" "1" "2" "3" ...
>>> $ n.used      : int 30
>>> $ order.max   : num 14
>>> $ partialacf  : num [1:14, 1, 1] 0.5273 -0.9179 -0.1824 -0.0477 -0.0393 ...
>>> $ resid       : num [1:30] NA NA -0.0145 -0.0734 -0.0725 ...
>>> $ method      : chr "Yule-Walker"
>>> $ series      : chr "sin(1:30)"
>>> $ frequency   : num 1
>>> $ call        : language ar(x = sin(1:30), aic = TRUE)
>>> $ asy.var.coef: num [1:2, 1:2] 0.00583 -0.00308 -0.00308 0.00583
>>> - attr(*, "class")= chr "ar"
>>>> a$ar
>>> [1]  1.0112512 -0.9178554
>>> 
>>> 
>>> 
>>> 
>>> Bill Dunlap
>>> TIBCO Software
>>> wdunlap tibco.com
>>> 
>>> On Thu, Jun 16, 2016 at 4:34 AM, T.Riedle <tr206 at kent.ac.uk> wrote:
>>> 
>>>> Hi everybody,
>>>> 
>>>> I am trying to run an AR1 model using the ar() function as shown below.
>>>> 
>>>>> rollingarma<-rollapply(data,width=36,function(data)
>>>>> ar(data,aic=TRUE))
>>>>> head(rollingarma,50)
>>>>    order ar        var.pred x.mean   aic        n.used order.max
>>>> partialacf resid      method        series
>>>> [1,] 1     0.7433347 1.382908 49.99861 Numeric,16 36     15
>>>> Numeric,15 Numeric,36 "Yule-Walker" "data"
>>>> [2,] 1     0.7410181 1.565755 49.94778 Numeric,16 36     15
>>>> Numeric,15 Numeric,36 "Yule-Walker" "data"
>>>> [3,] 1     0.7636966 1.660581 49.86861 Numeric,16 36     15
>>>> Numeric,15 Numeric,36 "Yule-Walker" "data"
>>>> 
>>>> 
>>>> I get the table as shown above if I use head().
>>>> 
>>>> How can I extract the ar coefficients from this table? I have 
>>>> already tried coef() and rollingarma$ar but both do not work.
>>>> What can I do?
>>>> 
>>>> Thanks for your help.
>>>> 
>>>> 
>>>>      [[alternative HTML version deleted]]
>>>> 
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>>>> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>>> 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.
>> 
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 
>> 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>> 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.
> 
> David Winsemius
> Alameda, CA, USA
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list