[R] Problems with basic loop

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Fri Jun 20 12:14:56 CEST 2008


Michael Pearmain wrote:
> I'm having trouble creating a looping variable and i can't see wher ethe
> problem arises from any hep gratfully appreciated
>
> First create a table
>
> x<-table(SURVEY$n_0,exposed)
>   
>> x
>>     
>           exposed
>            False True
>   Under 16    24    1
>   16-19       68    9
>   20-24      190   37
>   25-34      555  204
>   35-44      330   87
>   45-54      198   65
>   55-64       67   35
>   65+         10    8
>
> Now ectors to store counts and column proportions
>
>   
>> xT<-x[,"True"]
>> xF<-x[,"False"]
>> yT<-x[,"True"]/colSums(x)
>> yF<-x[,"False"]/colSums(x)
>>     
>
> check length for dynamic looping
>   
>> length(yT)
>>     
> [1] 8
>
> now create loop
>   
>> for(i in 1:length(yT)){
>>     
> + pwr.2p2n.test(2*(asin(sqrt(yT[i]))-asin(sqrt(yF[i]))),n1=xT[i],n2=xF[i])
> + }
> Error in pwr.2p2n.test(2 * (asin(sqrt(yT[i])) - asin(sqrt(yF[i]))), n1 =
> xT[i],  :
>   number of observations in the first group must be at least 2
>
> this confuses me as if i enter the data as values the procedure works?
>
> Thanks in advance
>   
Er, the first row "under 16" has a count of 1 in the "True" column and
it confuses you that you get an error saying that you need at least 2??

But what looks _really_ confused is what you are trying to do in the
first place: The p's you are passing to pwr.2p2n are the empirical
relative frequencies of the individual age groups. This sort of reverses
cause and effect (presumably the exposure does not cause middle age) and
it is pretty odd to compare a particular  row in a table with everything
else jumbled together but worse, it is post-hoc power calculation, which
is just a plain Bad Idea (as several people have pointed out before).

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list