[R] chisq.test(): standardized (adjusted) Pearson residuals

David Winsemius dwinsemius at comcast.net
Sat Aug 20 18:43:20 CEST 2011


On Aug 20, 2011, at 12:04 PM, Stephen Davies wrote:

>
>>>>> I'm using chisq.test() on a matrix of categorical data, and I see
>>>>> that the
>>>>> "residuals" attribute of the returned object will give me the
>>>>> Pearson residuals.
>>
>> Actually they are not an attribute in the R sense, but rather a list
>> value.
>
>    Oh. I was just going by:
>
>> attributes(my.chisq.test)
> $names
> [1] "statistic" "parameter" "p.value"   "method"    "data.name"  
> "observed"
> [7] "expected"  "residuals"
>
> $class
> [1] "htest"
>
>    which I interpreted as "this object has 8 attributes, called  
> 'statistic',
> 'parameter', ..., 'residuals'." Is that not the right terminology?

The names attribute let's you know what characters to use if you want  
to access values in a list. Unless you are doing programming  
attributes is not a particular useful function. It is much more common  
to access the names attribute with the  `names` function:

 > names(Xsq)
[1] "statistic" "parameter" "p.value"   "method"    "data.name"  
"observed"  "expected"
[8] "residuals" "stdres"

So "stdres" is not an attribute but rather one value in a particular  
attribute called "names".

You would get (much) more information by using str on the htest object  
as below:

 > str(Xsq)
List of 9
  $ statistic: Named num 30.1
   ..- attr(*, "names")= chr "X-squared"
  $ parameter: Named num 2
   ..- attr(*, "names")= chr "df"
  $ p.value  : num 2.95e-07
  $ method   : chr "Pearson's Chi-squared test"
  $ data.name: chr "M"
  $ observed : table [1:2, 1:3] 762 484 327 239 468 477
   ..- attr(*, "dimnames")=List of 2
   .. ..$ gender: chr [1:2] "M" "F"
   .. ..$ party : chr [1:3] "Democrat" "Independent" "Republican"
  $ expected : num [1:2, 1:3] 704 542 320 246 534 ...
   ..- attr(*, "dimnames")=List of 2
   .. ..$ gender: chr [1:2] "M" "F"
   .. ..$ party : chr [1:3] "Democrat" "Independent" "Republican"
  $ residuals: table [1:2, 1:3] 2.199 -2.505 0.411 -0.469 -2.843 ...
   ..- attr(*, "dimnames")=List of 2
   .. ..$ gender: chr [1:2] "M" "F"
   .. ..$ party : chr [1:3] "Democrat" "Independent" "Republican"
  $ stdres   : table [1:2, 1:3] 4.502 -4.502 0.699 -0.699 -5.316 ...
   ..- attr(*, "dimnames")=List of 2
   .. ..$ gender: chr [1:2] "M" "F"
   .. ..$ party : chr [1:3] "Democrat" "Independent" "Republican"
  - attr(*, "class")= chr "htest"

Now you can see that the values in the stdres object are really a list  
element and are in a table with particular row and column names. You  
get that object one of two ways. you ca use the "$" method as Dalgaard  
suggested or you can use "[[" with the name of the object:

Xsq[["stdres"]]


>
>
>>>>> That's cool. However, what I'd really like is the standardized
>>>>> (adjusted)
>>>>> Pearson residuals, which have a N(0,1) distribution. Is there a
>>>>> way to do that
>>>>> in R (other than by me programming it myself?)
>>>>
>>>> ?scale
>>>
>>> chisq.test(...)$stdres, more likely.
>
>    "scale" is not what I want. As for "$stdres," that would be  
> wonderful, but
> as you can see from the above list of attributes, it's not one of  
> the 8
> returned. What am I missing?

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list