[R] [R ] Writing loop to estimate ARCH test for a multiple columns of a data frame?

Subhamitra Patra @ubh@m|tr@@p@tr@ @end|ng |rom gm@||@com
Fri May 8 15:25:20 CEST 2020


Dear Sir,

Thank you very much for your wonderful suggestion for my problem. Your
suggested code has excellently worked and successfully extracted the
statistics and p-value in another R object.

Concerning your last suggestion, I attempted to separate the strings with
TAB character in the "spout" object by using different alternative packages
like dplyr, tidyr, qdap, ans also by using split,strsplit function so that
can export the statistics and p-values for each column to excel, and later
to the MSword file, but got the below error.

By using the  split function, I wrote the code as,
*string[] split = s.Split(spout, '\t')*
where I got the following errors.
Error: unexpected symbol in "string[] split"
Error: unexpected symbol in "string[[]]split"
Error in strsplit(row, "\t") : non-character argument

Then I tried with  strsplit function by the below code
*strsplit(spout, split)*
But, got the below error as
Error in as.character(split) :
  cannot coerce type 'closure' to vector of type 'character'.

Then used dplyr and tidyr package and the wrote the below code
library(dplyr)
library(tidyr)
*separate(spout,value,into=c(“ChiSq”,”p”),sep=”,”)*
*separate(spout,List of length 12,into=c(“ChiSq”,”p”),sep="\t")*
But, got the errors as,
Error: unexpected input in "separate(spout,value,into=c(“"
Error: unexpected symbol in "separate(spout,List of"

Then used qdap package with the code below

*colsplit2df(spout,, c("ChiSq", "p"), ",")*
*colsplit2df(spout,, c("ChiSq", "p"), sep = "\t")*
But got the following errors
Error in dataframe[, splitcol] : incorrect number of dimensions
In addition: Warning message:
In colsplit2df_helper(dataframe = dataframe, splitcol = splitcols[i],  :
  dataframe object is not of the class data.frame
Error in dataframe[, splitcol] : incorrect number of dimensions
In addition: Warning message:
In colsplit2df_helper(dataframe = dataframe, splitcol = splitcols[i],  :
  dataframe object is not of the class data.frame

Sir, please suggest me where I am going wrong in the above to separate
string in the "spout" object.

Thank you very much for your help.

[image: Mailtrack]
<https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&>
Sender
notified by
Mailtrack
<https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&>
05/08/20,
06:51:46 PM

On Fri, May 8, 2020 at 4:47 PM Jim Lemon <drjimlemon using gmail.com> wrote:

> 1) In general, *apply functions return a list with the number of elements
> equal to the number of columns or other elements of the input data. You can
> assign that list as I have to "spout" in the first example.
>
> 2) spout<-list() assigns the name "spout" to an empty list. As we are
> processing columns 2 to 12 of the input data, spout[[i-1]] assigns the
> results to elements 1 to 11 of the list "spout". Just a low trick.
>
> 1a) Yes, you can create a "wrapper" function that will return only the
> statistic and p.value.
>
> # create a function that returns only the
> # statistic and p.value as a string
> archStatP<-function(x) {
>  archout<-ArchTest(x)
>  return(sprintf("ChiSq = %f, p = %f",archout$statistic,archout$p.value))
> }
> # using "lapply", run the test on each column
> spout<-lapply(sp_8_5[,2:12],archStatP)
>
> Note that I should have used "lapply". I didn't check the output carefully
> enough.
>
> 2a) Now you only have to separate the strings in "spout" with TAB
> characters and import the result into Excel. I have to wash the dishes, so
> you're on your own.
>
> Jim
>
> On Fri, May 8, 2020 at 8:26 PM Subhamitra Patra <
> subhamitra.patra using gmail.com> wrote:
>
>> Dear Sir,
>>
>> Thank you very much for such an excellent solution to my problem. I was
>> trying sapply function since last days, but was really unable to write
>> properly. Now, I understood my mistake in using sapply function in the
>> code. Therefore, I have two queries regarding this which I want to discuss
>> here just for my learning purpose.
>>
>> 1. While using sapply function for estimating one method across the
>> columns of a data frame, one needs to define the list of the output table
>> after using sapply so that the test results for each column will be
>> consistently stored in an output object, right?
>>
>> 2. In the spout<- list() command, what spout[[i-1]]  indicates?
>>
>> Sir, one more possibility which I would like to ask related to my above
>> problem just to learn for further R programming language.
>>
>> After running your suggested code, all the results for each column are
>> being stored in the spout object. From this, I need only the statistics and
>> P-value for each column. So, my queries are:
>>
>> 1. Is there any way to extract only two values (i.e., statistics and
>> p-value) for each column that stored in spout object and save these two
>> values in another R data frame for each column?
>>  or
>> 2. Is there any possibility that the statistics and p-value
>> calculated for each column can directly export to a word file in a table
>> format (having 4 columns and 3 rows). In particular, is it possible to
>> extract both statistic and p-value results for each column to an MS word
>> file with the format of A1, A2, A3, A4 column results in 1st row, A5, A6,
>> A7, A8 column results in 2nd row, and A9, A10, A11, A12 column results in
>> the 3rd row of the table?
>>
>>
>> Like before, your suggestion will definitely help me to learn the
>> advanced R language.
>>
>> Thank you very much for your help.
>>
>> [image: Mailtrack]
>> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&> Sender
>> notified by
>> Mailtrack
>> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&> 05/08/20,
>> 03:47:26 PM
>>
>> On Fri, May 8, 2020 at 2:37 PM Jim Lemon <drjimlemon using gmail.com> wrote:
>>
>>> Hi Subhamitra,
>>> This isn't too hard:
>>>
>>> # read in the sample data that was
>>> # saved in the file "sp_8_5.tab"
>>> sp_8_5<-read.table("sp_8_5.tab",sep="\t",
>>>  header=TRUE,stringsAsFactors=FALSE)
>>> library(tseries)
>>> library(FinTS)
>>> # using "sapply", run the test on each column
>>> spout<-sapply(sp_8_5[,2:12],ArchTest)
>>>
>>> The list "spout" contains the test results. If you really want to use a
>>> loop:
>>>
>>> spout<-list()
>>> for(i in 2:12) spout[[i-1]]<-ArchTest(sp_8_5[,i])
>>>
>>> Jim
>>>
>>>
>>> On Fri, May 8, 2020 at 5:27 PM Subhamitra Patra <
>>> subhamitra.patra using gmail.com> wrote:
>>>
>>>> Dear Sir,
>>>>
>>>> Herewith I am pasting a part of my sample data having 12 columns below,
>>>> and want to calculate ARCH test for the 12 columns by using a loop.
>>>>
>>>>
>>
>> --
>> *Best Regards,*
>> *Subhamitra Patra*
>> *Phd. Research Scholar*
>> *Department of Humanities and Social Sciences*
>> *Indian Institute of Technology, Kharagpur*
>> *INDIA*
>>
>

-- 
*Best Regards,*
*Subhamitra Patra*
*Phd. Research Scholar*
*Department of Humanities and Social Sciences*
*Indian Institute of Technology, Kharagpur*
*INDIA*

	[[alternative HTML version deleted]]



More information about the R-help mailing list