R tells me " object 'train' not found " - r

In my customized function I met a strange problem.
I'm writing a function to do cross-validation with logistic and clogit(in survival) regression.Thus I need to generate a training set and testing set.I've marked the part to do it.
I need to compare the classic logistic regression and the conditional logistic regression.So I use an 'if' statement to distinguish those two functions.
Here's the problem.It seems that the glm function can find the train vector and doing well,but clogit can't find it!Even if the train vector is output correctly.
When I test each line out of my function gcv,clogit works again.
Can somebody tell me why is clogit not working with train?
I called this function as:
gcv(as.numeric(FNDX)~HIGD+DEG+CHK+AGP1+AGMN+NLV+LIV+WT+AGLP+MST+strata(STR),bbdm,method="clogit")
and the error message is
Error in `[.data.frame`(bbdm, train, ) : object 'train' not found
Do you need traceback() information?
and the data set is bbdm13 in http://www.umass.edu/statdata/statdata/stat-logistic.html.
There are NA in the original data,or use the sample after the code :)
Related codes are as following:
gcv<-function(formula,data=NULL,method="rpart",cross=5,times=10,k=7,layer=5,seed=0)
{
set=data;
n=nrow(set);
set.seed(as.vector(Sys.time()));
bb1=1:n;
bb2=rep(1:cross,ceiling(n/cross))[1:n];
bb2=sample(bb2,n);
samp=sample(c(1:n),size=n);
m=ceiling(n/cross);
smp<-mat.or.vec(cross,m);
j=rep(0,cross)
for (i in 1:n)
{
smp[bb2[i],j[bb2[i]]]=i
j[bb2[i]]=j[bb2[i]]+1
}
# Here we separate the original set into 5(variable cross)sets,
# each time we take one out and treat it as the testing set
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula","data"), names(mf), 0L)
mf <- mf[c(1L, m)]
mf$drop.unused.levels <- TRUE
mf[[1L]] <- as.name("model.frame")
mf <- eval(mf, parent.frame())
response<-model.response(mf)
#code copied from function.lm
reslvl<-length(levels(response))
tra<-mat.or.vec(reslvl,reslvl);
tes<-mat.or.vec(reslvl,reslvl);
for (i in 1:cross)
{
test<-smp[i,];
train<-setdiff(1:200,test);
show(train); #THe 'train' set can be shown here.
#some "if" and "else"statements are hidden
if (method=="logistic")#logistic is running well
{
bb.log<-step(glm(formula,set,family=binomial),trace=FALSE)
tra<-tra+as.vector(t(table(response[train],
bin(predict.glm(bb.log,set[train,],type="response")))))
tes<-tes+as.vector(t(table(response[test],
bin(predict.glm(bb.log,set[test,],type="response")))))
}
else if (method=="clogit")#clogit is meeting a problem.
{
library("survival")
bb.clog<-step(clogit(formula,bbdm[train,]),trace=FALSE)
tra<-tra+as.vector(t(table( response[train],
bin(predict(bb.clog,set[train,])))))
tes<-tes+as.vector(t(table( response[test],
bin(predict(bb.clog,set[test,])))))
}
}
tra<-tra/cross;
tes<-tes/cross;
trainrate=1-sum(diag(tra))/sum(tra)
testrate=1-sum(diag(tes))/sum(tes)
result<-list(Train=tra,TrainRate=trainrate,Test=tes,TestRate=testrate)
result
}
Sample Data:
STR OBS AGMT FNDX HIGD DEG CHK AGP1 AGMN NLV LIV WT AGLP MST
1 1 1 39 1 9 0 1 23 13 0 5 118 39 1
2 1 2 39 0 10 0 2 16 11 1 3 175 39 3
3 1 3 39 0 11 0 2 20 12 1 3 135 39 2
4 1 4 39 0 12 1 1 21 11 0 3 125 40 1
5 2 1 38 1 14 2 1 24 14 1 3 118 39 1
6 2 2 38 0 12 1 2 20 15 0 2 183 38 1
7 2 3 38 0 9 0 2 19 11 0 5 218 38 1
8 2 4 38 0 13 1 1 23 13 0 2 192 37 1
9 3 1 38 1 9 0 1 22 15 2 2 125 38 1
10 3 2 38 0 10 0 2 20 14 0 2 123 38 1
11 3 3 38 0 15 1 1 19 13 3 2 140 37 1
12 3 4 38 0 12 1 1 18 13 0 2 160 38 1
13 4 1 38 1 15 1 1 24 14 2 3 150 38 5
14 4 2 38 0 15 2 1 26 13 1 1 130 38 2
15 4 3 38 0 12 1 2 23 14 0 4 140 38 1
16 4 4 38 0 12 1 1 25 16 0 2 130 38 1
17 5 1 38 1 12 1 1 21 17 0 2 150 38 2
18 5 2 38 0 12 1 2 20 12 1 2 148 38 1
19 5 3 38 0 14 2 1 22 13 0 2 134 39 1
20 5 4 38 0 13 1 1 16 14 0 6 138 38 4
21 6 1 38 1 13 1 1 24 12 1 3 116 39 1
22 6 2 38 0 12 1 2 19 12 0 2 145 35 2
23 6 3 38 0 14 2 2 21 10 4 3 195 35 1
24 6 4 38 0 14 4 1 25 8 0 1 180 38 2
25 7 1 37 1 17 4 1 26 13 1 4 137 37 5
26 7 2 37 0 15 2 1 20 11 2 2 135 37 2
27 7 3 37 0 9 0 1 18 10 2 3 155 37 1
28 7 4 37 0 12 1 2 22 13 2 2 120 38 1
29 8 1 36 1 12 1 1 23 14 0 2 126 36 2
30 8 2 36 0 10 0 1 20 12 1 2 191 36 1
31 8 3 36 0 10 0 2 17 10 1 3 185 37 1
32 8 4 36 0 12 1 2 23 12 0 2 119 37 1
33 9 1 35 1 12 1 1 23 14 0 3 129 36 1
34 9 2 35 0 14 1 2 21 11 0 3 170 34 2
35 9 3 36 0 12 1 1 22 14 0 4 110 36 1
36 9 4 35 0 14 2 2 24 11 0 2 155 35 1
37 10 1 35 1 12 1 2 21 12 0 2 105 29 1
38 10 2 36 0 17 3 1 26 13 1 2 115 36 1
39 10 3 36 0 12 1 2 22 12 2 3 120 36 1
40 10 4 36 0 12 1 1 33 16 0 1 150 36 1
Structure:
structure(list(STR = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L,
13L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L,
17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 20L,
20L, 20L, 20L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 23L, 23L,
23L, 23L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 26L, 26L, 26L,
26L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L,
30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 32L, 32L, 32L, 32L, 33L,
33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L, 36L, 36L,
36L, 36L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 38L, 39L, 39L, 39L,
39L, 40L, 40L, 40L, 40L, 41L, 41L, 41L, 41L, 42L, 42L, 42L, 42L,
43L, 43L, 43L, 43L, 44L, 44L, 44L, 44L, 45L, 45L, 45L, 45L, 46L,
46L, 46L, 46L, 47L, 47L, 47L, 47L, 48L, 48L, 48L, 48L, 49L, 49L,
49L, 49L, 50L, 50L, 50L, 50L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50"), class = "factor"), OBS = structure(c(1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
AGMT = c(39L, 39L, 39L, 39L, 38L, 38L, 38L, 38L, 38L, 38L,
38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L,
38L, 38L, 37L, 37L, 37L, 37L, 36L, 36L, 36L, 36L, 35L, 35L,
36L, 35L, 35L, 36L, 36L, 36L, 35L, 35L, 35L, 35L, 34L, 35L,
34L, 34L, 33L, 33L, 32L, 33L, 33L, 33L, 33L, 33L, 32L, 32L,
32L, 32L, 31L, 30L, 31L, 31L, 68L, 68L, 68L, 68L, 64L, 64L,
64L, 64L, 63L, 63L, 63L, 63L, 62L, 62L, 62L, 62L, 61L, 61L,
61L, 61L, 61L, 62L, 62L, 61L, 61L, 62L, 61L, 61L, 61L, 61L,
61L, 61L, 60L, 60L, 60L, 60L, 58L, 58L, 58L, 58L, 55L, 55L,
55L, 55L, 55L, 55L, 55L, 55L, 52L, 52L, 52L, 52L, 52L, 52L,
52L, 52L, 51L, 51L, 51L, 51L, 49L, 49L, 49L, 49L, 48L, 48L,
48L, 48L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 46L, 46L,
46L, 46L, 46L, 46L, 46L, 46L, 45L, 45L, 45L, 45L, 45L, 45L,
45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 44L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 15L, 15L, 12L, 12L, 12L, 12L, 14L, 13L, 13L, 12L, 14L,
14L, 17L, 15L, 9L, 12L, 12L, 10L, 10L, 12L, 12L, 14L, 12L,
14L, 12L, 17L, 12L, 12L, 20L, 10L, 12L, 14L, 12L, 18L, 12L,
12L, 20L, 15L, 12L, 14L, 18L, 12L, 13L, 18L, 12L, 12L, 15L,
12L, 17L, 10L, 13L, 13L, 14L, 8L, 16L, 12L, 12L, 20L, 13L,
12L, 10L, 12L, 5L, 12L, 12L, 12L, 16L, 10L, 8L, 13L, 8L,
16L, 11L, 9L, 15L, 14L, 12L, 18L, 6L, 12L, 10L, 8L, 12L,
8L, 13L, 12L, 11L, 13L, 12L, 12L, 13L, 12L, 14L, 12L, 12L,
11L, 12L, 12L, 12L, 10L, 12L, 14L, 8L, 12L, 12L, 14L, 9L,
12L, 7L, 16L, 15L, 15L, 20L, 12L, 12L, 14L, 17L, 12L, 12L,
12L, 17L, 15L, 12L, 10L, 12L, 10L, 11L, 17L, 10L, 12L, 14L,
8L, 12L, 12L, 12L, 11L, 12L, 12L, 8L, 13L, 12L, 12L, 12L,
19L, 12L, 12L, 13L, 12L, 17L, 12L, 16L, 14L, 16L, 18L, 12L,
12L, 12L, 12L, 12L, 12L, 16L, 16L, 12L, 12L, 16L, 11L, 12L,
12L, 16L, 12L, 12L, 11L, 12L, 12L, 16L, 12L, 12L, 12L, 12L,
16L, 10L, 11L, 15L, 12L, 14L, 10L, 15L, 13L), DEG = structure(c(1L,
1L, 1L, 2L, 3L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 3L, 2L, 2L,
2L, 2L, 3L, 2L, 2L, 2L, 3L, 5L, 5L, 3L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 2L, 4L, 2L, 2L, 5L, 1L, 2L, 2L, 2L, 5L,
2L, 2L, 5L, 2L, 2L, 3L, 5L, 2L, 2L, 5L, 2L, 2L, 2L, 2L, 4L,
1L, 2L, 2L, 3L, 1L,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, 1L, 2L,
2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L), .Label = c("1",
"2"), class = "factor"), AGP1 = c(23, 16, 20, 21, 24, 20,
19, 23, 22, 20, 19, 18, 24, 26, 23, 25, 21, 20, 22, 16, 24,
19, 21, 25, 26, 20, 18, 22, 23, 20, 17, 23, 23, 21, 22, 24,
21, 26, 22, 33, 26, 18, 19, 21, 25, 27, 20, 25, 26, 21, 24,
25, 28, 21, 20, 21, 30, 25, 20, 23, 30, 21, 23, 24, 22, 34,
23, 19, 30, 28, 26, 25, 21, 24, 24, 24, 26, 26, 32, 22, 28,
26, 28, 27, 22, 30, 25, 26, 26, 33, 25, 29, 21, 18, 22, 23,
28, 25, 24, 33, 20, 25, 24, 24, 30, 30, 30, 24, 24, 23, 16,
26, 24, 28, 20, 25, 23, 21, 23, 20, 24, 24, 22, 24, 25, 25,
24, 25, 22, 22, 23, 19, 26, 20, 24, 22, 19, 23, 23, 21, 27,
19, 26, 15, 27, 23, 22, 17, 33, 25, 20, 22, 24, 23, 20, 30,
18, 22, 30, 22, 25, 23, 23, 23, 25, 27, 27, 25, 24, 22, 23,
18, 27, 31, 14, 20, 29, 22, 20, 23, 29, 28, 23, 26, 21, 27,
26, 25, 25, 20, 21, 22, 40, 21, 21, 26, 34, 21, 30, 21),
AGMN = c(13L, 11L, 12L, 11L, 14L, 15L, 11L, 13L, 15L, 14L,
13L, 13L, 14L, 13L, 14L, 16L, 17L, 12L, 13L, 14L, 12L, 12L,
10L, 8L, 13L, 11L, 10L, 13L, 14L, 12L, 10L, 12L, 14L, 11L,
14L, 11L, 12L, 13L, 12L, 16L, 11L, 13L, 11L, 12L, 10L, 13L,
11L, 16L, 14L, 11L, 12L, 12L, 14L, 12L, 13L, 13L, 13L, 11L,
9L, 16L, 14L, 14L, 11L, 13L, 12L, 14L, 13L, 12L, 14L, 14L,
11L, 10L, 15L, 12L, 14L, 11L, 16L, 15L, 12L, 12L, 14L, 13L,
15L, 14L, 16L, 11L, 15L, 13L, 17L, 11L, 13L, 13L, 15L, 13L,
17L, 15L, 17L, 11L, 13L, 15L, 12L, 16L, 12L, 10L, 16L, 13L,
12L, 14L, 14L, 14L, 12L, 15L, 12L, 12L, 14L, 13L, 14L, 12L,
11L, 11L, 16L, 12L, 13L, 13L, 14L, 12L, 13L, 13L, 11L, 11L,
12L, 11L, 14L, 12L, 14L, 13L, 12L, 15L, 13L, 12L, 15L, 11L,
13L, 13L, 12L, 12L, 11L, 13L, 14L, 13L, 11L, 11L, 12L, 11L,
12L, 12L, 15L, 17L, 13L, 10L, 16L, 12L, 13L, 12L, 12L, 13L,
14L, 13L, 15L, 15L, 12L, 17L, 15L, 12L, 12L, 14L, 12L, 12L,
11L, 16L, 12L, 11L, 12L, 11L, 17L, 11L, 13L, 12L, 16L, 13L,
14L, 12L, 15L, 16L, 12L, 14L, 13L, 13L, 12L, 12L), NLV = c(0,
1, 1, 0, 1, 0, 0, 0, 2, 0, 3, 0, 2, 1, 0, 0, 0, 1, 0, 0,
1, 0, 4, 0, 1, 2, 2, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2,
0, 0, 2, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
0, 0, 1, 0, 0, 1, 0, 0, 0, 1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, 138L, 116L, 145L, 195L, 180L, 137L, 135L, 155L,
120L, 126L, 191L, 185L, 119L, 129L, 170L, 110L, 155L, 105L,
115L, 120L, 150L, 135L, 110L, 170L, 145L, 170L, 140L, 240L,
100L, 92L, 160L, 155L, 132L, 110L, 145L, 155L, 110L, 129L,
131L, 218L, 115L, 110L, 130L, 97L, 120L, 130L, 150L, 123L,
145L, 135L, 132L, 205L, 127L, 120L, 145L, 175L, 144L, 123L,
170L, 134L, 155L, 125L, 140L, 120L, 134L, 150L, 117L, 147L,
124L, 129L, 170L, 153L, 130L, 145L, 140L, 155L, 116L, 115L,
175L, 179L, 119L, 153L, 185L, 280L, 140L, 126L, 193L, 140L,
116L, 140L, 138L, 175L, 155L, 125L, 113L, 110L, 190L, 114L,
126L, 159L, 170L, 156L, 161L, 150L, 115L, 95L, 235L, 145L,
123L, 145L, 155L, 115L, 190L, 120L, 110L, 148L, 120L, 132L,
115L, 125L, 120L, 155L, 170L, 180L, 179L, 137L, 107L, 144L,
189L, 80L, 142L, 150L, 154L, 90L, 150L, 102L, 110L, 101L,
109L, 210L, 198L, 124L, 133L, 120L, 165L, 130L, 240L, 125L,
183L, 130L, 105L, 123L, 180L, 130L, 104L, 158L, 160L, 108L,
127L, 145L, 127L, 132L, 140L, 178L, 130L, 130L, 265L, 195L,
125L, 105L, 161L, 135L, 185L, 115L, 140L, 145L, 195L, 138L,
118L, 129L, 180L), AGLP = c(39L, 39L, 39L, 40L, 39L, 38L,
38L, 37L, 38L, 38L, 37L, 38L, 38L, 38L, 38L, 38L, 38L, 38L,
39L, 38L, 39L, 35L, 35L, 38L, 37L, 37L, 37L, 38L, 36L, 36L,
37L, 37L, 36L, 34L, 36L, 35L, 29L, 36L, 36L, 36L, 35L, 35L,
36L, 36L, 34L, 35L, 34L, 35L, 33L, 33L, 32L, 33L, 33L, 29L,
29L, 33L, 32L, 32L, 26L, 32L, 30L, 30L, 31L, 31L, 50L, 53L,
35L, 46L, 53L, 44L, 42L, 50L, 52L, 46L, 51L, 50L, 33L, 39L,
53L, 39L, 53L, 50L, 41L, 45L, 56L, 36L, 52L, 52L, 34L, 54L,
50L, 55L, 53L, 56L, 55L, 43L, 51L, 42L, 50L, 47L, 53L, 55L,
42L, 25L, 44L, 50L, 55L, 47L, 52L, 50L, 47L, 50L, 36L, 45L,
40L, 48L, 50L, 43L, 42L, 42L, 52L, 50L, 45L, 51L, 49L, 44L,
44L, 49L, 48L, 48L, 48L, 29L, 47L, 47L, 45L, 45L, 47L, 29L,
47L, 39L, 46L, 45L, 46L, 40L, 46L, 46L, 46L, 39L, 45L, 38L,
45L, 46L, 45L, 45L, 28L, 45L, 45L, 40L, 40L, 33L, 45L, 45L,
46L, 35L, 44L, 45L, 44L, 44L, 44L, 44L, 33L, 44L, 43L, 43L,
21L, 39L, 29L, 27L, 27L, 29L, 50L, 49L, 43L, 49L, 47L, 42L,
50L, 47L, 27L, 31L, 36L, 41L, 41L, 41L, 40L, 41L, 42L, 41L,
41L, 41L), MST = structure(c(1L, 3L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 5L, 2L, 1L, 1L, 2L, 1L, 1L, 4L, 1L, 2L,
1L, 2L, 5L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L,
1L, 5L, 3L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 4L, 5L,
4L, 1L, 5L, 4L, 4L, 1L, 5L, 3L, 1L, 5L, 1L, 4L, 4L, 1L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 4L, 1L, 4L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 5L, 1L, 1L, 1L, 1L, 3L,
5L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 4L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 1L, 1L,
4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L, 1L, 1L, 1L,
1L, 3L, 4L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("1",
"2", "3", "4", "5"), class = "factor")), .Names = c("STR",
"OBS", "AGMT", "FNDX", "HIGD", "DEG", "CHK", "AGP1", "AGMN",
"NLV", "LIV", "WT", "AGLP", "MST"), row.names = c(NA, -200L), class = "data.frame")

Could it be bbdm[train] that it can't find, rather than train itself? What error message do you get?
You can use the browser command to debug here. i.e.
gcv<-function(formula,data=NULL,method="rpart",cross=5,times=10,k=7,layer=5,seed=0)
{
set=data;
n=nrow(set);
set.seed(as.vector(Sys.time()));
bb1=1:n;
bb2=rep(1:cross,ceiling(n/cross))[1:n];
bb2=sample(bb2,n);
samp=sample(c(1:n),size=n);
m=ceiling(n/cross);
smp<-mat.or.vec(cross,m);
j=rep(0,cross)
for (i in 1:n)
{
smp[bb2[i],j[bb2[i]]]=i
j[bb2[i]]=j[bb2[i]]+1
}
# Here we separate the original set into 5(variable cross)sets,
# each time we take one out and treat it as the testing set
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula","data"), names(mf), 0L)
mf <- mf[c(1L, m)]
mf$drop.unused.levels <- TRUE
mf[[1L]] <- as.name("model.frame")
mf <- eval(mf, parent.frame())
response<-model.response(mf)
#code copied from function.lm
reslvl<-length(levels(response))
tra<-mat.or.vec(reslvl,reslvl);
tes<-mat.or.vec(reslvl,reslvl);
for (i in 1:cross)
{
test<-smp[i,];
train<-setdiff(1:200,test);
show(train); #THe 'train' set can be shown here.
#some "if" and "else"statements are hidden
if (method=="logistic")#logistic is running well
{
bb.log<-step(glm(formula,set,family=binomial),trace=FALSE)
tra<-tra+as.vector(t(table(response[train],
bin(predict.glm(bb.log,set[train,],type="response")))))
tes<-tes+as.vector(t(table(response[test],
bin(predict.glm(bb.log,set[test,],type="response")))))
}
else if (method=="clogit")#clogit is meeting a problem.
{
##### BROWSER() CALL ##########
browser()
library("survival")
bb.clog<-step(clogit(formula,bbdm[train,]),trace=FALSE)
tra<-tra+as.vector(t(table( response[train],
bin(predict(bb.clog,set[train,])))))
tes<-tes+as.vector(t(table( response[test],
bin(predict(bb.clog,set[test,])))))
}
}
tra<-tra/cross;
tes<-tes/cross;
trainrate=1-sum(diag(tra))/sum(tra)
testrate=1-sum(diag(tes))/sum(tes)
result<-list(Train=tra,TrainRate=trainrate,Test=tes,TestRate=testrate)
result
}
Browser can be used to debug functions like this. Essentially, when you run the code, you'll enter into the environment at the moment browser was called. This will allow you to explore and see if the variables are what you thought they were. You can do an ls() to see which objects are defined, or try to find the value of train or (my suspicion) bbdm to see that they're all properly defined.

Related

Gtsummary output with mgcv gam

I have the following data set:
structure(list(Age = c(83L, 26L, 26L, 20L, 20L, 77L, 32L, 21L,
15L, 75L, 27L, 81L, 81L, 15L, 24L, 16L, 35L, 27L, 30L, 31L, 24L,
24L, 31L, 79L, 30L, 19L, 20L, 42L, 62L, 83L, 79L, 18L, 26L, 66L,
23L, 83L, 77L, 80L, 57L, 42L, 32L, 76L, 85L, 29L, 65L, 79L, 9L,
34L, 20L, 16L, 34L, 22L, 19L, 23L, 25L, 14L, 53L, 28L, 79L, 22L,
22L, 21L, 82L, 81L, 16L, 19L, 77L, 15L, 18L, 15L, 78L, 24L, 16L,
14L, 29L, 18L, 50L, 17L, 43L, 8L, 14L, 85L, 31L, 20L, 30L, 23L,
78L, 29L, 6L, 61L, 14L, 22L, 10L, 83L, 15L, 13L, 15L, 15L, 29L,
8L, 9L, 15L, 8L, 9L, 15L, 9L, 34L, 8L, 9L, 9L, 16L, 8L, 25L,
21L, 23L, 13L, 56L, 10L, 7L, 27L, 8L, 8L, 8L, 8L, 80L, 80L, 6L,
15L, 42L, 25L, 23L, 21L, 8L, 11L, 43L, 69L, 34L, 34L, 14L, 12L,
10L, 22L, 78L, 16L, 76L, 12L, 10L, 16L, 6L, 13L, 66L, 11L, 26L,
12L, 16L, 13L, 24L, 76L, 10L, 65L, 20L, 13L, 25L, 14L, 12L, 15L,
43L, 51L, 27L, 15L, 24L, 34L, 63L, 17L, 15L, 9L, 12L, 17L, 82L,
75L, 24L, 44L, 69L, 11L, 10L, 12L, 10L, 10L, 70L, 54L, 45L, 42L,
84L, 54L, 23L, 23L, 14L, 81L, 17L, 42L, 44L, 16L, 15L, 43L, 45L,
50L, 53L, 23L, 53L, 49L, 13L, 69L, 14L, 65L, 14L, 13L, 22L, 67L,
59L, 52L, 54L, 44L, 78L, 62L, 69L, 10L, 63L, 57L, 22L, 12L, 62L,
9L, 82L, 53L, 54L, 66L, 49L, 63L, 51L, 9L, 45L, 49L, 77L, 49L,
61L, 62L, 57L, 67L, 16L, 65L, 75L, 45L, 16L, 55L, 17L, 64L, 67L,
56L, 52L, 63L, 10L, 62L, 14L, 66L, 68L, 15L, 13L, 43L, 47L, 55L,
69L, 21L, 67L, 34L, 52L, 15L, 31L, 64L, 55L, 13L, 48L, 71L, 64L,
13L, 25L, 34L, 50L, 61L, 70L, 33L, 57L, 51L, 46L, 57L, 69L, 46L,
8L, 11L, 46L, 71L, 33L, 38L, 56L, 17L, 29L, 28L, 6L, 8L), Sex = structure(c(1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L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ean_AD_scaled = c(3.15891332561581, -0.0551328105526693,
0.582747640515478, 1.94179165777054, 1.7064645993306, 2.37250948563045,
1.015775832203, 1.36189033704266, -1.05640048650493, 0.184814975542474,
-0.143366705302007, 1.81560178585347, 2.06325078470728, -0.473088628698217,
0.414641167726219, 0.199887349084444, -0.60620959209809,
-0.17879228399189, -1.03483709078065, -1.43497010225613,
-0.958595084469815, 1.0203965598582, -1.44731404613503, -1.17191867788498,
-2.02547709312595, -1.22395687266857, -1.09952727795348,
-1.0830246791849, 1.21072653232248, 1.69997357714829, 1.53648783201423,
0.208688735094353, 0.0862394522314924, 1.08662698958276,
-0.731299290763917, 2.29307697689102, -0.660008064083659,
-1.21425334459264, 1.10191939777498, -2.0957781638801, -1.14947514355972,
0.248845058764562, 2.6526135953958, 0.197907037232212, -0.222469162066061,
1.92880961340592, 1.23328008397287, -1.17288683034607, -0.308282675662673,
-1.02603570477074, -1.32647101621898, -1.58316343919798,
-0.0440210607151585, -0.388375288352846, -0.935491446193807,
-0.63789458173376, 0.454577456746182, -1.77391147749773,
0.709267564407921, 0.125735671950958, -0.821073428064989,
-0.126534054558056, 0.519597695894384, 0.188005477971066,
0.212319306823438, -1.45807374053215, 1.5856655763446, -1.25641198358011,
-0.910847565366061, -1.1191763722206, 0.25300371365424, -0.750772357310844,
0.37932560636146, -0.871791414947088, -1.92771569802088,
-1.1752191976387, 0.210449012296334, -0.347778895382139,
-0.132254955464496, 0.953616043508016, -0.0862677135627232,
0.838977990728951, -1.8993092246739, -0.0254281327692267,
0.298022803094927, -1.21559555595915, 0.0134079829994995,
-0.763094297724715, 0.334768589686298, -1.12568939786794,
-2.11786964276497, -0.0434709740895377, 0.388237009696492,
1.30050066962355, -0.260645173884043, -0.60620959209809,
1.05945271027717, -0.275717547426008, -0.0238878902174922,
0.496604074943496, 0.534009965485611, -0.692903244295693,
-0.566933407028871, 0.125625654625835, -0.518305749324122,
1.79381835547894, -0.790708646330802, -0.227860010997131,
0.347420582075538, 0.784189362817269, -0.660118081408782,
1.29962053102256, -0.561652575422924, -0.710395998990384,
-1.29315777017148, -0.457356151205503, -1.01756437073621,
0.146528946399368, -1.07136284272178, -1.42968927065019,
0.798601632408495, -0.799730066990963, -0.431348055546223,
0.569545561500617, 2.32168148142323, 0.472070211440872, 1.65145593676866,
-0.814142336582189, -0.544489872703603, -0.315433801795725,
0.382626126115175, -0.623812364117908, 0.216279930527897,
-0.606099574772967, -0.367207954999011, 0.719829227619811,
-0.749122097433987, 0.934693063586709, -0.79026857703031,
-0.371872689584264, 0.0769979969210905, -0.793899148759394,
1.50414273842782, 0.730280873506577, -0.290569886317732,
0.303743704001367, 0.390877425499463, -1.00359217044547,
-0.534918365417827, 0.325967203676389, 0.129036191704673,
0.34434009697207, -0.141386393449775, -0.363401355549725,
-0.395416397160769, -0.0235578382421178, -1.13583299524436,
1.16781977552417, -1.31890182425046, 0.139377820266317, 0.0160483988024708,
0.481311666751279, -1.05475022662807, 0.839858129329941,
0.652498624644007, -0.350199276534864, -0.262075399110649,
0.178543988010412, -1.13198238886502, -0.05117218684821,
-1.29678834190056, 0.429603523943066, 1.05098137624263, -0.956504755292464,
0.502765045150433, -0.81678275238516, -1.50263075720731,
-0.826684311646306, 2.40100397283753, 2.06633126981075, -0.470558230220369,
0.484942238480364, 0.822035322659877, 0.143888530596397,
0.384056351341786, -0.63580425255641, 0.358422314587926,
-0.372422776209885, 0.0607154328027556, -0.113221958218067,
1.02710761669075, -0.349649189909243, 2.27195365046724, -0.507634068787109,
-0.326105482332738, -1.0396778530861, 1.06484355920824, 1.32151397872221,
-0.185173288849074, -0.651888785489516, -0.171311105883464,
-0.104200537557911, -0.693673365571561, -1.26609350819101,
0.411230630647381, -0.929770545287362, -0.481009876107135,
0.386146680519137, 0.0482834750637615, -0.198265350538812,
0.790020281048832, 0.926001694901924, -1.08918564939184,
0.50298507980068, -0.0694350628187722, 1.04966116834114,
0.00878725534429612, 1.48742010500899, 0.750194009353997,
0.423772605711498, -0.596418050162068, -0.652636903300361,
-0.308942779613417, 0.314437388003408, 0.679562886624478,
-1.24312189070515, -0.432712270377761, 0.00427654501421597,
-0.197935298563442, 0.228821905592019, 1.06957430418856,
-1.61612462980509, 1.9499329398297, -0.263285589687014, 0.156430505660519,
-0.322254875953402, -0.451085163673446, -0.35526007349056,
0.10780284795577, 0.408700232169533, -0.957604928543701,
-1.05662052115517, 1.00345389178912, -0.238751726184391,
0.300003114947154, -0.397946795638617, -0.0802167606809086,
0.943714484246865, 1.10973062785877, 1.76279346979401, 1.62087112038423,
0.25533608094687, 0.226841593739787, 0.869672824438507, -1.44960240649761,
-0.450315042397579, -0.199629565370345, 0.29813282042005,
0.760425620590513, 1.87391096816911, -0.454275666102039,
-0.0559029318285365, -0.343048150401812, -1.01371376435687,
0.68880434193488, -0.29222014619459, 1.16132875334186, -1.95715633422403,
-0.534368278792206, -0.560112332871189, 1.84508642898666,
-1.19150176175703, -0.772203732244971, -0.3443683583033,
-1.45684154649076, -0.633823940704178, -1.77454957798344,
0.279539892474118, -0.875532004001301, 1.26001429397797,
-0.536590628759707, 2.1869102581465, 0.211109116247078, 0.130246382281038,
-0.355810160116181, -0.898085555651692, -0.429741802599415,
1.13360438741065, 1.61338994227581, 0.588688576072169, 0.454137387445685,
0.747113524250528, 0.460848444278238, -0.38177424884541,
-0.169990897981981, -0.747361820232001, -0.760123829946369,
0.208028631143609, -1.28748087619509, 2.33950428809329, -0.973029357526068,
-1.06091119683501, 0.917530360867389, -0.35041931118511,
-1.90613029883158, -1.15057531681095, 0.65348878057012, 0.43147381847017
)), row.names = c(NA, -308L), class = c("tbl_df", "tbl",
"data.frame"))
I am using this gam model:
m1 <- gam(mean_AD_scaled ~ s(Age, bs = 'ad', k = -1) + Sex + ti(Age, by = Sex, bs ='fs'),
data = DF,
method = 'REML',
family = gaussian)
Output:
Family: gaussian
Link function: identity
Formula:
mean_AD_scaled ~ s(Age, bs = "ad", k = -1) + Sex + ti(Age,
by = Sex, bs = "fs")
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.04691 0.06976 0.672 0.502
SexFemale -0.12950 0.09428 -1.374 0.171
Approximate significance of smooth terms:
edf Ref.df F p-value
s(Age) 2.980 3.959 8.72 2.24e-06 ***
ti(Age):SexMale 2.391 2.873 23.47 < 2e-16 ***
ti(Age):SexFemale 1.000 1.000 43.40 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Rank: 48/49
R-sq.(adj) = 0.34 Deviance explained = 35.6%
-REML = 375.4 Scale est. = 0.63867 n = 308
But when I use gtsummary, I get a repeated value for each gender 'interaction':
tbl_regression(m1, tidy_fun = tidy_gam)
I see the following in a publication, which I am trying to replicate with gender and age:
I am not sure how to fix this. My goal is to print a table for a manuscript so any other gam-related information that can be added like edf and R^2.
I think you've found a bug in the handling of these types of interactions. While we work on a fix to the bug, this code should get you what you need. Thanks
library(gtsummary)
#> #BlackLivesMatter
library(mgcv)
packageVersion("gtsummary")
#> [1] ‘1.5.2’
m1 <- gam(marker ~ s(age, bs = 'ad', k = -1) + grade + ti(age, by = grade, bs ='fs'),
data = gtsummary::trial,
method = 'REML',
family = gaussian)
tbl_regression(m1, tidy_fun = gtsummary::tidy_gam) %>%
modify_table_body(
~ .x %>%
dplyr::select(-n_obs) %>%
dplyr::distinct()
) %>%
as_kable() # convert to kable to display on SO
Characteristic
Beta
95% CI
p-value
Grade
I
—
—
II
-0.39
-0.70, -0.08
0.014
III
-0.13
-0.43, 0.18
0.4
s(age)
>0.9
ti(age):gradeI
0.6
ti(age):gradeII
>0.9
ti(age):gradeIII
0.6
Created on 2022-02-21 by the reprex package (v2.0.1)

Obtaining intercept and slope per patient (as diagnostics in a repeated measurements study) using the lmLst and intervals functions

I have a repeated measurements dataset of 24 stroke patients in which I want to assess the effect of three different types of rehabilitation (Group) on functional recovery scores (Barthel_index). Each patients functional ability was measured weekly (Time_num) for 8 weeks.
The data looks as follows:
library(dplyr)
library(magrittr)
library(nlme)
library(lmer)
mydata <-
structure(list(Subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L), Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 2, 3, 4,
5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7,
8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2,
3, 4, 5, 6, 7, 8), Barthel_index = c(45L, 45L, 45L, 45L,
80L, 80L, 80L, 90L, 20L, 25L, 25L, 25L, 30L, 35L, 30L, 50L,
50L, 50L, 55L, 70L, 70L, 75L, 90L, 90L, 25L, 25L, 35L, 40L,
60L, 60L, 70L, 80L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
100L, 20L, 20L, 30L, 50L, 50L, 60L, 85L, 95L, 30L, 35L, 35L,
40L, 50L, 60L, 75L, 85L, 30L, 35L, 45L, 50L, 55L, 65L, 65L,
70L, 40L, 55L, 60L, 70L, 80L, 85L, 90L, 90L, 65L, 65L, 70L,
70L, 80L, 80L, 80L, 80L, 30L, 30L, 40L, 45L, 65L, 85L, 85L,
85L, 25L, 35L, 35L, 35L, 40L, 45L, 45L, 45L, 45L, 45L, 80L,
80L, 80L, 80L, 80L, 80L, 15L, 15L, 10L, 10L, 10L, 20L, 20L,
20L, 35L, 35L, 35L, 45L, 45L, 45L, 50L, 50L, 40L, 40L, 40L,
55L, 55L, 55L, 60L, 65L, 20L, 20L, 30L, 30L, 30L, 30L, 30L,
30L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 40L, 35L, 35L, 35L,
40L, 40L, 40L, 45L, 45L, 45L, 65L, 65L, 65L, 80L, 85L, 95L,
100L, 45L, 65L, 70L, 90L, 90L, 95L, 95L, 100L, 25L, 30L,
30L, 35L, 40L, 40L, 40L, 40L, 25L, 25L, 30L, 30L, 30L, 30L,
35L, 40L, 15L, 35L, 35L, 35L, 40L, 50L, 65L, 65L)), row.names = c(NA,
-192L), class = c("tbl_df", "tbl", "data.frame"))
head(mydata)
# A tibble: 6 x 4
Subject Group Time_num Barthel_index
<int> <fct> <dbl> <int>
1 1 A 1 45
2 1 A 2 45
3 1 A 3 45
4 1 A 4 45
5 1 A 5 80
6 1 A 6 80
To see if and how intercepts and slopes vary per patient I want to plot the intercepts and slopes using the lmList and interval functions.
Question 1 I don't understand why calling the lmList function () in lme4 gives me 48 warnings while the same function in nlme does not:
lmlist <-
lme4::lmList(Barthel_index ~ Time_num | Subject,
data=mydata)
> There were 48 warnings (use warnings() to see them)
lmlist <-
nlme::lmList(Barthel_index ~ Time_num | Subject,
data=mydata)
# Works fine
Question 2 I am trying to extract the confidence intervals for each regression slope, but this gives a warning and NaN for certain values:
lmlist <-
nlme::lmList(Barthel_index ~ Time_num | Subject,
data=mydata)
coefs <- coef(lmlist)
names(coefs) <- c("Intercepts", "Slopes")
intervals(lmlist)
> Warning message:
In summary.lm(el) : essentially perfect fit: summary may be unreliable
Question 3 Now that I have my new list of coefficients with confidence intervals, I'd like to plot them to see if and how much intercepts and slopes vary amongst patients. I'm trying to achieve something like the following:
Any help? Thanks.
Q1. The warnings are occurring in lme4::lmList because you're using a tibble as input: no warnings from
lme4::lmList(Barthel_index ~ Time_num | Subject,
data=as.data.frame(mydata))
(this is a harmless "infelicity" or buglet in lme4 ...)
Q2. If you look at the list of coefficients, you'll see that subject 5 is the problematic one. The data for this subject all have the same response value: thus it's not surprising that we can't compute confidence intervals on a linear regression fit ...
mydata[mydata$Subject=="5",]
# A tibble: 8 × 4
Subject Group Time_num Barthel_index
<int> <fct> <dbl> <int>
1 5 A 1 100
2 5 A 2 100
3 5 A 3 100
4 5 A 4 100
5 5 A 5 100
6 5 A 6 100
7 5 A 7 100
8 5 A 8 100
Q3 plot(intervals(lmlist))
For Q3, you could use the dotplot function in the lattice package:
require(lattice)
m0 <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)
dotplot(ranef(m0, condVar = TRUE))

