[R] Entering a table

David Winsemius dwinsemius at comcast.net
Thu Aug 23 00:19:46 CEST 2012


On Aug 21, 2012, at 5:48 AM, Thomas wrote:

> I'm trying to enter a frequency table manually so that I can run a  
> goodness of fit test (I only have the frequencies, I don't have the  
> raw data).
>
> So for example, let's say I want to re-create the HorseKicks table:
>
> library(vcd)
>
> data(HorseKicks)
> str(HorseKicks)
>
> 'table' int [1:5(1d)] 109 65 22 3 1
> - attr(*, "dimnames")=List of 1
>  ..$ nDeaths: chr [1:5] "0" "1" "2" "3" ...
>
> I tried this the following but it didn't work:
>
> vex <- matrix(c(0,109,1,65,2,22,3,3,4,1), nrow=2, ncol=5)
> vec <- as.table(vex)
> str(vec)
>
> table [1:2, 1:5] 0 109 1 65 2 22 3 3 4 1
> - attr(*, "dimnames")=List of 2
>  ..$ : chr [1:2] "A" "B"
>  ..$ : chr [1:5] "A" "B" "C" "D" ...
>
> I also tried:
>
> vex <- c(109,65,22,3,1)
> vec <- as.table(vex)
> str(vec)
>
> table [1:5(1d)] 109 65 22 3 1
> - attr(*, "dimnames")=List of 1
>  ..$ : chr [1:5] "A" "B" "C" "D" ...
>
> goodfit(HorseKicks) works fine
> goodfit(vec) does not

You needed a two _column_ matrix with the frequencies in the _first_  
column, rather than a two-row matrix with frequencies in the second row:

 > vcd::goodfit( t(vex)[ , 2:1])  # transpose to column oriented and  
swap columns

Observed and fitted values for poisson distribution
with parameters estimated by `ML'

  count observed      fitted
      0      109 108.6701738
      1       65  66.2888060
      2       22  20.2180858
      3        3   4.1110108
      4        1   0.6269291
>


-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list