[R] Error; contrasts can be applied only to factors with 2 or more levels
grace
sololorox at hotmail.com
Tue Aug 14 11:54:15 CEST 2012
Hi,
I have been running the same code without problem for the last few days,
changing data sets etc with no issue.
Today I changed the covariates for the model and am now getting this error
message:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Everything in the code looks the same to me, but I'm a rookie and could be
doing something stupid.
Here's how the working code looked:
> R <- 121
> J <- 2
> y<- matrix(c(
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 1, 0,
+ 1, 1,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 1, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 1, 1,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 1, 1,
+ 0, 0,
+ 1, 1,
+ 0, 0,
+ 1, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 1, 0,
+ 1, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 1, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0, 0,
+ 0,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2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
> 4, 4, 4, 4, 4, 4, 4)))
>
>
> elev<-list(
+ x3=matrix(c(
+ 25,
+ 23,
+ 18,
+ 13,
+ 11,
+ 10,
+ 23,
+ 32,
+ 38,
+ 42,
+ 44,
+ 44,
+ 41,
+ 40,
+ 35,
+ 38,
+ 56,
+ 45,
+ 35,
+ 27,
+ 30,
+ 25,
+ 21,
+ 19,
+ 24,
+ 23,
+ 24,
+ 26,
+ 23,
+ 22,
+ 18,
+ 24,
+ 29,
+ 26,
+ 27,
+ 26,
+ 29,
+ 33,
+ 37,
+ 32,
+ 36,
+ 49,
+ 54,
+ 63,
+ 74,
+ 76,
+ 84,
+ 84,
+ 68,
+ 52,
+ 46,
+ 46,
+ 48,
+ 58,
+ 77,
+ 82,
+ 78,
+ 77,
+ 78,
+ 77,
+ 74,
+ 69,
+ 74,
+ 82,
+ 90,
+ 92,
+ 92,
+ 95,
+ 100,
+ 96,
+ 93,
+ 90,
+ 80,
+ 70,
+ 67,
+ 71,
+ 69,
+ 74,
+ 74,
+ 76,
+ 91,
+ 97,
+ 90,
+ 82,
+ 86,
+ 90,
+ 94,
+ 84,
+ 68,
+ 62,
+ 44,
+ 64,
+ 73,
+ 92,
+ 106,
+ 119,
+ 125,
+ 126,
+ 132,
+ 138,
+ 157,
+ 160,
+ 171,
+ 174,
+ 164,
+ 159,
+ 149,
+ 132,
+ 109,
+ 80,
+ 62,
+ 49,
+ 42,
+ 36,
+ 30,
+ 26,
+ 44,
+ 51,
+ 67,
+ 70,
+ 88), nrow=R, ncol=J, byrow=TRUE))
>
>
> umf<-unmarkedFrameOccu(y=y, siteCovs=ndvi, obsCovs=elev)
> summary(umf)
unmarkedFrame Object
121 sites
Maximum number of observations per site: 2
Mean number of observations per site: 2
Sites with at least one detection: 13
Tabulation of y observations:
0 1 <NA>
223 19 0
Site-level covariates:
x1 x2
2: 8 2: 8
3:92 3:92
4:21 4:21
Observation-level covariates:
x3
Min. : 10.00
1st Qu.: 35.00
Median : 67.00
Mean : 65.92
3rd Qu.: 86.00
Max. :174.00
>
> (occu1 <- occu(~1 ~ 1, umf))
Call:
occu(formula = ~1 ~ 1, data = umf)
Occupancy:
Estimate SE z P(>|z|)
-1.95 0.325 -6.01 1.82e-09
Detection:
Estimate SE z P(>|z|)
0.539 0.556 0.969 0.333
AIC: 114.2012
> (occu2 <- occu(~x3 ~x1, umf))
Call:
occu(formula = ~x3 ~ x1, data = umf)
Occupancy:
Estimate SE z P(>|z|)
(Intercept) -1.729 1.11 -1.5505 0.121
x13 -0.020 1.16 -0.0172 0.986
x14 -0.163 1.35 -0.1207 0.904
Detection:
Estimate SE z P(>|z|)
(Intercept) -1.0434 1.0762 -0.969 0.332
x3 0.0168 0.0106 1.581 0.114
AIC: 117.5561
> (occu3 <- occu(~x3+x1 ~x1, umf))
Call:
occu(formula = ~x3 + x1 ~ x1, data = umf)
Occupancy:
Estimate SE z P(>|z|)
(Intercept) 1.84 10.4 0.176 0.861
x13 -3.66 10.5 -0.350 0.726
x14 -3.70 10.5 -0.353 0.724
Detection:
Estimate SE z P(>|z|)
(Intercept) -4.0248 2.085 -1.93 0.0536
x3 0.0171 0.011 1.55 0.1201
x13 3.1903 1.992 1.60 0.1093
x14 2.8895 2.528 1.14 0.2531
AIC: 120.386
>
> (occuRN1<-occuRN(~1 ~1, umf))
Call:
occuRN(formula = ~1 ~ 1, data = umf)
Abundance:
Estimate SE z P(>|z|)
-2.01 0.31 -6.48 9.03e-11
Detection:
Estimate SE z P(>|z|)
0.447 0.577 0.775 0.438
AIC: 114.2012
> (occuRN2<-occuRN(~x3 ~x1, umf))
Call:
occuRN(formula = ~x3 ~ x1, data = umf)
Abundance:
Estimate SE z P(>|z|)
(Intercept) -1.8442 1.02 -1.8151 0.0695
x13 0.0447 1.06 0.0424 0.9662
x14 -0.0980 1.23 -0.0795 0.9366
Detection:
Estimate SE z P(>|z|)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 56,
+ 57,
+ 57,
+ 54,
+ 55,
+ 63,
+ 66,
+ 69,
+ 72,
+ 74,
+ 85,
+ 87,
+ 89,
+ 91,
+ 93,
+ 93,
+ 97,
+ 99,
+ 101,
+ 101,
+ 100,
+ 95,
+ 98,
+ 107,
+ 111,
+ 125,
+ 126,
+ 150,
+ 149,
+ 148,
+ 133,
+ 132,
+ 149,
+ 165,
+ 169,
+ 161,
+ 158,
+ 144,
+ 137,
+ 130,
+ 131,
+ 122,
+ 115,
+ 116,
+ 123,
+ 124,
+ 117,
+ 111,
+ 111,
+ 109,
+ 103,
+ 104,
+ 109,
+ 119,
+ 115,
+ 109,
+ 105,
+ 103,
+ 91,
+ 90,
+ 88,
+ 85,
+ 93,
+ 99,
+ 95,
+ 93,
+ 94,
+ 96,
+ 96,
+ 95,
+ 94,
+ 92,
+ 86,
+ 83,
+ 80,
+ 77,
+ 74,
+ 72,
+ 72,
+ 73,
+ 70,
+ 67,
+ 64,
+ 61,
+ 54,
+ 50,
+ 48,
+ 45,
+ 44,
+ 37,
+ 31,
+ 49,
+ 67,
+ 66,
+ 77,
+ 73,
+ 69,
+ 78,
+ 94,
+ 104,
+ 107,
+ 102,
+ 105,
+ 105,
+ 101,
+ 107,
+ 91,
+ 77,
+ 85,
+ 90,
+ 92,
+ 95,
+ 97,
+ 102,
+ 104,
+ 107,
+ 103,
+ 99,
+ 106,
+ 96,
+ 91,
+ 90,
+ 89,
+ 88,
+ 84,
+ 85,
+ 84,
+ 82,
+ 80), nrow=R, ncol=J, byrow=TRUE))
> umf<-unmarkedFrameOccu(y=y, siteCovs=ndvi, obsCovs=elev)
> summary(umf)
unmarkedFrame Object
131 sites
Maximum number of observations per site: 1
Mean number of observations per site: 1
Sites with at least one detection: 6
Tabulation of y observations:
0 1 <NA>
125 6 0
Site-level covariates:
x1 x2
3:131 3:131
Observation-level covariates:
x3
Min. : 31.00
1st Qu.: 77.00
Median : 94.00
Mean : 93.95
3rd Qu.:107.00
Max. :169.00
>
> (occu1<-occu(~1 ~ 1, umf))
Call:
occu(formula = ~1 ~ 1, data = umf)
Occupancy:
Estimate SE z P(>|z|)
-1.3 NaN NaN NaN
Detection:
Estimate SE z P(>|z|)
-1.3 NaN NaN NaN
AIC: 52.72215
Warning messages:
1: In sqrt(diag(vcov(obj))) : NaNs produced
2: In sqrt(diag(vcov(obj))) : NaNs produced
> (occu2<-occu(~x3 ~x1, umf))
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
> (occu3<-occu(~x3+x1 ~x1, umf))
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
>
> (occuRN1<-occuRN(~1 ~1, umf))
Call:
occuRN(formula = ~1 ~ 1, data = umf)
Abundance:
Estimate SE z P(>|z|)
-2.48 1063 -0.00233 0.998
Detection:
Estimate SE z P(>|z|)
0.236 2408 9.79e-05 1
AIC: 52.72215
> (occuRN2<-occuRN(~x3 ~x1, umf))
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
> (occuRN3<-occuRN(~x3+x1 ~x1, umf))
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
I have run it with one column in 'y' before with no problem. I also thought
it was x1 or x2, which work fine also. I just can't seem to get the 'elev'
x3 variables to work.
Thanks,
Grace
--
View this message in context: http://r.789695.n4.nabble.com/Error-contrasts-can-be-applied-only-to-factors-with-2-or-more-levels-tp4640257.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list