Adding points to persp 3D plot - hide or obscure points when behind surface

Background:
I'm attempting to add a 3D plot to a Shiny application. I've added a button to rotate the plot ~ 90 degrees. I'd also like to include radio buttons to plot points on the surface.
Problem:
When points are plotted they simply appear on top of the image, even when they should be behind the surface.
Question:
Is there a way to plot the surface so that it's transparent and points appear either behind or in front? Or hide the points if they land out of eyesight?
Data:
d <- list(x = c(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6,
6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10), y = c(0, 0.5, 1, 1.5, 2, 2.5,
3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10),
z = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0.000147818839413345, 0.00112553487724733,
0.00210325091508131, 0.00308096695291529, 0.00405868299074927,
0.00503639902858325, 0.00601411506641723, 0.00699183110425121,
0.00796954714208519, 0.00894726317991917, 0.00992497921775315,
0.0109026952555871, 0.0118804112934211, 0.0128581273312551,
0.0138358433690891, 0.0148135594069231, 0.015791275444757,
0.016768991482591, 0.017746707520425, 0.018724423558259,
0.019702139596093, 0.00332663525507192, 0.0253299512993333,
0.0473332673435947, 0.0693365833878561, 0.0913398994321175,
0.113343215476379, 0.13534653152064, 0.157349847564902, 0.179353163609163,
0.201356479653424, 0.223359795697686, 0.245363111741947,
0.267366427786209, 0.28936974383047, 0.311373059874731, 0.333376375918993,
0.355379691963254, 0.377383008007516, 0.399386324051777,
0.421389640096038, 0.4433929561403, 0.0185048854236584, 0.140901484725856,
0.263298084028054, 0.385694683330252, 0.50809128263245, 0.630487881934648,
0.752884481236846, 0.875281080539044, 0.997677679841242,
1.12007427914344, 1.24247087844564, 1.36486747774784, 1.48726407705003,
1.60966067635223, 1.73205727565443, 1.85445387495663, 1.97685047425883,
2.09924707356102, 2.22164367286322, 2.34404027216542, 2.46643687146762,
0.0575583422570596, 0.438265663185897, 0.818972984114734,
1.19968030504357, 1.58038762597241, 1.96109494690124, 2.34180226783008,
2.72250958875892, 3.10321690968776, 3.48392423061659, 3.86463155154543,
4.24533887247427, 4.6260461934031, 5.00675351433194, 5.38746083526078,
5.76816815618962, 6.14887547711845, 6.52958279804729, 6.91029011897613,
7.29099743990496, 7.6717047608338, 0.129117933403967, 0.98314083577592,
1.83716373814787, 2.69118664051983, 3.54520954289178, 4.39923244526373,
5.25325534763568, 6.10727825000764, 6.96130115237959, 7.81532405475154,
8.6693469571235, 9.52336985949545, 10.3773927618674, 11.2314156642394,
12.0854385666113, 12.9394614689833, 13.7934843713552, 14.6475072737272,
15.5015301760991, 16.3555530784711, 17.209575980843, 0.23363441995763,
1.77895922624881, 3.32428403254, 4.86960883883118, 6.41493364512237,
7.96025845141355, 9.50558325770473, 11.0509080639959, 12.5962328702871,
14.1415576765783, 15.6868824828695, 17.2322072891607, 18.7775320954518,
20.322856901743, 21.8681817080342, 23.4135065143254, 24.9588313206166,
26.5041561269078, 28.0494809331989, 29.5948057394901, 31.1401305457813,
0.36143039040365, 2.75203425835922, 5.14263812631479, 7.53324199427035,
9.92384586222592, 12.3144497301815, 14.7050535981371, 17.0956574660926,
19.4862613340482, 21.8768652020038, 24.2674690699593, 26.6580729379149,
29.0486768058705, 31.439280673826, 33.8298845417816, 36.2204884097372,
38.6110922776927, 41.0016961456483, 43.3923000136039, 45.7829038815594,
48.173507749515, 0.494048345421132, 3.76182525870662, 7.02960217199211,
10.2973790852776, 13.5651559985631, 16.8329329118486, 20.1007098251341,
23.3684867384196, 26.636263651705, 29.9040405649905, 33.171817478276,
36.4395943915615, 39.707371304847, 42.9751482181325, 46.242925131418,
49.5107020447035, 52.778478957989, 56.0462558712744, 59.3140327845599,
62.5818096978454, 65.8495866111309, 0.608277972936286, 4.63160227964344,
8.65492658635059, 12.6782508930577, 16.7015751997649, 20.724899506472,
24.7482238131792, 28.7715481198863, 32.7948724265935, 36.8181967333006,
40.8415210400078, 44.8648453467149, 48.8881696534221, 52.9114939601292,
56.9348182668364, 60.9581425735435, 64.9814668802507, 69.0047911869578,
73.028115493665, 77.0514398003722, 81.0747641070793, 0.68169864474794,
5.19064825215217, 9.6995978595564, 14.2085474669606, 18.7174970743649,
23.2264466817691, 27.7353962891733, 32.2443458965776, 36.7532955039818,
41.262245111386, 45.7711947187903, 50.2801443261945, 54.7890939335987,
59.298043541003, 63.8069931484072, 68.3159427558114, 72.8248923632157,
77.3338419706199, 81.8427915780241, 86.3517411854284, 90.8606907928326,
0.698331143785818, 5.31729285196915, 9.93625456015249, 14.5552162683358,
19.1741779765192, 23.7931396847025, 28.4121013928858, 33.0310631010692,
37.6500248092525, 42.2689865174358, 46.8879482256192, 51.5069099338025,
56.1258716419859, 60.7448333501692, 65.3637950583525, 69.9827567665359,
74.6017184747192, 79.2206801829025, 83.8396418910859, 88.4586035992692,
93.0775653074525, 0.653010606586468, 4.9722093330084, 9.29140805943032,
13.6106067858523, 17.9298055122742, 22.2490042386961, 26.568202965118,
30.88740169154, 35.2066004179619, 39.5257991443838, 43.8449978708057,
48.1641965972277, 52.4833953236496, 56.8025940500715, 61.1217927764935,
65.4409915029154, 69.7601902293373, 74.0793889557592, 78.3985876821812,
82.7177864086031, 87.036985135025, 0.553337675961259, 4.21327116124787,
7.87320464653448, 11.5331381318211, 15.1930716171077, 18.8530051023943,
22.5129385876809, 26.1728720729675, 29.8328055582542, 33.4927390435408,
37.1526725288274, 40.812606014114, 44.4725394994006, 48.1324729846872,
51.7924064699738, 55.4523399552604, 59.112273440547, 62.7722069258337,
66.4321404111203, 70.0920738964069, 73.7520073816935, 0.418509049668882,
3.18664747819306, 5.95478590671724, 8.72292433524142, 11.4910627637656,
14.2592011922898, 17.027339620814, 19.7954780493381, 22.5636164778623,
25.3317549063865, 28.0998933349107, 30.8680317634349, 33.636170191959,
36.4043086204832, 39.1724470490074, 41.9405854775316, 44.7087239060558,
47.4768623345799, 50.2450007631041, 53.0131391916283, 55.7812776201525,
0.274945103406177, 2.09351057307846, 3.91207604275075, 5.73064151242304,
7.54920698209532, 9.36777245176761, 11.1863379214399, 13.0049033911122,
14.8234688607845, 16.6420343304568, 18.460599800129, 20.2791652698013,
22.0977307394736, 23.9162962091459, 25.7348616788182, 27.5534271484905,
29.3719926181628, 31.1905580878351, 33.0091235575073, 34.8276890271796,
36.6462544968519, 0.14939138421548, 1.1375086826693, 2.12562598112311,
3.11374327957693, 4.10186057803075, 5.08997787648456, 6.07809517493838,
7.06621247339219, 8.05432977184601, 9.04244707029983, 10.0305643687536,
11.0186816672075, 12.0067989656613, 12.9949162641151, 13.9830335625689,
14.9711508610227, 15.9592681594765, 16.9473854579304, 17.9355027563842,
18.923620054838, 19.9117373532918, 0.0610345623904979, 0.464734596487648,
0.868434630584799, 1.27213466468195, 1.6758346987791, 2.07953473287625,
2.4832347669734, 2.88693480107055, 3.2906348351677, 3.69433486926485,
4.098034903362, 4.50173493745915, 4.9054349715563, 5.30913500565345,
5.7128350397506, 6.11653507384775, 6.52023510794491, 6.92393514204206,
7.32763517613921, 7.73133521023636, 8.13503524433351, 0.0150842607904164,
0.114855871447028, 0.214627482103639, 0.31439909276025, 0.414170703416861,
0.513942314073472, 0.613713924730083, 0.713485535386694,
0.813257146043305, 0.913028756699917, 1.01280036735653, 1.11257197801314,
1.21234358866975, 1.31211519932636, 1.41188680998297, 1.51165842063958,
1.61143003129619, 1.71120164195281, 1.81097325260942, 1.91074486326603,
2.01051647392264, 0.00112075907879118, 0.00853377984279572,
0.0159468006068003, 0.0233598213708048, 0.0307728421348093,
0.0381858628988139, 0.0455988836628184, 0.0530119044268229,
0.0604249251908275, 0.067837945954832, 0.0752509667188366,
0.0826639874828411, 0.0900770082468456, 0.0974900290108502,
0.104903049774855, 0.112316070538859, 0.119729091302864,
0.127142112066868, 0.134555132830873, 0.141968153594877,
0.149381174358882, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(21L, 21L)), facetcol = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 6L, 6L, 1L, 2L, 2L, 3L, 4L, 4L, 5L, 6L, 6L, 7L, 8L,
9L, 9L, 10L, 11L, 11L, 12L, 13L, 13L, 14L, 1L, 3L, 4L, 5L,
7L, 8L, 9L, 11L, 12L, 13L, 15L, 16L, 17L, 19L, 20L, 21L,
23L, 24L, 25L, 27L, 2L, 4L, 6L, 9L, 11L, 13L, 15L, 17L, 19L,
22L, 24L, 26L, 28L, 30L, 33L, 35L, 37L, 39L, 41L, 44L, 3L,
6L, 9L, 12L, 15L, 18L, 21L, 25L, 28L, 31L, 34L, 37L, 40L,
44L, 47L, 50L, 53L, 56L, 59L, 62L, 3L, 7L, 11L, 15L, 19L,
23L, 28L, 32L, 36L, 40L, 44L, 48L, 52L, 56L, 60L, 64L, 68L,
72L, 76L, 80L, 4L, 8L, 13L, 18L, 23L, 27L, 32L, 37L, 42L,
46L, 51L, 56L, 61L, 65L, 70L, 75L, 80L, 84L, 89L, 94L, 4L,
9L, 14L, 19L, 24L, 29L, 34L, 39L, 45L, 50L, 55L, 60L, 65L,
70L, 75L, 80L, 85L, 90L, 95L, 100L, 4L, 9L, 14L, 19L, 24L,
29L, 34L, 39L, 44L, 49L, 54L, 59L, 64L, 69L, 74L, 78L, 83L,
88L, 93L, 98L, 3L, 8L, 12L, 17L, 21L, 26L, 30L, 35L, 39L,
43L, 48L, 52L, 57L, 61L, 66L, 70L, 75L, 79L, 83L, 88L, 3L,
6L, 10L, 14L, 17L, 21L, 24L, 28L, 32L, 35L, 39L, 42L, 46L,
49L, 53L, 57L, 60L, 64L, 67L, 71L, 2L, 5L, 7L, 10L, 12L,
15L, 18L, 20L, 23L, 25L, 28L, 30L, 33L, 35L, 38L, 41L, 43L,
46L, 48L, 51L, 2L, 3L, 5L, 6L, 8L, 9L, 11L, 12L, 14L, 16L,
17L, 19L, 20L, 22L, 23L, 25L, 27L, 28L, 30L, 31L, 1L, 2L,
3L, 3L, 4L, 5L, 6L, 6L, 7L, 8L, 9L, 10L, 10L, 11L, 12L, 13L,
13L, 14L, 15L, 16L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("(-0.357,3.59]", "(3.59,7.18]",
"(7.18,10.8]", "(10.8,14.4]", "(14.4,17.9]", "(17.9,21.5]",
"(21.5,25.1]", "(25.1,28.7]", "(28.7,32.3]", "(32.3,35.9]",
"(35.9,39.5]", "(39.5,43.1]", "(43.1,46.6]", "(46.6,50.2]",
"(50.2,53.8]", "(53.8,57.4]", "(57.4,61]", "(61,64.6]", "(64.6,68.2]",
"(68.2,71.8]", "(71.8,75.3]", "(75.3,78.9]", "(78.9,82.5]",
"(82.5,86.1]", "(86.1,89.7]", "(89.7,93.3]", "(93.3,96.9]",
"(96.9,100]", "(100,104]", "(104,108]", "(108,111]", "(111,115]",
"(115,118]", "(118,122]", "(122,126]", "(126,129]", "(129,133]",
"(133,136]", "(136,140]", "(140,144]", "(144,147]", "(147,151]",
"(151,154]", "(154,158]", "(158,161]", "(161,165]", "(165,169]",
"(169,172]", "(172,176]", "(176,179]", "(179,183]", "(183,187]",
"(187,190]", "(190,194]", "(194,197]", "(197,201]", "(201,204]",
"(204,208]", "(208,212]", "(212,215]", "(215,219]", "(219,222]",
"(222,226]", "(226,230]", "(230,233]", "(233,237]", "(237,240]",
"(240,244]", "(244,248]", "(248,251]", "(251,255]", "(255,258]",
"(258,262]", "(262,265]", "(265,269]", "(269,273]", "(273,276]",
"(276,280]", "(280,283]", "(283,287]", "(287,291]", "(291,294]",
"(294,298]", "(298,301]", "(301,305]", "(305,309]", "(309,312]",
"(312,316]", "(316,319]", "(319,323]", "(323,326]", "(326,330]",
"(330,334]", "(334,337]", "(337,341]", "(341,344]", "(344,348]",
"(348,352]", "(352,355]", "(355,359]"), class = "factor"))
Code
flip <- 1 # 1 or 2
theta = c(-300,120)[flip]
pmat <- persp(d$x, d$y, d$z, asp = 1,col = color[d$facetcol], phi = 30, theta = theta, border = "grey10"
,d = .8,r = 2.8,expand = .6,shade = .2,axes = F,box = T,cex = .1)
xx <- c(7.76245335753423, 6.73123147037805)
yy <- c(4.88402435072353, 4.20867046100364)
zz <- c(68.727, 48.558)
mypoints <- trans3d(xx,yy,zz,pmat = pmat)
points(mypoints,pch = 16,col = 2)
The image below is correct, but when the plot is rotated (set flip to 2) the points do not jive. In other words, when the plot is rotated the points should be hidden from view, or seen through a semi-transparent surface. Help is appreciated!
In case this is helpful to anyone. I ended up using the persp3D() function from the plot3D package. All my custom axes labels and tick marks transferred seamlessly from the base persp() with the added bonus of a transparency argument (alpha =) and proper point plotting (points3D).

