[R] Odp: Loop overwrite and data output problems

RCulloch ross.culloch at dur.ac.uk
Mon Mar 1 11:46:12 CET 2010


Hi Petr,

Thank you for your post - I really appreciate you taking the time over my
problem. 

Apologies for not posting more data, it is just that the data set is rather
large, and I don't like posting the whole thing on the website for that
reason. 

I have managed to random sample the 98 individuals so that I effectively get
98 data points from the data set, I do this with the script below, which I
appreciate may not be the best way to do it:

for (S in 1:1){
	Sample.dat<-ALL.R[1,]
	for (I in 1:98){
		tmp<-ALL.R[ALL.R$ID2==I,]
		max<-dim(tmp)[1]
		if (I==1) Sample.dat[1,]<-tmp[sample(1:max,1),] else {
Sample.dat<-rbind(Sample.dat,tmp[sample(1:max,1),])

Here is the first section of the output for Sample.dat, some columns are not
used in the analysis and the majority of the variables are not shown here,
but what you can see is that the script has taken one data point from ID2
1:98 (columns are a bit skewed and ID2 appears under DAY because the numeric
for the rows is inc. in the e.g.) 

SITE_NAME SITE YEAR NAME DAY ID2 n_DAY BEH_F BEH_T
1           NR    1 2007   A1   3   1    78    22    56
31          NR    1 2007   A2   1   2    24     1    23
80          NR    1 2007   D2  23   3    36     2    34
106         NR    1 2007   D5  19   4    34     9    25
136         NR    1 2007   E2  19   5    36    12    24
160         NR    1 2007  F10  13   6    48     4    44
193         NR    1 2007   F4  16   7    36     5    31
222         NR    1 2007   F8  15   8    47     8    39
263         NR    1 2007   G3  26   9    30     6    24
292         NR    1 2007   G4  25  10    30     8    22
317         NR    1 2007   G5  20  11    36     6    30
339         NR    1 2007   H1  12  12    42    11    31
370         NR    1 2007   I1  13  13    48    16    32
411         NR    1 2007   I4  24  14    12     2    10
433         NR    1 2007   J1  16  15    36    11    25
477         NR    1 2007   J2  30  16    36     1    35
500         NR    1 2007   K1  23  17    33     6    27
537         NR    1 2007   K4  30  18    36     4    32
567         NR    1 2007   L1  30  19    36    12    24
592         NR    1 2007   L2  25  20    30     9    21
614         NR    1 2007   M2  17  21    36     4    32
644         NR    1 2007   M3  17  22    24     4    20
688         NR    1 2007   M4  31  23    36     3    33
707         NR    1 2007   N1  20  24    36     4    32
741         NR    1 2007   N4  24  25     1     0     1
776         NR    1 2007   P1  29  26    36    10    26
804         NR    1 2007   R1  27  27    18     0    18
836         NR    1 2007   R4  29  28    36     3    33
862         NR    1 2007   S1  25  29    30    11    19
897         NR    1 2007   S4  30  30    36     3    33
911         NR    1 2008   A1  11  31   110    24    86
930         NR    1 2008   A2   3  32   114    34    80
1159        NR    1 2008   A3  16  33   115    19    96
1178        NR    1 2008   A4   8  34   121    29    92
1205        NR    1 2008   A5   8  35    62    19    43
1246        NR    1 2008   B1  22  36   105    39    66
1258        NR    1 2008   C1   7  37    49    12    37
1289        NR    1 2008   C3  11  38   121    40    81
1328        NR    1 2008   D1  23  39    35    12    23
1354        NR    1 2008   F1  22  40   109    31    78
1377        NR    1 2008   G1  18  41   111    20    91
1400        NR    1 2008   G2  14  42   115    15   100
978         NR    1 2008   H1  24  43    91    23    68
1438        NR    1 2008   H2  25  44    91    18    73
1003        NR    1 2008   I1  22  45   109    28    81
1452        NR    1 2008   I2  12  46    30    11    19
1491        NR    1 2008   I3  24  47    91    24    67
1025        NR    1 2008   I4  17  48    34     9    25
1059        NR    1 2008   J1  24  49    91    16    75
1512        NR    1 2008   J3  18  50    92    21    71
1535        NR    1 2008   J4  14  51    44     2    42
1564        NR    1 2008   J6  16  52   115    13   102
1080        NR    1 2008   K1  18  53   111     4   107
1595        NR    1 2008   K2  20  54    41    12    29
1620        NR    1 2008   K3  18  55    27     7    20
1104        NR    1 2008   L2  15  56    48     9    39
1650        NR    1 2008   L4  21  57   115    33    82
1677        NR    1 2008   L5  21  58   111    24    87
1143        NR    1 2008   N1  28  59    75    24    51
1701        NR    1 2008   N3  18  60   107    18    89
1735        NR    1 2008  NNB  25  61    91    11    80
1757        NR    1 2008   O1  20  62    20     9    11
2002        FA    0 2008  A10   8  63    95    28    67
2006        FA    0 2008  A11   2  64    46    14    32
2020        FA    0 2008  A12   6  65    97    30    67
2026        FA    0 2008  A13   2  66    88    33    55
2038        FA    0 2008  A14   4  67    97    32    65
2049        FA    0 2008  A15   5  68    92    34    58
2055        FA    0 2008  A16   1  69    20     5    15
1888        FA    0 2008   A2   4  70    97    28    69
1916        FA    0 2008   A3   2  71    88    19    69
1929        FA    0 2008   A4   5  72    91    28    63
1937        FA    0 2008   A5   3  73    97    30    67
2069        FA    0 2008   A6   5  74    91    18    73
2082        FA    0 2008   A7   8  75    95    17    78
2085        FA    0 2008   A8   1  76    82    13    69
2098        FA    0 2008   A9   4  77    97    29    68
1947        FA    0 2008   B1   3  78    97    18    79
1987        FA    0 2008   B2   3  79    97    24    73
1958        FA    0 2008   C1   4  80    96    22    74
2114        FA    0 2008   C2  10  81    91    17    74
1973        FA    0 2008   D1   9  82    97    34    63
1904        FA    0 2008   D2  10  83    48    20    28
2124        FA    0 2008   E1  10  84    91    20    71
1910        FA    0 2008   E2   6  85    31    13    18
1983        FA    0 2008   F1   9  86    97    20    77
1765        FA    0 2007   A1   1  87    30    11    19
1778        FA    0 2007   B1   4  88    36    20    16
1808        FA    0 2007  B13   4  89    36    25    11
1821        FA    0 2007   B2   7  90    33     8    25
1834        FA    0 2007   B3  10  91    15     3    12
1841        FA    0 2007   B4   7  92    35    11    24
1845        FA    0 2007   B6   1  93    30     8    22
1864        FA    0 2007   B7  10  94     9     4     5
1874        FA    0 2007   B8  10  95    18     4    14
1878        FA    0 2007   B9   4  96    36    14    22
1793        FA    0 2007   C1   9  97    36     5    31
1799        FA    0 2007   G1   5  98    36     5    31


I then run the random sample of data through a model then using the MuMIn
package and the dredge command I get a list of all of the possible subsets
of the model in order of lowest to highest AIC values, the script below is
shown for that:

		m1.R<-glm(cbind(Sample.dat$BEH_T, Sample.dat$BEH_F) ~ Sample.dat$SITE +
Sample.dat$YEAR + Sample.dat$PRECIP_MM_DAY + Sample.dat$PUP_AGE_EST +
Sample.dat$MO_AIR_TEMP, family="binomial") 
mod<-dredge(m1.R)

The problem occurs here, manually I can do this fine, but for a 100 or so
simulations you don't want to do it manually. So I want to take mod[1,] for
each simulation and store that to look at later, but any way in which I try
to do this results in the data being overwritten.

I'm not sure if this is any clear, I am sorry, It is taking me a little
longer to get my head around looping scripts!

Best wishes,

Ross





-- 
View this message in context: http://n4.nabble.com/Loop-overwrite-and-data-output-problems-tp1570593p1573407.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list