问题
After reading this answer on error in contrasts and reviewing my data I am still stuck with a problem when trying to combine packages 'plm' and 'effects'. This might be impossible as John Fox does not discuss this possibility in his effects document (link is not allowed apparently - Google: "john fox effects package" if you want to have a look). So if it is indeed impossible please tell me.
I am running a simple regression on a reduced data set
library(plm) ; library(effects)
shortdata<-plm.data(shortdata,index=c("ID","Year"))
MESS<-plm(paci_to_t ~ paco_to_t + cddom + cddom2,data=shortdata,model="within")
Effect(c('cddom2'),MESS)
and I get the following error (and the typical plm warning message)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
I know it's still a pretty long data structure but I reckon it's worth sharing
> dput(shortdata)
structure(list(ID = structure(c(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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("1", "2", "4", "5",
"6", "7"), class = "factor"), Year = structure(c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L), .Label = c("1991",
"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006"), class = "factor"),
paco_to_t = c(47L, 45L, 50L, 61L, 113L, 76L, 60L, 70L, 75L,
70L, 52L, 39L, 29L, 34L, 12L, 12L, 1L, 4L, 6L, 34L, 42L,
25L, 38L, 34L, 30L, 27L, 32L, 27L, 7L, 5L, 0L, 0L, 14L, 33L,
38L, 36L, 33L, 24L, 22L, 25L, 28L, 65L, 59L, 44L, 49L, 43L,
27L, 15L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 3L, 13L, 22L,
11L, 12L, 2L, 1L, 3L, 7L, 11L, 31L, 53L, 24L, 28L, 29L, 22L,
38L, 41L, 47L, 40L, 30L, 21L, 8L, 9L, 14L, 59L, 79L, 129L,
78L, 91L, 112L, 86L, 104L, 101L, 140L, 83L, 35L, 17L, 13L
), paci_to_t = c(535L, 641L, 582L, 634L, 871L, 392L, 421L,
285L, 223L, 183L, 52L, 24L, 1L, 5L, 1L, 0L, 2L, 22L, 25L,
124L, 216L, 70L, 115L, 78L, 46L, 32L, 14L, 10L, 2L, 1L, 0L,
0L, 683L, 626L, 1520L, 385L, 514L, 226L, 166L, 85L, 169L,
391L, 160L, 85L, 43L, 23L, 6L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 7L, 13L, 5L, 0L, 0L, 0L, 21L, 120L, 186L,
187L, 519L, 113L, 200L, 110L, 41L, 43L, 20L, 28L, 12L, 2L,
0L, 0L, 143L, 73L, 368L, 708L, 920L, 533L, 583L, 412L, 256L,
212L, 147L, 177L, 84L, 8L, 0L, 0L), cddom = structure(c(4.22657266700715,
7.07828323739468, 5.58155937520987, 6.09945741088926, 4.98990473760187,
13.1975509132969, 5.60581022431812, 6.4115834088139, 3.19372115108478,
3.47380227675641, 4.13404916693487, 2.97544267158703, 2.54471629085273,
2.64814971815201, 1.28583544617808, 0.437627780850619, 0.136363636363636,
0.164922480620155, 0.981640399790555, 1.61119564479727, 4.27951983102512,
4.52440902710094, 2.32837056711187, 6.18186426561852, 4.0123555170895,
4.22609174210518, 3.95801989660183, 4.61957981110804, 3.77282177376164,
1.66791871414077, 0.546721420916684, 0.265624433322453, 2.41282572727806,
2.77748331046807, 7.00223921984389, 3.55837337174436, 5.31590575343992,
2.61930006177923, 2.31060371552517, 2.57360416944421, 1.84395565608465,
2.92626684365294, 3.61419385672379, 1.80034349383174, 1.50187312925546,
1.11122165051967, 0.997854809226777, 0.386006521136941, 0,
0, 0, 0, 0, 0, 0, 0, 0.0476190476190476, 0.056547619047619,
0.0696251163521699, 0.385345494115747, 0.451639377056987,
0.33059260900224, 0.347544282905224, 0.0138314176245211,
0.211351052048726, 0.525647451963241, 0.696630753538187,
0.666082288178836, 1.09492110512526, 3.8035303566375, 1.00915595031924,
0.816810004384515, 0.530090984042535, 0.554438577055268,
1.08116003643161, 0.467985507058757, 0.512281275398252, 0.833471981363938,
0.506558172321601, 0.498777213205938, 0.548336215316966,
0.85237681730237, 0.982747572848003, 6.59758768791534, 8.74094242997363,
9.29913186611362, 6.85204239723805, 7.22544455545639, 7.55800542543194,
4.85857232261679, 3.3323740421857, 3.49663263434836, 4.73578798222752,
3.12245695547519, 1.39134774182295, 0.676122574837844), .Dim = 96L, .Dimnames = list(
c("1990_ABT", "1991_ABT", "1992_ABT", "1993_ABT", "1994_ABT",
"1995_ABT", "1996_ABT", "1997_ABT", "1998_ABT", "1999_ABT",
"2000_ABT", "2001_ABT", "2002_ABT", "2003_ABT", "2004_ABT",
"2005_ABT", "1990_AKN", "1991_AKN", "1992_AKN", "1993_AKN",
"1994_AKN", "1995_AKN", "1996_AKN", "1997_AKN", "1998_AKN",
"1999_AKN", "2000_AKN", "2001_AKN", "2002_AKN", "2003_AKN",
"2004_AKN", "2005_AKN", "1990_ALL", "1991_ALL", "1992_ALL",
"1993_ALL", "1994_ALL", "1995_ALL", "1996_ALL", "1997_ALL",
"1998_ALL", "1999_ALL", "2000_ALL", "2001_ALL", "2002_ALL",
"2003_ALL", "2004_ALL", "2005_ALL", "1990_ABT", "1990_ABT",
"1990_ABT", "1990_ABT", "1990_ABT", "1990_ABT", "1990_ABT",
"1990_ABT", "1998_ALT", "1999_ALT", "2000_ALT", "2001_ALT",
"2002_ALT", "2003_ALT", "2004_ALT", "2005_ALT", "1990_AMG",
"1991_AMG", "1992_AMG", "1993_AMG", "1994_AMG", "1995_AMG",
"1996_AMG", "1997_AMG", "1998_AMG", "1999_AMG", "2000_AMG",
"2001_AMG", "2002_AMG", "2003_AMG", "2004_AMG", "2005_AMG",
"1990_AZN", "1991_AZN", "1992_AZN", "1993_AZN", "1994_AZN",
"1995_AZN", "1996_AZN", "1997_AZN", "1998_AZN", "1999_AZN",
"2000_AZN", "2001_AZN", "2002_AZN", "2003_AZN", "2004_AZN",
"2005_AZN"))), cddom2 = structure(c(17.8639165094919,
50.1020935887824, 31.1538050589932, 37.2033807072519, 24.8991492903416,
174.175350109063, 31.4251082710696, 41.1084018081777, 10.1998547908863,
12.067302257998, 17.0903625146349, 8.85325909190098, 6.47558100093125,
7.01269692974855, 1.65337279464797, 0.191518074572238, 0.018595041322314,
0.0271994246139054, 0.96361787450096, 2.5959514058137, 18.3142899841373,
20.4702770445124, 5.42130949779285, 38.2154457985312, 16.0989967955186,
17.8598514126896, 15.665921501896, 21.340517631197, 14.2341841365699,
2.78195283698102, 0.298904312089158, 0.0705563395778743,
5.8217279902149, 7.71441353992869, 49.03135409192, 12.6620210527394,
28.2588539794557, 6.86073281363667, 5.33888953019871, 6.6234384209806,
3.40017246160656, 8.56303764026254, 13.06239723398, 3.24123669578228,
2.2556228963796, 1.23481355658365, 0.995714220297008, 0.149001034360244,
0, 0, 0, 0, 0, 0, 0, 0, 0.00226757369614512, 0.00319763321995465,
0.0048476568270532, 0.148491149835309, 0.203978126908423,
0.109291473126908, 0.120787028580106, 0.000191308113503912,
0.0446692672021035, 0.276305243755448, 0.485294406775182,
0.443665614625554, 1.19885222644872, 14.466843173863, 1.01839573206472,
0.667178583262631, 0.280996451363183, 0.30740213572707, 1.16890702437679,
0.219010434817042, 0.26243210512366, 0.694675543718729, 0.2566011819458,
0.248778708413482, 0.300672605028135, 0.726546238674519,
0.965792791938642, 43.528163299732, 76.4040745641133, 86.4738534633698,
46.9504850135478, 52.2070490239743, 57.1234460108586, 23.6057250140979,
11.104716757033, 12.2264397795899, 22.4276878126106, 9.74973743879538,
1.93584853867582, 0.457141736205356), .Dim = 96L, .Dimnames = list(
c("1990_ABT", "1991_ABT", "1992_ABT", "1993_ABT", "1994_ABT",
"1995_ABT", "1996_ABT", "1997_ABT", "1998_ABT", "1999_ABT",
"2000_ABT", "2001_ABT", "2002_ABT", "2003_ABT", "2004_ABT",
"2005_ABT", "1990_AKN", "1991_AKN", "1992_AKN", "1993_AKN",
"1994_AKN", "1995_AKN", "1996_AKN", "1997_AKN", "1998_AKN",
"1999_AKN", "2000_AKN", "2001_AKN", "2002_AKN", "2003_AKN",
"2004_AKN", "2005_AKN", "1990_ALL", "1991_ALL", "1992_ALL",
"1993_ALL", "1994_ALL", "1995_ALL", "1996_ALL", "1997_ALL",
"1998_ALL", "1999_ALL", "2000_ALL", "2001_ALL", "2002_ALL",
"2003_ALL", "2004_ALL", "2005_ALL", "1990_ABT", "1990_ABT",
"1990_ABT", "1990_ABT", "1990_ABT", "1990_ABT", "1990_ABT",
"1990_ABT", "1998_ALT", "1999_ALT", "2000_ALT", "2001_ALT",
"2002_ALT", "2003_ALT", "2004_ALT", "2005_ALT", "1990_AMG",
"1991_AMG", "1992_AMG", "1993_AMG", "1994_AMG", "1995_AMG",
"1996_AMG", "1997_AMG", "1998_AMG", "1999_AMG", "2000_AMG",
"2001_AMG", "2002_AMG", "2003_AMG", "2004_AMG", "2005_AMG",
"1990_AZN", "1991_AZN", "1992_AZN", "1993_AZN", "1994_AZN",
"1995_AZN", "1996_AZN", "1997_AZN", "1998_AZN", "1999_AZN",
"2000_AZN", "2001_AZN", "2002_AZN", "2003_AZN", "2004_AZN",
"2005_AZN")), class = "AsIs")), .Names = c("ID", "Year",
"paco_to_t", "paci_to_t", "cddom", "cddom2"), class = c("plm.dim",
"data.frame"), row.names = c(1L, 2L, 3L, 4L, 160L, 5L, 162L,
163L, 164L, 6L, 166L, 7L, 8L, 9L, 170L, 171L, 172L, 173L, 174L,
175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 10L,
186L, 11L, 188L, 12L, 190L, 191L, 192L, 13L, 194L, 14L, 196L,
197L, 15L, 199L, 200L, 16L, 202L, 17L, 157L, 400L, 401L, 583L,
524L, 565L, 526L, 527L, 204L, 205L, 206L, 207L, 208L, 209L, 210L,
211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 18L, 19L, 221L,
222L, 223L, 224L, 20L, 226L, 21L, 22L, 23L, 230L, 231L, 232L,
233L, 234L, 235L, 236L, 237L, 238L, 239L, 24L, 25L, 26L, 27L))
Now to the best of my ability, I do not see a factor with less than 2 levels so I don't understand where this error comes from. My eventual goal for using the effects package is to be able to plot the marginal effect of cddom (and cddom2 which is the squared version of cddom) on my response variable. Of course in my full data set I have about 15 more controls, none of which are factors with a single level, but I keep getting the same problem.
I hope someone can advise!
EDIT This problem with the effect function changes sometimes. e.g.
Effect(c("cddom","cddom2"),MESS)
gives the following error
Error in mod.matrix %*% mod$coefficients[!is.na(coef(mod))] : non-conformable arguments
Thanks in advance
Simon
来源:https://stackoverflow.com/questions/23131824/r-packages-effects-plm-error-in-contrasts-when-trying-to-plot-marginal-eff