[R-sig-Geo] FW: Extracting p values from a series of wilcoxon tests done in a loop

Kerstin Kober Kerstin.Kober at jncc.gov.uk
Fri Apr 19 17:58:11 CEST 2013



Hello everybody,

I've got some code which runs wilcoxon tests for each 'year' and 'hotspot_ID' combination from my data. It basically compares the densities each hotspot_Id with the densities in hotspot 0 (one of the hotspot_ID's which means 'everywhere else that is not in a hotspot') - some test data is attached.

It looks like this. It gives me a nice, and very big, word document with all the tests in there.


d<-read.csv("F:\\Offshore_analysis1_(UK_wide_approach)\\Extra_analyses\\test_regularity_without_nim_numbers\\3_data_for_R\\6360_wn.csv<file:///\\Extra_analyses\test_regularity_without_nim_numbers\3_data_for_R\6360_wn.csv>", header=T)



# all tests between 0 and the other groups within each year together

for(i in 1980:2005){

    for(j in 1:34){

        tmp<- d[d$YEAR == i & d$hotspot_ID %in% c(0, j), ]

        print(c(i, j))

        try(print(wilcox.test(DENSITY~factor(hotspot_ID), data= tmp, na.action = na.omit)))

    }

}




Note that many of these did not run as there is not data for every combination of year and hotspot.

I want to be able to quickly extract which combinations ran, and of those, which had a P value less than some threshold, lets say 0.05.

So I'd like to extract some kind of table or ideally a .csv file with 3 columns:
Year, Hotspot_ID, P-value.

My attempts at altering the code to do that are here.


d<-read.csv("F:\\offshore_SPAs\\1_Stage1\\2_analysis_GL1.4\\2_UK_wide\\3_data_for_R.\\220_br.csv", header=T)



# all tests between 0 and the other groups within each year together

  output.2<-matrix( ncol=3, nrow=3000)



for(i in 1980:2005){

    for(j in 1:34){

        tmp<- d[d$YEAR == i & d$hotspot_ID %in% c(0, j), ]

        print(c(i, j))

        try(wt<-(wilcox.test(DENSITY~factor(hotspot_ID), data= tmp, na.action = na.omit)

        )

        output.2$p<-wt$p.value)

      }

}





I've played around quite a bit with the placement of the output bits,
I'm hoping someone can spot either where I'm going wrong with my attempts, or suggest a better way of doing this. I've also played a bit with matrix, and less so with sapply, but don't really know what I'm doing with those.

Any advice greatly appreciated.

Thanks


Kerstin



_____________________________________________________________________
The Joint Nature Conservation Committee (JNCC) is the statutory adviser to Government on UK and international nature conservation, on behalf of the Council for Nature Conservation and the Countryside, Cyfoeth Naturiol Cymru (Natural Resources Wales), Natural England and Scottish Natural Heritage.  Its work contributes to maintaining and enriching biological diversity, conserving geological features and sustaining natural systems.

JNCC SUPPORT CO. Registered in England and Wales, company no. 05380206. Registered office:  Monkstone House, City Road, Peterborough, Cambridgeshire PE1 1JY

If you have a Freedom of Information/Environmental Information request please refer to our website page

This message has been checked for all known viruses by JNCC delivered through the MessageLabs Virus Control Centre.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20130419/f16e8950/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_data.csv
Type: application/octet-stream
Size: 3494 bytes
Desc: test_data.csv
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20130419/f16e8950/attachment.obj>


More information about the R-sig-Geo mailing list