Nonlinear model convergence

I have a time series data set and each time series has datapoint of 30-year from different/same species. I am developing a forecasting model using the first 23 years of data from each time series data point and I am using the rest 7 years as test set to know the predictive ability of model but the nonlinear model (Model 6 and Model 7) don't give succinct result?
Data:
DD <- structure(list(Plot = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L,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, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("BD", "BG"), class = "factor"), Year = c(37L,
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L,
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L,
64L, 65L, 66L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L,
56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L,
61L, 62L, 63L, 64L, 65L, 66L, 37L, 38L, 39L, 40L, 41L, 42L, 43L,
44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L,
57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 37L, 38L, 39L,
40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L,
53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L,
66L), Count = c(81L, 45L, 96L, 44L, 24L, 8L, 28L, 32L, 39L, 29L,
40L, 17L, 4L, 12L, 18L, 11L, 63L, 98L, 78L, 76L, 67L, 36L, 56L,
43L, 81L, 8L, 14L, 20L, 25L, 19L, 135L, 91L, 171L, 88L, 59L,
1L, 1L, 1L, 2L, 1L, 11L, 9L, 34L, 15L, 32L, 21L, 33L, 43L, 39L,
20L, 6L, 3L, 9L, 9L, 28L, 16L, 15L, 2L, 1L, 1L, 34L, 16L, 19L,
35L, 32L, 7L, 2L, 30L, 29L, 25L, 28L, 11L, 31L, 31L, 28L, 27L,
34L, 110L, 87L, 103L, 72L, 19L, 46L, 43L, 107L, 32L, 26L, 31L,
12L, 29L, 23L, 40L, 50L, 23L, 34L, 11L, 9L, 4L, 24L, 55L, 14L,
16L, 51L, 43L, 2L, 13L, 8L, 96L, 52L, 118L, 32L, 1L, 8L, 17L,
34L, 29L, 38L, 15L, 4L, 38L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
4L, 6L, 4L, 4L, 10L, 6L, 7L, 9L, 15L, 30L, 25L, 36L, 13L, 17L,
43L, 36L, 60L, 50L, 26L, 13L, 13L, 27L, 18L, 56L, 96L, 16L, 54L,
2L, 2L, 9L, 5L, 5L, 6L, 2L, 6L, 2L, 3L, 4L, 3L, 136L, 71L, 116L,
28L, 23L, 76L, 64L, 98L, 58L, 26L, 13L, 13L, 13L, 18L, 19L, 24L,
18L, 17L, 3L, 23L, 19L, 9L, 11L, 13L, 20L, 29L, 29L, 17L, 20L,
26L, 71L, 63L, 53L, 54L, 20L, 22L, 18L, 93L, 50L, 18L, 12L, 12L,
31L), LogCount = c(1.908385019, 1.653212514, 1.982271233, 1.643462676,
1.380211242, 0.903089987, 1.447158031, 1.505109978, 1.591064607,
1.462397998, 1.602059991, 1.230448921, 0.602059991, 1.079181206,
1.255272505, 1.041392685, 1.799340549, 1.991226076, 1.892094603,
1.880813592, 1.826074803, 1.556302501, 1.748188027, 1.633468456,
1.908485019, 0.903089987, 1.146128035, 1.301029996, 1.397940009,
1.278753601, 2.130333768, 1.95904139, 2.2329961, 1.94448267,
1.770852012, 0, 0, 0, 0.30102999, 0, 1.0411392685, 0.954242509,
1.531478917, 1.176031259, 1.505149978, 1.322219295, 1.51851394,
1.6334684456, 1.591064607, 1.301029996, 0.77815125, 0.477121255,
0.954242509, 0.954242509, 1.447158031, 1.204119983, 1.176091259,
0.301029996, 0, 0, 1.531478917, 1.204119983, 1.278753501, 1.544068044,
1.505149978, 0.084509804, 0.301029996, 1.477121255, 1.462397998,
1.397940009, 1.447158031, 1.041392685, 1.491361694, 1.491361694,
1.447158031, 1.431363754, 1.531478917, 2.041392685, 1.939519253,
2.012837225, 1.857332495, 1.278753601, 1.662757382, 1.633468456,
2.029383778, 1.505149978, 1.414973348, 1.491361594, 1.079181245,
1.462397998, 1.361727835, 1.602059991, 1.698970004, 1.361727836,
1.531478917, 1.041392685, 0.954242509, 0.602059991, 1.380211242,
1.740362689, 1.146128036, 1.204119983, 1.707570176, 1.633468456,
0.301029996, 1.113943352, 0.903089987, 1.982271233, 1.716003344,
2.071882007, 1.50514997, 0, 0.903089987, 1.230448921, 1.53147891,
1.2397998, 1.57978359, 1.176091259, 0.602059991, 1.57978359,
0.301029996, 0, 0, 0, 0, 0, 0.477121255, 0.477121255, 0.602059991,
0.77815125, 0.602059991, 0.602059991, 1, 0.77815125, 0.84509804,
0.95424509, 1.176091259, 1.477121255, 1.39790009, 1.555302501,
1.113943352, 1.230448921, 1.633468456, 1.555302501, 1.77815125,
1.698970004, 1.414973348, 1.113943352, 1.113943352, 1.431353754,
1.255272505, 1.748188027, 1.982271233, 1.204119983, 1.73239376,
1.431363754, 1.361727835, 0.954242509, 0.698970004, 0.698970004,
0.77815125, 0.301029996, 0.77815125, 0.301029996, 0.477121255,
0.602059991, 0.477121255, 2.133538908, 1.851258349, 2.064457989,
1.447158031, 1.361727836, 1.880813592, 1.806179974, 1.991226076,
1.763427994, 1.414973348, 1.113943352, 1.113943352, 1.113943352,
1.255272505, 1.278753601, 1.380211242, 1.255272505, 1.230446921,
0.477121255, 1.361727835, 1.278753601, 0.954242509, 1.0411392685,
1.113943352, 1.301029996, 1.462397998, 1.462397998, 1.230448921,
1.301029995, 1.414973348, 1.851258349, 1.799340549, 1.72427587,
1.73239376, 1.301029996, 1.342422681, 1.255272505, 1.968482949,
1.698970004, 1.255272505, 1.079181246, 1.079181246, 1.491361694
), Diff = c(-0.255272505, 0.329058719, -0.338818557, -0.263241434,
-0.077121255, 0.544068044, 0.057991947, 0.085910629, -0.128666609,
0.139661993, -0.37161107, -0.62838893, 0.477121255, 0.176091259,
-0.21387982, 0.757947864, 0.191885527, -0.099131473, -0.011281011,
-0.054738789, -0.269772302, 0.191885526, -0.114719571, 0.275016563,
-1.005395032, 0.243038049, 0.15490196, 0.096910013, -0.119186408,
NA, -0.171292376, 0.273954718, -0.288513438, -0.17363066, -1.770852012,
0, 0, 0.301029996, -0.301029996, 1.041392685, -0.087150176, 0.577235408,
-0.355387658, 0.329058719, -0.182930683, 0.196294545, 0.110954516,
-0.042403849, -0.290034611, -0.522878746, -0.301029995, 0.477121254,
0, 0.492915522, -0.243038048, -0.028028724, -0.875061263, -0.301029996,
0, 1.531078917, -0.32735893, 0.070633618, 0.265310043, -0.038918066,
-0.660051938, -0.544068044, 1.176091259, -0.014723257, -0.064457989,
0.049218022, -0.405765346, 0.449969009, 0, -0.044203663, -0.015794267,
0.100115153, 0.509913768, -0.101873432, 0.073317972, -0.155504729,
-0.578578895, 0.384054231, -0.029289376, 0.395915322, -0.5202338,
-0.09017663, 0.076388346, -0.412180448, 0.383216752, -0.100670162,
0.240332155, 0.096910013, -0.337242168, 0.169751081, -0.490086232,
-0.087150176, -0.352182518, 0.778151251, 0.360151447, -0.594234653,
0.057991947, 0.503450193, -0.07410172, -1.33243846, 0.812913356,
-0.210853365, 1.079181246, -0.266267889, 0.355878663, -0.566732029,
-1.505149978, 0.903089987, 0.327358934, 0.301029996, -0.069080919,
0.117385599, -0.403692338, -0.574031268, 0.977723606, -1.278753601,
-0.301029996, 0, 0, 0, 0, 0.477121255, 0, 0.124938736, 0.176091259,
-0.176091259, 0, 0.397490009, -0.2218485, 0.06690679, 0.10914469,
0.22184875, 0.301029996, -0.079181206, 0.158362092, -0.442359149,
0.116505569, 0.403019535, -0.077165955, 0.221848749, -0.079181206,
-0.283996656, -0.301029996, 0, 0.317420412, -0.176091259, 0.492915522,
0.23483206, -0.77815125, 0.528273777, -0.301029996, -0.069635928,
-0.407485327, -0.255272505, 0, 0.079181246, -0.477121254, 0.477121254,
-0.477121254, 0.176091259, 0.124938736, -0.124938736, 1.656417653,
-0.282280559, 0.21319964, -0.617299958, -0.085430195, 0.5191085756,
-0.074533518, 0.185045102, -0.227798082, -0.348454546, -0.301029996,
0, 0, 0.141329153, 0.023481096, 0.101457641, -0.124938737, -0.024823584,
-0.753327666, 0.884606581, -0.082974235, -0.324511092, 0.087150176,
0.072550667, 0.187086644, 0.161368002, 0, -0.231949077, 0.070581075,
0.113903352, 0.436285001, -0.00519178, -0.075054679, 0.00811789,
-0.431363764, 0.041392685, -0.087150176, 0.713210444, -0.269512945,
-0.443697499, -0.176091259, 0, 0.412180448, -0.148939013)), class = "data.frame", row.names = c(NA,
-210L))
Code:
for(f in 1:11){
for(b in 1:5){
for (c in 1:5){
#To select test sets 1,2,3,4, and 5 years beyond the training set:
#Calculate the mean of abundance for the training set years.
Model1<-lm(mean~1, data=DD1)
#
Output2:
2 3 0.676209994477288 1.9365051784348e-09 4.44089209850063e-16
3 53 11.9236453578109 2.06371097988267e-09 1.13686837721616e-13
4 31 1.94583877614293 1.11022302462516e-15 1.99840144432528e-15
5 4 8.06660449042397 1.48071350736245e-08 3.19744231092045e-14
6 5 10.5321102149558 9.31706267692789e-10 1.4210854715202e-14
..
First, please see the time series graph of counts for different species and plots below.
library(ggplot2)
ggplot(DD, aes(Year, Count)) +
geom_point() +
geom_line() +
facet_grid(Plot ~ Species) +
scale_y_log10()
It is seen that there is no obvious trend which can be fitted by power or log-power function using nls.
Second, as I understand you are trying to use nls to predict outside the training data set. Usually it is not quite an effective to use least square models because of auto-correlated nature of time-series.
Third, the simplest prediction algorithm is Holt-Winters (see "dirty" implementation below). You can use as well a ton of other algorithms like ARIMA, exponential smoothing state space etc.
x <- ts(DD[DD$Species == "BG" & DD$Plot == "elq1a3", ]$LogCount)
m <- HoltWinters(x, gamma = FALSE)
library(forecast)
f <- forecast(m, 2)
plot(f, main = "elq1a3 at BG")
Fourth, about your algorithm in question, it throws
Error in qr.solve(QR.B, cc) : singular matrix 'a' in solve.
The reason is that in the first step of for-loop (at f = b = c = 1 DD2 data frame contains just one row. And executing
Model6<-nls(Diff~1+Count^T,start=list(T=1),trace=TRUE,algorithm ="plinear",data=DD2)
means that you are trying to fit a curve using only one data point, which is impossible.
However if you change f value in for-loop from 1:11 to 2:11 another error thrown:
Error in nls(Diff ~ 1 + Count^T, start = list(T = 1), trace = TRUE,
algorithm = "plinear", : step factor 0.000488281 reduced below
minFactor 0.000976562.
In this case you cannot use "naive" approach used by plinear algorithm with self-starting inital value and, e.g. nls.control(min.factor = 1e-5). You must feed all initial coefficients explicitely with default Gauss-Newton algorithm. Quite exausting, please try yourself :)

removing rows from dataframe in two different columns R

I have a dataframe of results. There are multiple comparisons for Cruise_Strata. I have two columns of cruise_strata (Cruise1_Strata1 and Cruise2_Strata2). The problem I found is that there are "duplicate" records in the dataframe. For example one row will have
Cruise_Strata1 Cruise_Strata2
201501.35 201502.35
and another row will have
Cruise_Strata1 Cruise_Strata2
201502.35 201501.35
The rows have the same results for the remaining columns. I would like to be able to identify rows where this happens and remove one row from the dataset, but do not know how to go about it. I cant use duplicate because they are not duplicates.
Any help would be appreciated.
Here is the dataframe.
dput(result5)
structure(list(Cruise_Strata1 = structure(c(1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L,
11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L,
17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L,
24L, 24L, 25L, 25L, 26L, 26L, 27L, 27L, 28L, 28L, 29L, 29L, 30L,
30L, 31L, 31L, 32L, 32L, 33L, 33L, 34L, 34L, 35L, 35L, 36L, 36L,
37L, 37L, 38L, 38L, 39L, 39L, 40L, 40L, 41L, 41L, 42L, 42L, 43L,
43L, 44L, 44L, 45L, 45L, 46L, 46L, 47L, 47L, 48L, 48L, 49L, 49L,
50L, 50L, 51L, 51L, 52L, 52L, 53L, 53L, 54L, 54L, 55L, 55L, 56L,
56L, 57L, 57L, 58L, 58L, 59L, 59L, 60L, 60L, 61L, 61L, 62L, 62L,
63L, 63L, 64L, 64L, 65L, 65L, 66L, 66L), .Label = c("201501.10",
"201501.11", "201501.13", "201501.14", "201501.15", "201501.17",
"201501.18", "201501.19", "201501.21", "201501.22", "201501.23",
"201501.24", "201501.25", "201501.26", "201501.27", "201501.29",
"201501.30", "201501.31", "201501.33", "201501.34", "201501.35",
"201501.9", "201502.10", "201502.11", "201502.13", "201502.14",
"201502.15", "201502.17", "201502.18", "201502.19", "201502.21",
"201502.22", "201502.23", "201502.24", "201502.25", "201502.26",
"201502.27", "201502.29", "201502.30", "201502.31", "201502.33",
"201502.34", "201502.35", "201502.9", "201503.10", "201503.11",
"201503.13", "201503.14", "201503.15", "201503.17", "201503.18",
"201503.19", "201503.21", "201503.22", "201503.23", "201503.24",
"201503.25", "201503.26", "201503.27", "201503.29", "201503.30",
"201503.31", "201503.33", "201503.34", "201503.35", "201503.9"
), class = "factor"), Cruise_Strata2 = structure(c(23L, 45L,
24L, 46L, 25L, 47L, 26L, 48L, 27L, 49L, 28L, 50L, 29L, 51L, 30L,
52L, 31L, 53L, 32L, 54L, 33L, 55L, 34L, 56L, 35L, 57L, 36L, 58L,
37L, 59L, 38L, 60L, 39L, 61L, 40L, 62L, 41L, 63L, 42L, 64L, 43L,
65L, 44L, 66L, 1L, 45L, 2L, 46L, 3L, 47L, 4L, 48L, 5L, 49L, 6L,
50L, 7L, 51L, 8L, 52L, 9L, 53L, 10L, 54L, 11L, 55L, 12L, 56L,
13L, 57L, 14L, 58L, 15L, 59L, 16L, 60L, 17L, 61L, 18L, 62L, 19L,
63L, 20L, 64L, 21L, 65L, 22L, 66L, 1L, 23L, 2L, 24L, 3L, 25L,
4L, 26L, 5L, 27L, 6L, 28L, 7L, 29L, 8L, 30L, 9L, 31L, 10L, 32L,
11L, 33L, 12L, 34L, 13L, 35L, 14L, 36L, 15L, 37L, 16L, 38L, 17L,
39L, 18L, 40L, 19L, 41L, 20L, 42L, 21L, 43L, 22L, 44L), .Label = c("201501.10",
"201501.11", "201501.13", "201501.14", "201501.15", "201501.17",
"201501.18", "201501.19", "201501.21", "201501.22", "201501.23",
"201501.24", "201501.25", "201501.26", "201501.27", "201501.29",
"201501.30", "201501.31", "201501.33", "201501.34", "201501.35",
"201501.9", "201502.10", "201502.11", "201502.13", "201502.14",
"201502.15", "201502.17", "201502.18", "201502.19", "201502.21",
"201502.22", "201502.23", "201502.24", "201502.25", "201502.26",
"201502.27", "201502.29", "201502.30", "201502.31", "201502.33",
"201502.34", "201502.35", "201502.9", "201503.10", "201503.11",
"201503.13", "201503.14", "201503.15", "201503.17", "201503.18",
"201503.19", "201503.21", "201503.22", "201503.23", "201503.24",
"201503.25", "201503.26", "201503.27", "201503.29", "201503.30",
"201503.31", "201503.33", "201503.34", "201503.35", "201503.9"
), class = "factor"), P_value = c(0.63, 0.6793, 0.0319, 0.0289,
0.9516, 0.8128, 0.9967, 0.3071, 0.9641, 0.0246, 0.7967, 0.2551,
0.2329, 0.3725, 0.0269, 0.3796, 0.0245, 0.5562, 0.9952, 0.5176,
0.5596, 0.9966, 0.32, 0.6402, 0.7691, 0.9671, 0.9396, 0.9, 0.9024,
0.3624, 0.0433, 0.3402, 0.5302, 0.787, 0.0295, 0.3638, 0.006,
0.701, 0.6323, 0.0366, 2e-04, 0.0011, 0.8849, 0.3, 0.63, 0.9738,
0.0319, 0.5197, 0.9516, 0.7369, 0.9967, 0.2276, 0.9641, 0.0158,
0.7967, 0.6332, 0.2329, 0.0322, 0.0269, 0.3013, 0.0245, 0.0129,
0.9952, 0.795, 0.5596, 0.7277, 0.32, 0.747, 0.7691, 0.3817, 0.9396,
0.7961, 0.9024, 0.4164, 0.0433, 0.0028, 0.5302, 0.2864, 0.0295,
0.7036, 0.006, 0, 0.6323, 0.002, 2e-04, 0.9548, 0.8849, 0.0546,
0.6793, 0.9738, 0.0289, 0.5197, 0.8128, 0.7369, 0.3071, 0.2276,
0.0246, 0.0158, 0.2551, 0.6332, 0.3725, 0.0322, 0.3796, 0.3013,
0.5562, 0.0129, 0.5176, 0.795, 0.9966, 0.7277, 0.6402, 0.747,
0.9671, 0.3817, 0.9, 0.7961, 0.3624, 0.4164, 0.3402, 0.0028,
0.787, 0.2864, 0.3638, 0.7036, 0.701, 0, 0.0366, 0.002, 0.0011,
0.9548, 0.3, 0.0546), Cruise1 = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("201501",
"201502", "201503"), class = "factor"), Cruise1_Strata1 = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L,
9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L,
16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L,
22L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L,
8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L,
14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L,
21L, 21L, 22L, 22L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L,
6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L,
13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L,
20L, 20L, 21L, 21L, 22L, 22L), .Label = c("10", "11", "13", "14",
"15", "17", "18", "19", "21", "22", "23", "24", "25", "26", "27",
"29", "30", "31", "33", "34", "35", "9"), class = "factor"),
Cruise2 = structure(c(2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L,
2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L,
3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L), .Label = c("201501", "201502", "201503"), class = "factor"),
Cruise2_Strata2 = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L,
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L,
11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L,
17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L,
9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L,
15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L,
21L, 21L, 22L, 22L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L,
6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L,
12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L,
18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L), .Label = c("10",
"11", "13", "14", "15", "17", "18", "19", "21", "22", "23",
"24", "25", "26", "27", "29", "30", "31", "33", "34", "35",
"9"), class = "factor"), adjuste_p = c(1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.792, 1, 1, 1, 0.0264,
0.1452, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.3696, 1,
1, 1, 1, 0.792, 0, 1, 0.264, 0.0264, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 0.3696, 1, 1, 1, 1, 1, 0, 1, 0.264,
0.1452, 1, 1, 1)), .Names = c("Cruise_Strata1", "Cruise_Strata2",
"P_value", "Cruise1", "Cruise1_Strata1", "Cruise2", "Cruise2_Strata2",
"adjuste_p"), row.names = c(1453L, 2905L, 1520L, 2972L, 1587L,
3039L, 1654L, 3106L, 1721L, 3173L, 1788L, 3240L, 1855L, 3307L,
1922L, 3374L, 1989L, 3441L, 2056L, 3508L, 2123L, 3575L, 2190L,
3642L, 2257L, 3709L, 2324L, 3776L, 2391L, 3843L, 2458L, 3910L,
2525L, 3977L, 2592L, 4044L, 2659L, 4111L, 2726L, 4178L, 2793L,
4245L, 2860L, 4312L, 23L, 2927L, 90L, 2994L, 157L, 3061L, 224L,
3128L, 291L, 3195L, 358L, 3262L, 425L, 3329L, 492L, 3396L, 559L,
3463L, 626L, 3530L, 693L, 3597L, 760L, 3664L, 827L, 3731L, 894L,
3798L, 961L, 3865L, 1028L, 3932L, 1095L, 3999L, 1162L, 4066L,
1229L, 4133L, 1296L, 4200L, 1363L, 4267L, 1430L, 4334L, 45L,
1497L, 112L, 1564L, 179L, 1631L, 246L, 1698L, 313L, 1765L, 380L,
1832L, 447L, 1899L, 514L, 1966L, 581L, 2033L, 648L, 2100L, 715L,
2167L, 782L, 2234L, 849L, 2301L, 916L, 2368L, 983L, 2435L, 1050L,
2502L, 1117L, 2569L, 1184L, 2636L, 1251L, 2703L, 1318L, 2770L,
1385L, 2837L, 1452L, 2904L), class = "data.frame")
R Info
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Does this give you your desired result?
duplicated(apply(cbind(result5$Cruise_Strata1, df$Cruise_Strata2), 1,
function(x) paste(min(x), max(x))))
You can use the resulting logical vector to subset your data.
First you create a vector pasting the values in Cruise_Strata1 and Cruise_Strata2. Doing this you move the smaller of the two to the front and the larger one to the end (or you could do it vice versa). This is just a trick so that you can apply the duplicated function and recognize the duplicates.
Note: this approach will remove duplicates of the form:
Cruise_Strata1 Cruise_Strata2
x y
y x
As well as (if this is not desired let me know):
Cruise_Strata1 Cruise_Strata2
x y
x y
For a generic data frame df with duplicated values in Cruise_Strata1 and Cruise_Strata2:
df$dupe <- 0
for(i in 1:(length(df$Cruise_Strata1)-1))
{
for(j in (i+1):length(df$Cruise_Strata1))
if(df$Cruise_Strata1[i]==df$Cruise_Strata2[j])
{print(df[c(i,j),]); df$dupe[i] = 1;break}
}
df[df$dupe != 1,]

Resources