I used the fmi function from SemTools package just a few weeks ago, and it worked great! Here is the code that I saved and that worked fine:
dat.imp2 <- mice(data = dat1, m = 37, method = "pmm", seed = 444)
out <- fmi(dat.imp2$imputations)
out
I have used it to compare the loss of efficiency in using 4 source variables vs 1 composite, so I re-ran it twice - first with the 4 source variables and then with 1 composite, and it was much better for composite. Also, the output showed fmi for means and variances separately.
Come back to this code a few weeks later, and it doens't work! The error message reads:
Error in dim(robj) <- c(dX, dY) :
dims [product 0] do not match the length of object [1]
So, I modified the code as follows:
imp2 <- mice(dat1, m = 37, method = "pmm", seed = 444)
out <- fmi(imp2$data)
out
This works but only with the composite variable in the dataset, and only gives me fmi for means but not variances. If I substitute this composite variable with the four source variables it gives me the following error:
Warning message:
In lavaan(slotOptions = object#Options, slotParTable = object#ParTable, :
lavaan WARNING: model has NOT converged!
I don't understand how the code that worked a couple weeks ago does not work now? Did anyone come across this problem? I wasn't able to find much online.
Thank you!
Here is the dataset with one composite variable instead (mommh)
> dput(dat2)
structure(list(mompa = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1,
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0), format.spss = "F8.2", display_width = 10L), momabhx = structure(c(1,
0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1,
0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1), format.spss = "F8.2", display_width = 10L),
mommh = c(63, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 35.75, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 43.25, NA, NA, 63, 41.5,
34.25, 38.5, 39, 38.5, NA, 49.75, 57.5, 59.25, 50, 42.75,
45, 49, 32.75, NA, 35.75, 64.75, 50.5, 46.5, 39.75, 51.75,
34.75, 61.25, 46, 43, 56.25, 47, 42.25, 36.5, 34.5, 47, 50,
35, 48.25, 46.5, 58.5, 35.5, 55.25, 43.5, 42.75, 35.75, 38,
35.5, 50, 38.25, 57, 45.75, 38.5, 44.25, 51.75, NA, 38.25,
39.75, 34, 57.25, 39.25, 42.25, 37.25, NA, 32.75, 52.75,
NA, NA, 55.75, 62.25, 59.75, 43.75, 59.75, 35.75, NA, 34.25,
59.25, 39, 34.75, 32.75, NA, 53.5, NA, 40.5, 50, 33.5, 45.25,
41, 50, NA, 38.5, 61.5, 36.25, 46.25, 46, 44.75, 44.75, 62.5,
38.25, 49.5, 33.75, NA, 50.25, 43, 43.75, 42.25, 60.5, NA,
50.25, 54.75, 42.75, 45.75, 61, 58.25, 44.5, 46.5, 34.25,
56.75, 40.5, 47, 42.25, 48, 44, 36.75, 39.75, 48.75, 38.25,
49.25, 49.25, NA, NA, 34.25, 44.5, NA, 51, 44, 50.75, 56.25,
35, 55, 58.75, 56.5, 68.75, 54, 53, 41.5, 50.75, NA, 32.75,
46.75, 32.75, 43, 57, 55.25, NA, NA, 43.75, 55.5, NA, NA,
32.75, NA, NA, NA, NA, 60.5, 32.75, NA, 68.25, 50.5, 32.75,
66.5, 33, 38.5, 43, 43.75, 62.75, 47, 36.5, 39.5, 39.5),
risk6 = structure(c(0, 0, 0, 0, 3, 1, 1, 1, 1, 0, 1, 1, 0,
0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 1, 2, 1, 1, 0, 0, 2, 1, 1,
2, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 2, 1, 3, 2, 2, 0, 0, 0,
2, 0, 2, 2, 1, 2, 2, 1, 3, 2, 3, 1, 1, 0, 1, 3, 1, 2, 2,
0, 1, 0, 0, 1, 3, 1, 0, 1, 0, 0, 1, 3, 0, 1, 1, 0, 0, 2,
3, 3, 1, 2, 3, 2, 0, 0, 4, 1, 2, 1, 3, 2, 1, 2, 0, 1, 1,
2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1,
NA, 1, 1, 1, 2, 0, NA, 3, 0, 2, 2, 3, 4, 4, 0, 1, 0, 2, 3,
2, 2, 2, 2, 1, 3, 2, 2, 3, 1, 1, 1, 0, 0, 1, 1, 0, 2, 0,
1, 2, 3, 1, 1, 1, 2, 1, 2, 0, 0, 2, 2, 0, 1, 2, 0, 0, 2,
1, 1, 1, 1, 1, 3, 1, 0, 3, 0, 1, 0, 1, 1, 1, 2, 2, 0, 2,
3, 3, 0, 0, 0, 1, 2, 1, 1, 1, 0, 1, 1, 3, 1, 1, 0, 0, 3,
2, 0, 0, 3, 2, 1, 3, 1, 0, 3, 0, 1, 1, 2, 3, 3, 1, 4, 2,
3, 2, 2), format.spss = "F8.2", display_width = 10L), eadiff = structure(c(-1.26734803867686,
-0.355541076313792, 0.518653050779668, 1.50568568368194,
0.0940935989894723, 2.07356799670629, 1.01843817310907, -1.26734803867686,
-0.317928241044189, 0.531190662536203, 0.0940935989894723,
-1.47335895869369, -0.586627219843691, -1.26734803867686,
0.325179742519372, 0.556265886049271, 1.4179224013862, 1.2244490931259,
-0.586627219843691, 0.081555987232938, -0.149530156296961,
-0.380616299826861, -0.805175751617057, -0.368078688070326,
0.0940935989894723, -0.124454932783893, 0.955750114326398,
-0.805175751617057, 0.531190662536203, -0.830250975130125,
0.968287726082933, 0.749739194309568, -0.368078688070326,
-1.03626189514696, 3.19138587908619, -0.574089608087157,
1.67408376842917, -0.586627219843691, -0.343003464557258,
-0.162067768053496, 0.325179742519372, -1.24227281516379,
-1.03626189514696, 0.749739194309568, 0.325179742519372,
0.556265886049271, 0.762276806066102, -0.817713363373591,
-0.805175751617057, 0.119168822502541, -0.805175751617057,
-0.149530156296961, 0.0940935989894723, -1.48589657045022,
1.01843817310907, 0.312642130762837, 1.21191148136937, -0.355541076313792,
-1.04879950690349, -0.368078688070326, -0.124454932783893,
0.312642130762837, -1.25481042692032, -0.136992544540427,
1.01843817310907, -0.124454932783893, -0.368078688070326,
-0.805175751617057, 0.081555987232938, -0.805175751617057,
0.325179742519372, 2.97283734731282, 0.337717354275906, 0.0690183754764037,
-0.136992544540427, -0.830250975130125, 3.03552540609549,
0.0940935989894723, 0.0690183754764037, -0.124454932783893,
-0.817713363373591, -0.355541076313792, 0.312642130762837,
0.980825337839467, -0.343003464557258, 0.993362949596001,
-0.586627219843691, -0.574089608087157, -1.02372428339042,
-0.561551996330623, -0.111917321027358, -0.136992544540427,
-0.149530156296961, -0.830250975130125, 0.568803497805805,
0.0690183754764037, -0.805175751617057, -0.830250975130125,
0.556265886049271, 0.968287726082933, 0.531190662536203,
0.312642130762837, 0.337717354275906, 0.774814417822636,
0.337717354275906, 0.337717354275906, -0.586627219843691,
0.106631210746007, -1.02372428339042, -0.574089608087157,
-0.355541076313792, 0.737201582553033, 0.325179742519372,
0.312642130762837, 0.556265886049271, 0.0940935989894723,
0.300104519006303, -0.330465852800723, 0.0940935989894723,
-0.355541076313792, -0.599164831600226, 0.312642130762837,
0.531190662536203, -1.25481042692032, 0.531190662536203,
1.89263230020253, -0.817713363373591, -1.02372428339042,
0.980825337839467, -0.149530156296961, -0.586627219843691,
1.23698670488244, 0.556265886049271, 0.325179742519372, -0.817713363373591,
1.01843817310907, -1.02372428339042, -0.805175751617057,
-0.355541076313792, 1.67408376842917, 0.0690183754764037,
-0.368078688070326, -0.124454932783893, 0.980825337839467,
-1.03626189514696, 0.119168822502541, -1.03626189514696,
-1.03626189514696, 1.4555352366558, -0.136992544540427, -1.04879950690349,
0.749739194309568, -0.792638139860522, 0.312642130762837,
-0.0993797092708241, -0.17460537981003, -0.343003464557258,
-0.586627219843691, 0.300104519006303, -0.355541076313792,
-0.805175751617057, 0.518653050779668, -1.26734803867686,
-1.25481042692032, -0.368078688070326, -0.805175751617057,
-0.343003464557258, -0.343003464557258, -0.599164831600226,
-0.124454932783893, 1.66154615667263, -0.586627219843691,
-0.586627219843691, -0.124454932783893, 0.955750114326398,
-0.355541076313792, -0.343003464557258, 0.0940935989894723,
-0.792638139860522, -0.599164831600226, NA, -0.586627219843691,
-1.26734803867686, 0.762276806066102, 1.2244490931259, 0.081555987232938,
-0.574089608087157, -1.01118667163389, 0.312642130762837,
0.081555987232938, -0.368078688070326, -1.26734803867686,
1.63647093315956, -0.368078688070326, 0.531190662536203,
0.081555987232938, 0.543728274292737, 0.0564807637198694,
0.955750114326398, -1.25481042692032, 1.44299762489927, -1.04879950690349,
0.106631210746007, -0.586627219843691, 0.0940935989894723,
-0.162067768053496, 0.0940935989894723, -0.111917321027358,
0.968287726082933, 0.0940935989894723, 0.312642130762837,
-0.586627219843691, 0.543728274292737, -0.124454932783893,
0.543728274292737, -0.817713363373591, -0.586627219843691,
-0.368078688070326, 0.0940935989894723, -0.599164831600226,
-1.03626189514696, 0.774814417822636, 0.106631210746007,
-0.111917321027358, -0.817713363373591, -0.330465852800723,
0.993362949596001, -0.368078688070326, 1.19937386961283,
0.531190662536203, 0.749739194309568, 1.6490085449161, 0.0690183754764037,
-0.574089608087157, -0.368078688070326, 1.00590056135254,
1.4555352366558, -0.574089608087157, -0.586627219843691,
-0.817713363373591, -0.817713363373591, 0.0940935989894723,
-0.792638139860522, 0.0690183754764037), format.spss = "F8.2", display_width = 10L)), .Names = c("mompa",
"momabhx", "mommh", "risk6", "eadiff"), row.names = c(NA, -244L
), class = "data.frame")
And here is the same dataset with 4 source variables (depr, anxt, host, bpsipdr1)
> dput(dat3)
structure(list(mompa = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0,
1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0,
0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1,
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0), format.spss = "F8.2", display_width = 10L), momabhx = structure(c(1,
0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1,
0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1), format.spss = "F8.2", display_width = 10L),
risk6 = structure(c(0, 0, 0, 0, 3, 1, 1, 1, 1, 0, 1, 1, 0,
0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 1, 2, 1, 1, 0, 0, 2, 1, 1,
2, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 2, 1, 3, 2, 2, 0, 0, 0,
2, 0, 2, 2, 1, 2, 2, 1, 3, 2, 3, 1, 1, 0, 1, 3, 1, 2, 2,
0, 1, 0, 0, 1, 3, 1, 0, 1, 0, 0, 1, 3, 0, 1, 1, 0, 0, 2,
3, 3, 1, 2, 3, 2, 0, 0, 4, 1, 2, 1, 3, 2, 1, 2, 0, 1, 1,
2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1,
NA, 1, 1, 1, 2, 0, NA, 3, 0, 2, 2, 3, 4, 4, 0, 1, 0, 2, 3,
2, 2, 2, 2, 1, 3, 2, 2, 3, 1, 1, 1, 0, 0, 1, 1, 0, 2, 0,
1, 2, 3, 1, 1, 1, 2, 1, 2, 0, 0, 2, 2, 0, 1, 2, 0, 0, 2,
1, 1, 1, 1, 1, 3, 1, 0, 3, 0, 1, 0, 1, 1, 1, 2, 2, 0, 2,
3, 3, 0, 0, 0, 1, 2, 1, 1, 1, 0, 1, 1, 3, 1, 1, 0, 0, 3,
2, 0, 0, 3, 2, 1, 3, 1, 0, 3, 0, 1, 1, 2, 3, 3, 1, 4, 2,
3, 2, 2), format.spss = "F8.2", display_width = 10L), eadiff = structure(c(-1.26734803867686,
-0.355541076313792, 0.518653050779668, 1.50568568368194,
0.0940935989894723, 2.07356799670629, 1.01843817310907, -1.26734803867686,
-0.317928241044189, 0.531190662536203, 0.0940935989894723,
-1.47335895869369, -0.586627219843691, -1.26734803867686,
0.325179742519372, 0.556265886049271, 1.4179224013862, 1.2244490931259,
-0.586627219843691, 0.081555987232938, -0.149530156296961,
-0.380616299826861, -0.805175751617057, -0.368078688070326,
0.0940935989894723, -0.124454932783893, 0.955750114326398,
-0.805175751617057, 0.531190662536203, -0.830250975130125,
0.968287726082933, 0.749739194309568, -0.368078688070326,
-1.03626189514696, 3.19138587908619, -0.574089608087157,
1.67408376842917, -0.586627219843691, -0.343003464557258,
-0.162067768053496, 0.325179742519372, -1.24227281516379,
-1.03626189514696, 0.749739194309568, 0.325179742519372,
0.556265886049271, 0.762276806066102, -0.817713363373591,
-0.805175751617057, 0.119168822502541, -0.805175751617057,
-0.149530156296961, 0.0940935989894723, -1.48589657045022,
1.01843817310907, 0.312642130762837, 1.21191148136937, -0.355541076313792,
-1.04879950690349, -0.368078688070326, -0.124454932783893,
0.312642130762837, -1.25481042692032, -0.136992544540427,
1.01843817310907, -0.124454932783893, -0.368078688070326,
-0.805175751617057, 0.081555987232938, -0.805175751617057,
0.325179742519372, 2.97283734731282, 0.337717354275906, 0.0690183754764037,
-0.136992544540427, -0.830250975130125, 3.03552540609549,
0.0940935989894723, 0.0690183754764037, -0.124454932783893,
-0.817713363373591, -0.355541076313792, 0.312642130762837,
0.980825337839467, -0.343003464557258, 0.993362949596001,
-0.586627219843691, -0.574089608087157, -1.02372428339042,
-0.561551996330623, -0.111917321027358, -0.136992544540427,
-0.149530156296961, -0.830250975130125, 0.568803497805805,
0.0690183754764037, -0.805175751617057, -0.830250975130125,
0.556265886049271, 0.968287726082933, 0.531190662536203,
0.312642130762837, 0.337717354275906, 0.774814417822636,
0.337717354275906, 0.337717354275906, -0.586627219843691,
0.106631210746007, -1.02372428339042, -0.574089608087157,
-0.355541076313792, 0.737201582553033, 0.325179742519372,
0.312642130762837, 0.556265886049271, 0.0940935989894723,
0.300104519006303, -0.330465852800723, 0.0940935989894723,
-0.355541076313792, -0.599164831600226, 0.312642130762837,
0.531190662536203, -1.25481042692032, 0.531190662536203,
1.89263230020253, -0.817713363373591, -1.02372428339042,
0.980825337839467, -0.149530156296961, -0.586627219843691,
1.23698670488244, 0.556265886049271, 0.325179742519372, -0.817713363373591,
1.01843817310907, -1.02372428339042, -0.805175751617057,
-0.355541076313792, 1.67408376842917, 0.0690183754764037,
-0.368078688070326, -0.124454932783893, 0.980825337839467,
-1.03626189514696, 0.119168822502541, -1.03626189514696,
-1.03626189514696, 1.4555352366558, -0.136992544540427, -1.04879950690349,
0.749739194309568, -0.792638139860522, 0.312642130762837,
-0.0993797092708241, -0.17460537981003, -0.343003464557258,
-0.586627219843691, 0.300104519006303, -0.355541076313792,
-0.805175751617057, 0.518653050779668, -1.26734803867686,
-1.25481042692032, -0.368078688070326, -0.805175751617057,
-0.343003464557258, -0.343003464557258, -0.599164831600226,
-0.124454932783893, 1.66154615667263, -0.586627219843691,
-0.586627219843691, -0.124454932783893, 0.955750114326398,
-0.355541076313792, -0.343003464557258, 0.0940935989894723,
-0.792638139860522, -0.599164831600226, NA, -0.586627219843691,
-1.26734803867686, 0.762276806066102, 1.2244490931259, 0.081555987232938,
-0.574089608087157, -1.01118667163389, 0.312642130762837,
0.081555987232938, -0.368078688070326, -1.26734803867686,
1.63647093315956, -0.368078688070326, 0.531190662536203,
0.081555987232938, 0.543728274292737, 0.0564807637198694,
0.955750114326398, -1.25481042692032, 1.44299762489927, -1.04879950690349,
0.106631210746007, -0.586627219843691, 0.0940935989894723,
-0.162067768053496, 0.0940935989894723, -0.111917321027358,
0.968287726082933, 0.0940935989894723, 0.312642130762837,
-0.586627219843691, 0.543728274292737, -0.124454932783893,
0.543728274292737, -0.817713363373591, -0.586627219843691,
-0.368078688070326, 0.0940935989894723, -0.599164831600226,
-1.03626189514696, 0.774814417822636, 0.106631210746007,
-0.111917321027358, -0.817713363373591, -0.330465852800723,
0.993362949596001, -0.368078688070326, 1.19937386961283,
0.531190662536203, 0.749739194309568, 1.6490085449161, 0.0690183754764037,
-0.574089608087157, -0.368078688070326, 1.00590056135254,
1.4555352366558, -0.574089608087157, -0.586627219843691,
-0.817713363373591, -0.817713363373591, 0.0940935989894723,
-0.792638139860522, 0.0690183754764037), format.spss = "F8.2", display_width = 10L),
host = structure(c(68, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 38, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 41, 49, 41, 78, 41, 41,
49, 41, 45, 45, 51, 71, 73, 62, 51, 51, 65, 38, NA, 38, 70,
58, 45, 38, 64, 38, 72, 55, 45, 60, 58, 38, 38, 38, 38, 45,
38, 38, 51, 60, 38, 68, 51, 60, 38, 45, 38, 38, 38, 68, 45,
38, 51, 51, NA, 45, 38, 38, 66, 38, 45, 38, 65, 38, 51, NA,
NA, 60, 71, 70, 45, 71, 38, NA, 38, 55, 38, 38, 38, NA, 62,
58, 38, 58, 38, 51, 38, 72, 64, 45, 71, 45, 45, 51, 45, 45,
75, 38, 51, 38, 58, 55, 55, 38, 38, 70, 55, 65, 64, 55, 55,
69, 68, 55, 38, 38, 55, 45, 58, 38, 64, 38, 51, 45, 45, 38,
45, 62, 66, NA, 38, 45, 58, 58, 51, 65, 64, 38, 60, 60, 70,
75, 65, 62, 51, 62, NA, 38, 58, 38, 45, 38, 65, NA, 64, 38,
51, NA, NA, 38, NA, NA, NA, NA, 70, 38, NA, 75, 55, 38, 71,
38, 38, 55, 55, 58, 58, 45, 45, 45), format.spss = "F2.0", display_width = 11L),
anxt = structure(c(73, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 39, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 51, 51, 51, 66, 51, 40,
40, 51, 55, 62, 55, 55, 67, 51, 55, 55, 65, 39, NA, 39, 62,
59, 59, 39, 67, 39, 62, 51, 51, 65, 51, 51, 51, 39, 51, 59,
39, 59, 55, 71, 39, 53, 51, 51, 51, 51, 39, 55, 39, 65, 59,
51, 39, 65, NA, 39, 51, 39, 65, 51, 51, 39, 59, 39, 67, NA,
NA, 59, 70, 67, 39, 65, 39, NA, 39, 65, 51, 39, 39, NA, 62,
65, 55, 39, 39, 59, 39, 70, NA, 55, 67, 39, 51, 55, 51, 55,
70, 55, 56, 39, 70, 55, 51, 51, 51, 62, NA, 59, 62, 55, 59,
62, 59, 51, 51, 39, 65, 39, 55, 62, 51, 55, 39, 39, 62, 51,
55, 62, NA, NA, 39, 51, NA, 62, 39, 62, 59, 39, 59, 71, 51,
74, 59, 51, 51, 62, NA, 39, 51, 39, 51, 72, 62, NA, 62, 55,
62, NA, NA, 39, NA, NA, NA, NA, 70, 39, NA, 70, 65, 39, 73,
39, 51, 51, 55, 74, 62, 39, 51, 51), format.spss = "F2.0", display_width = 11L),
depr = structure(c(71, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 42, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 53, 68, NA, 71, 44, 44,
44, 44, 42, 42, 61, 64, 57, 70, 42, 50, 54, 42, NA, 50, 78,
57, 50, 42, 54, 42, 68, 42, 54, 64, 54, 54, 42, 42, 68, 64,
42, 65, 50, 68, 42, 70, 50, 42, 42, 42, 42, 50, 42, 60, 54,
42, 56, 64, NA, 42, 42, 42, 61, 42, 42, 42, 57, 42, 64, 57,
NA, 68, 73, 72, 57, 68, 42, 57, 42, 65, 50, 42, 42, NA, 62,
62, 42, 61, 42, 54, 54, 42, 71, 42, 64, 42, 54, 54, 57, 42,
70, 42, 54, 42, 57, 57, 54, 60, 54, 71, 60, 54, 60, 42, 42,
68, 70, 50, 60, 42, 69, 54, 42, 42, 57, 50, 42, 42, 54, 42,
65, 57, 68, NA, 42, 60, 64, 50, 60, 50, 70, 42, 65, 64, 65,
71, 64, 62, 42, 62, NA, 42, 42, 42, 57, 70, 61, NA, 57, 42,
68, NA, NA, 42, NA, NA, NA, NA, 65, 42, NA, 75, 61, 42, 75,
42, 42, 54, 50, 72, 42, 50, 42, 42), format.spss = "F2.0", display_width = 11L),
bpsipdr1 = structure(c(40, 26, 34, 29, 23, 41, 37, 21, 38,
NA, 33, 28, 25, NA, NA, 15, 18, 30, NA, NA, 28, 34, NA, 51,
24, 28, 23, 12, 39, 55, 28, NA, 26, 18, 33, NA, 27, 32, 27,
23, 28, 41, NA, 22, 21, 26, 26, 36, 16, 24, 24, 25, 23, 24,
26, 35, 32, 27, 38, 25, 26, 32, 27, 41, 28, NA, 27, 37, 30,
12, 21, 20, 12, NA, 32, 40, 40, 17, 23, 24, 12, 12, NA, 16,
49, 28, 32, 40, 22, 20, 43, 36, 22, 36, 25, 26, 15, 19, 31,
32, 21, 31, 30, 35, 23, 30, 22, 18, 12, 14, 23, 57, 34, 35,
25, 23, 31, 27, 33, 27, 28, 17, 37, 26, 31, 30, NA, 12, 29,
NA, 19, 36, 35, 30, 34, 35, 24, 34, 18, 52, 17, 20, 12, 25,
28, NA, 27, 42, 15, 17, 33, 16, 30, 12, 44, 19, 35, 24, 26,
37, 35, 18, 37, 16, NA, 34, 12, 26, 26, 39, 34, 23, 33, 19,
27, 45, 36, 22, 37, 18, 38, 24, 33, 27, 20, 33, 15, 33, 34,
22, 32, 16, 30, 24, 18, 22, 42, 34, 26, 26, 32, 21, 36, 40,
40, 55, 28, 37, 22, 17, 41, 12, 36, 12, 19, 48, 33, 26, NA,
40, 41, 24, 27, 12, 36, 24, 38, NA, 37, 12, 20, 53, 21, 12,
47, 13, 23, 12, 15, 47, 26, 12, 20, 20), format.spss = "F3.0", display_width = 11L)), .Names = c("mompa",
"momabhx", "risk6", "eadiff", "host", "anxt", "depr", "bpsipdr1"
), row.names = c(NA, -244L), class = "data.frame")
Related
I have the below data:
> paste(data_s)
[1] "c(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, 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, 0, 0, 0, 0, 0, 0, 0, 0)"
[2] "c(34, 34, 35, 35, 35, 34, 6, 34, 34, 6, 34, 34, 34, 6, 6, 6, 34, 34, 35, 6, 34, 34, 34, 34, 34, 34, 34, 34, 6, 34, 35, 35, 34, 34, 6, 34, 34, 34, 34, 6, 6, 35, 34, 34, 34, 35, 6, 35, 34, 34, 34, 34, 34, 34, 6, 34, 34, 6, 34, 34, 34, 6, 34, 34, 34, 34, 6, 34, 34, 34, 35, 6, 35, 34, 34, 35, 34, 6, 6, 35, 34, 34, 6, 34, 6, 6, 34, 34, 6, 34, 6, 35, 34, 6, 34, 35, 34, 6, 34, 34)"
[3] "c(1, 1, 4, 0, 3, 4, 5, 2, 4, 1, 2, 1, 4, 9, 9, 1, 1, 5, 1, 4, 4, 2, 3, 2, 3, 2, 1, 2, 5, 6, 5, 5, 5, 1, 5, 5, 2, 1, 1, 3, 4, 2, 9, 1, 4, 3, 2, 5, 2, 2, 3, 4, 4, 5, 5, 4, 1, 2, 0, 3, 4, 2, 2, 5, 0, 2, 5, 3, 3, 1, 0, 1, 4, 2, 5, 1, 1, 4, 2, 3, 5, 1, 5, 0, 2, 4, 1, 5, 4, 2, 2, 4, 5, 1, 2, 2, 0, 3, 7, 3)"
> str(data_s)
tibble [100 × 3] (S3: tbl_df/tbl/data.frame)
$ y : num [1:100] 0 0 0 0 0 0 0 0 1 0 ...
$ x1: num [1:100] 34 34 35 35 35 34 6 34 34 6 ...
$ x2: num [1:100] 1 1 4 0 3 4 5 2 4 1 ...
- attr(*, "na.action")= 'omit' Named int [1:197659] 4 5 6 7 9 14 19 20 24 27 ...
..- attr(*, "names")= chr [1:197659] "4" "5" "6" "7" ...
I am using vivi function using vivid package to explore the feature importance of variables.
I write the below code:
library("vivid")
library("dplyr")
library("xgboost")
y=data_s["y"]
x=data_s[,c("x1","x2")]
gbst <- xgboost(data = as.matrix(x),
label = as.matrix(y),
nrounds = 600)
pFun <- function(fit, data, ...) predict(fit, as.matrix(x))
viviGBst <- vivi(fit = gbst,
data = data_s,
response = "y",
reorder = FALSE,
normalized = FALSE,
predictFun = pFun)
But I get the below error:
Error:
! Assigned data `predict(x, data = X[, cols, drop = FALSE])` must be compatible with existing data.
✖ Existing data has 5000 rows.
✖ Assigned data has 100 rows.
ℹ Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.
Why do I get this error and how can I fix it?
I will be very glad for any help.
Thanks.
Looking at similar questions, I could not find one that matched my need.
If one does contain a solution, please share its link.
I have this dput-produced data:
structure(list(Player = c("Seth Lugo", "Jacob deGrom", "Rick Porcello",
"David Peterson", "Michael Wacha", "Seth Lugo", "Jacob deGrom",
"Rick Porcello", "David Peterson", "Steven Matz", "Seth Lugo",
"Jacob deGrom", "Rick Porcello", "David Peterson", "Seth Lugo",
"Jacob deGrom", "Rick Porcello", "Michael Wacha", "David Peterson",
"Jacob deGrom", "Seth Lugo", "Rick Porcello", "Robert Gsellman",
"Michael Wacha", "Ariel Jurado", "Jacob deGrom", "Rick Porcello",
"Seth Lugo", "Robert Gsellman", "David Peterson"), Date = structure(c(1601164800,
1601078400, 1601078400, 1600905600, 1600819200, 1600732800, 1600646400,
1600560000, 1600473600, 1600387200, 1600300800, 1600214400, 1600128000,
1599955200, 1599868800, 1599782400, 1599609600, 1599523200, 1599436800,
1599350400, 1599264000, 1599177600, 1599091200, 1599004800, 1598918400,
1598832000, 1598745600, 1598745600, 1598659200, 1598572800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), DblHdr = c(0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 2), DateStr = c("09/27/2020",
"09/26/2020", "09/26/2020", "09/24/2020", "09/23/2020", "09/22/2020",
"09/21/2020", "09/20/2020", "09/19/2020", "09/18/2020", "09/17/2020",
"09/16/2020", "09/15/2020", "09/13/2020", "09/12/2020", "09/11/2020",
"09/09/2020", "09/08/2020", "09/07/2020", "09/06/2020", "09/05/2020",
"09/04/2020", "09/03/2020", "09/02/2020", "09/01/2020", "08/31/2020",
"08/30/2020", "08/30/2020", "08/29/2020", "08/28/2020"), Month = c("09",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "09", "09", "09", "09", "09", "09", "09", "09", "09",
"09", "09", "08", "08", "08", "08", "08"), Tm = c("NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM", "NYM",
"NYM"), Opp = c("WSN", "WSN", "WSN", "WSN", "TBR", "TBR", "TBR",
"ATL", "ATL", "ATL", "PHI", "PHI", "PHI", "TOR", "TOR", "TOR",
"BAL", "BAL", "PHI", "PHI", "PHI", "PHI", "NYY", "BAL", "BAL",
"MIA", "NYY", "NYY", "NYY", "NYY"), Rslt = c("L 5-15", "L 3-4",
"L 3-5", "W 3-2", "L 5-8", "W 5-2", "L 1-2", "L 0-7", "W 7-2",
"L 2-15", "W 10-6", "W 5-4", "L 1-4", "L 3-7", "L 2-3", "W 18-1",
"W 7-6", "L 2-11", "L 8-9", "W 14-1", "W 5-1", "L 3-5", "W 9-7",
"W 9-4", "L 5-9", "L 3-5", "L 7-8", "L 2-5", "L 1-2", "W 4-3"
), W_L = c("L", "L", "L", "W", "L", "W", "L", "L", "W", "L",
"W", "W", "L", "L", "L", "W", "W", "L", "L", "W", "W", "L", "W",
"W", "L", "L", "L", "L", "L", "W"), temp = c("L 5", "L 3", "L 3",
"W 3", "L 5", "W 5", "L 1", "L 0", "W 7", "L 2", "W 10", "W 5",
"L 1", "L 3", "L 2", "W 18", "W 7", "L 2", "L 8", "W 14", "W 5",
"L 3", "W 9", "W 9", "L 5", "L 3", "L 7", "L 2", "L 1", "W 4"
), RS = c(5, 3, 3, 3, 5, 5, 1, 0, 7, 2, 10, 5, 1, 3, 2, 18, 7,
2, 8, 14, 5, 3, 9, 9, 5, 3, 7, 2, 1, 4), RA = c(15, 4, 5, 2,
8, 2, 2, 7, 2, 15, 6, 4, 4, 7, 3, 1, 6, 11, 9, 1, 1, 5, 7, 4,
9, 5, 8, 5, 2, 3), Rdiff = c(-10, -1, -2, 1, -3, 3, -1, -7, 5,
-13, 4, 1, -3, -4, -1, 17, 1, -9, -1, 13, 4, -2, 2, 5, -4, -2,
-1, -3, -1, 1), absV = c(10, 1, 2, 1, 3, 3, 1, 7, 5, 13, 4, 1,
3, 4, 1, 17, 1, 9, 1, 13, 4, 2, 2, 5, 4, 2, 1, 3, 1, 1), App_Dec = c("GS-2, L",
"GS-5", "GS-3, L", "GS-7, W", "GS-6, L", "GS-7, W", "GS-7, L",
"GS-7, L", "GS-6, W", "GS-3, L", "GS-2", "GS-2", "GS-6, L", "GS-5, L",
"GS-6, L", "GS-6, W", "GS-4", "GS-4, L", "GS-2", "GS-7, W", "GS-5, W",
"GS-6", "GS-2", "GS-3", "GS-4", "GS-6, L", "GS-5", "GS-4", "GS-4",
"GS-4"), IP = c(1.1, 5, 3, 7, 6, 6.1, 7, 7, 6, 2.2, 1.2, 2, 6,
5, 5.1, 6, 4, 4, 2, 7, 5, 6, 1.2, 3, 4, 6, 5, 3.2, 4, 4), H = c(5,
5, 8, 4, 6, 4, 4, 3, 3, 8, 8, 4, 6, 3, 7, 3, 10, 7, 3, 3, 4,
3, 4, 4, 9, 6, 4, 4, 4, 4), R = c(6, 3, 5, 1, 4, 2, 2, 1, 1,
6, 6, 3, 4, 2, 3, 1, 5, 5, 5, 1, 1, 2, 4, 2, 5, 4, 2, 1, 1, 3
), ER = c(6, 3, 3, 1, 4, 1, 2, 1, 1, 6, 6, 3, 4, 2, 3, 1, 5,
4, 5, 1, 1, 2, 4, 2, 5, 1, 2, 1, 1, 3), BB = c(2, 2, 1, 1, 0,
1, 2, 2, 4, 3, 0, 1, 2, 2, 1, 2, 0, 0, 4, 2, 2, 2, 4, 1, 0, 2,
2, 2, 0, 3), SO = c(1, 10, 3, 4, 4, 7, 14, 10, 10, 5, 3, 1, 5,
2, 5, 9, 3, 3, 3, 12, 8, 6, 0, 2, 2, 9, 2, 7, 4, 3), HR = c(0,
2, 1, 0, 2, 1, 1, 1, 1, 2, 4, 0, 1, 1, 0, 0, 0, 2, 1, 1, 1, 0,
0, 0, 1, 1, 0, 1, 1, 0), UER = c(0, 0, 2, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0),
Pit = c(38, 113, 67, 107, 66, 95, 112, 100, 102, 76, 52,
40, 94, 81, 91, 102, 66, 71, 70, 108, 81, 100, 52, 69, 84,
103, 86, 60, 57, 70), Str = c(24, 78, 42, 68, 45, 66, 70,
70, 62, 45, 30, 25, 66, 52, 60, 68, 45, 49, 37, 74, 50, 65,
22, 41, 53, 72, 55, 39, 33, 37), GSc = c(19, 53, 29, 68,
48, 65, 73, 75, 68, 20, 18, 36, 47, 53, 46, 69, 25, 33, 29,
77, 61, 62, 27, 44, 26, 57, 51, 54, 54, 42), BF = c(12, 22,
19, 26, 23, 24, 26, 26, 24, 18, 14, 11, 26, 20, 24, 23, 21,
20, 14, 26, 21, 23, 13, 15, 21, 27, 20, 16, 15, 18), AB = c(8,
20, 18, 24, 23, 23, 23, 23, 20, 15, 13, 9, 24, 18, 22, 21,
21, 20, 9, 24, 19, 21, 8, 13, 20, 25, 18, 14, 15, 15), H2B = c(2,
0, 1, 1, 1, 0, 2, 0, 2, 2, 1, 2, 1, 0, 2, 1, 1, 1, 1, 1,
0, 0, 1, 0, 2, 2, 2, 0, 1, 0), H3B = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 1, 0), IBB = c(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),
HBP = c(1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), SH = c(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), SF = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0), GDP = c(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, 1, 1, 0, 1), SB = c(0, 1,
1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 0,
1, 0, 0, 0, 3, 0, 0, 0, 0), CS = c(0, 0, 0, 0, 1, 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), PO = c(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), BK = c(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), WP = c(0, 1, 1, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0), ERA = c("40.5", "5.4", "9", "1.29", "6", "1.42",
"2.57", "1.29", "1.5", "20.25", "32.4", "13.5", "6", "3.6",
"5.0599999999999996", "1.5", "11.25", "9", "22.5", "1.29",
"1.8", "3", "21.6", "6", "11.25", "1.5", "3.6", "2.4500000000000002",
"2.25", "6.75"), WPA = c(-0.471, -0.087, -0.256, 0.34, -0.22,
0.18, 0.107, 0.219, 0.229, -0.358, -0.487, -0.186, -0.156,
0.036, -0.047, 0.049, -0.329, -0.321, -0.34, 0.193, 0.156,
0.07, -0.312, -0.042, -0.278, -0.271, 0.029, 0.02, 0.092,
-0.174), RE24 = c(-5.122, -0.193, -3.316, 2.931, -1.08, 1.509,
1.406, 2.406, 1.92, -4.641, -5.444, -1.919, -0.758, 0.679,
0.245, 2.215, -3.054, -3.054, -4.027, 2.406, 1.433, 0.92,
-3.788, -0.359, -2.812, -1.08, 0.707, 0.364, 1.166, -0.834
), aLI = c(1.45, 1.244, 0.974, 1.271, 0.965, 0.921, 0.955,
0.888, 1.066, 0.962, 0.767, 1.073, 0.941, 0.852, 1.353, 0.392,
0.857, 0.805, 0.904, 0.75, 1.037, 0.861, 1.232, 1.355, 0.914,
1.239, 1.213, 1.28, 0.748, 1.407)), row.names = c(NA, -30L
), class = c("tbl_df", "tbl", "data.frame"))
Desired output:
The numbers starting in the second column are the total absV values for each player for each column. The last column contains the sum of all the absV values for each player where absV > 5. Only a sample of the first 3 rows are shown, and the absV values are just filler numbers.
| Player | 1 | 2 | 3 | 4 | 5 | >5 |
| deGrom | 2 | 3 | 5 | 0 | 1 | 3 |
| Matz | 2 | 3 | 5 | 0 | 1 | 3 |
Code tried (I need help getting beyond the point shown). I would prefer if the code uses dplyr:
starter %>%
select(Player, absV) %>%
group_by(Player, absV) %>%
summarize(numG= n()) %>%
arrange(Player,absV)
To do this you to bifurcate your data with rows per player >5 and <=5, then rbind them together and thereafter pivot_wider. Follow this code
library(dplyr)
library(tidyr)
df <- starter %>% group_by(Player) %>%
mutate(row = row_number()) %>%
select(Player, absV, row) %>% arrange(Player)
df %>% filter(row <= 5) %>%
mutate(row = as.character(row)) %>%
rbind(df %>% filter(row > 5) %>%
summarise( absV = sum(absV)) %>%
mutate(row = ">5")) %>%
pivot_wider(id_cols = Player, names_from = row, values_from = absV)
# A tibble: 8 x 7
# Groups: Player [8]
Player `1` `2` `3` `4` `5` `>5`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Ariel Jurado 4 NA NA NA NA NA
2 David Peterson 1 5 4 1 1 NA
3 Jacob deGrom 1 1 1 17 13 2
4 Michael Wacha 3 9 5 NA NA NA
5 Rick Porcello 2 7 3 1 2 1
6 Robert Gsellman 2 1 NA NA NA NA
7 Seth Lugo 10 3 4 1 4 3
8 Steven Matz 13 NA NA NA NA NA
Note. Loading tidyverse package, at once, directly is advised.
Note-2 If you still want to sort absV before changing the data-format, add absV in arrange syntax beforehand joining them..
df <- starter %>% group_by(Player) %>%
arrange(Player, absV) %>%
mutate(row = row_number()) %>%
select(Player, absV, row)
df %>% filter(row <= 5) %>%
mutate(row = as.character(row)) %>%
rbind(df %>% filter(row > 5) %>%
summarise( absV = sum(absV)) %>%
mutate(row = ">5")) %>%
pivot_wider(id_cols = Player, names_from = row, values_from = absV)
#this will give the following diff output
# A tibble: 8 x 7
# Groups: Player [8]
Player `1` `2` `3` `4` `5` `>5`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Ariel Jurado 4 NA NA NA NA NA
2 David Peterson 1 1 1 4 5 NA
3 Jacob deGrom 1 1 1 2 13 17
4 Michael Wacha 3 5 9 NA NA NA
5 Rick Porcello 1 1 2 2 3 7
6 Robert Gsellman 1 2 NA NA NA NA
7 Seth Lugo 1 3 3 4 4 10
8 Steven Matz 13 NA NA NA NA NA
Additional Question in comments below
Follow this code to work out frequency of each absV
df %>% group_by(Player, absV) %>% mutate(freq = n()) %>% ungroup()
#check it
df %>% group_by(Player, absV) %>% mutate(freq = n()) %>% ungroup() %>% select(Player, absV, freq)
Player absV freq
<chr> <dbl> <int>
1 Seth Lugo 10 1
2 Jacob deGrom 1 3
3 Rick Porcello 2 2
4 David Peterson 1 3
5 Michael Wacha 3 1
6 Seth Lugo 3 2
7 Jacob deGrom 1 3
8 Rick Porcello 7 1
9 David Peterson 5 1
10 Steven Matz 13 1
# ... with 20 more rows
Using data.table
library(data.table)
dcast(setDT(starter), Player ~ rowid(Player), value.var = 'absV')
I am trying to create a new variable based on conditions and to take the value of other variables when the condition is met.
Basically, my data.frame looks like this:
df <- data.frame (
party=c("A", "B", "C", "A", "B", "C", "A", "B", "C", "D",
"E", "F", "G", "H", "I","E", "F", "G", "H", "I", "E", "F", "G", "H", "I",
"J", "K", "L", "J", "K", "L", "J", "K", "L"),
edate = c(1991, 1991, 1991, 1995, 1995, 1995, 1998, 1998, 1998, 1998,
2000, 2000, 2000, 2000, 2000, 2005, 2005, 2005, 2005, 2005, 2010, 2010, 2010, 2010, 2010,
1999, 1999, 1999, 2001, 2001, 2001, 2006, 2006, 2006),
RRP = c(1,0,0,1,0,0,1,0,0,0,
0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,
0,0,0,0,0,0, 0,0,0),
absseats = c(0, 20, 30, 5, 25, 20, 0, 10, 28, 12,
100, 50, 50, 25, 0, 120, 30, 75, 0, 15, 90, 60, 70, 5, 15,
10, 20, 40, 30, 30, 10, 50, 10, 10),
country=c(11,11,11,11,11,11,11,11,11,11,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
43, 43, 43, 43, 43, 43, 43, 43, 43),
treat = c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0,0,0,0,0, 1,1,1,1,1,
0, 0, 0,0,0,0,0,0,0))
What I want is a variable that takes the value of edate for which treat changes from 0 to 1 in each observation. And I want to assign value 0 when, for a specific "party", "treat" is always == 0 in the dataset.
Basically I want this:
first.treat <- c( 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998,
2010, 2010, 2010, 2010, 2010,2010, 2010, 2010, 2010, 2010,2010, 2010, 2010, 2010, 2010,
0, 0, 0,0,0,0,0,0,0)
ideal.df<- cbind(df,first.treat)
I tried different things, like different iteration of:
df%>%
group_by(party)%>%
arrange(edate)%>%
mutate(first.treat = if_else(nrow = 1, edate, 0))
Here's a dput of my actual dataframe:
dput(df3[1:100,])
structure(list(country = c(61, 61, 52, 52, 61, 61, 61, 52, 52,
52, 61, 61, 52, 52, 61, 61, 52, 52, 61, 61, 52, 52, 52, 61, 61,
11, 11, 11, 11, 11, 61, 61, 14, 14, 14, 14, 14, 14, 52, 52, 52,
62, 62, 62, 62, 51, 51, 51, 12, 12, 12, 12, 12, 12, 23, 23, 23,
23, 13, 13, 13, 13, 13, 13, 13, 21, 21, 21, 22, 22, 22, 22, 22,
22, 22, 32, 32, 32, 32, 32, 15, 15, 15, 15, 171, 171, 63, 63,
63, 31, 31, 31, 31, 31, 31, 64, 64, 73, 43, 43), countryname = c("United States",
"United States", "Northern Ireland", "Northern Ireland", "United States",
"United States", "United States", "Northern Ireland", "Northern Ireland",
"Northern Ireland", "United States", "United States", "Northern Ireland",
"Northern Ireland", "United States", "United States", "Northern Ireland",
"Northern Ireland", "United States", "United States", "Northern Ireland",
"Northern Ireland", "Northern Ireland", "United States", "United States",
"Sweden", "Sweden", "Sweden", "Sweden", "Sweden", "United States",
"United States", "Finland", "Finland", "Finland", "Finland",
"Finland", "Finland", "Northern Ireland", "Northern Ireland",
"Northern Ireland", "Canada", "Canada", "Canada", "Canada", "United Kingdom",
"United Kingdom", "United Kingdom", "Norway", "Norway", "Norway",
"Norway", "Norway", "Norway", "Luxembourg", "Luxembourg", "Luxembourg",
"Luxembourg", "Denmark", "Denmark", "Denmark", "Denmark", "Denmark",
"Denmark", "Denmark", "Belgium", "Belgium", "Belgium", "Netherlands",
"Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands",
"Netherlands", "Italy", "Italy", "Italy", "Italy", "Italy", "Iceland",
"Iceland", "Iceland", "Iceland", "Mexico", "Mexico", "Australia",
"Australia", "Australia", "France", "France", "France", "France",
"France", "France", "New Zealand", "New Zealand", "Sri Lanka",
"Switzerland", "Switzerland"), oecdmember = c(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, 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, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), eumember = c(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, 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, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), edate = structure(c(-17957,
-17957, -17897, -17897, -16494, -16494, -16494, -16436, -16436,
-16436, -15031, -15031, -14975, -14975, -13568, -13568, -13514,
-13514, -12112, -12112, -11657, -11657, -11657, -10649, -10649,
-9237, -9237, -9237, -9237, -9237, -9186, -9186, -9055, -9055,
-9055, -9055, -9055, -9055, -8980, -8980, -8980, -8970, -8970,
-8970, -8970, -8946, -8946, -8946, -8851, -8851, -8851, -8851,
-8851, -8851, -8838, -8838, -8838, -8838, -8829, -8829, -8829,
-8829, -8829, -8829, -8829, -8719, -8719, -8719, -8630, -8630,
-8630, -8630, -8630, -8630, -8630, -8614, -8614, -8614, -8614,
-8614, -8586, -8586, -8586, -8586, -8579, -8579, -8496, -8496,
-8496, -8453, -8453, -8453, -8453, -8453, -8453, -8436, -8436,
-8401, -8103, -8103), class = "Date"), date = c(192011, 192011,
192101, 192101, 192411, 192411, 192411, 192501, 192501, 192501,
192811, 192811, 192901, 192901, 193211, 193211, 193301, 193301,
193611, 193611, 193802, 193802, 193802, 194011, 194011, 194409,
194409, 194409, 194409, 194409, 194411, 194411, 194503, 194503,
194503, 194503, 194503, 194503, 194506, 194506, 194506, 194506,
194506, 194506, 194506, 194507, 194507, 194507, 194510, 194510,
194510, 194510, 194510, 194510, 194510, 194510, 194510, 194510,
194510, 194510, 194510, 194510, 194510, 194510, 194510, 194602,
194602, 194602, 194605, 194605, 194605, 194605, 194605, 194605,
194605, 194606, 194606, 194606, 194606, 194606, 194606, 194606,
194606, 194606, 194607, 194607, 194609, 194609, 194609, 194611,
194611, 194611, 194611, 194611, 194611, 194611, 194611, 194701,
194710, 194710), party = c(61320, 61620, 52620, 52710, 61320,
61620, 61621, 52320, 52620, 52710, 61320, 61620, 52620, 52710,
61320, 61620, 52320, 52620, 61320, 61620, 52320, 52620, 52710,
61320, 61620, 11220, 11320, 11420, 11620, 11810, 61320, 61620,
14221, 14320, 14420, 14620, 14810, 14901, 52320, 52620, 52710,
62320, 62420, 62620, 62951, 51320, 51420, 51620, 12220, 12320,
12420, 12520, 12620, 12810, 23220, 23320, 23420, 23520, 13220,
13320, 13410, 13420, 13620, 13952, 13953, 21320, 21420, 21520,
22210, 22320, 22420, 22522, 22523, 22525, 22952, 32220, 32320,
32410, 32420, 32520, 15220, 15320, 15620, 15810, 171301, 171601,
63320, 63620, 63810, 31220, 31320, 31421, 31521, 31621, 31622,
64320, 64620, 73330, 43320, 43321), partyname = c("Democratic Party",
"Republican Party", "Unionist Party", "Nationalist Party", "Democratic Party",
"Republican Party", "La Follette Progressive Party", "Northern Ireland Labour Party",
"Unionist Party", "Nationalist Party", "Democratic Party", "Republican Party",
"Unionist Party", "Nationalist Party", "Democratic Party", "Republican Party",
"Northern Ireland Labour Party", "Unionist Party", "Democratic Party",
"Republican Party", "Northern Ireland Labour Party", "Unionist Party",
"Nationalist Party", "Democratic Party", "Republican Party",
"Communist Party of Sweden", "Social Democratic Labour Party",
"People’s Party", "Right Party", "Agrarian Party", "Democratic Party",
"Republican Party", "Finnish People’s Democratic Union", "Finnish Social Democrats",
"National Progressive Party", "National Coalition", "Agrarian Union",
"Swedish People’s Party", "Northern Ireland Labour Party",
"Unionist Party", "Nationalist Party", "Cooperative Commonwealth Federation",
"Liberal Party of Canada", "Progressive Conservative Party",
"Social Credit", "Labour Party", "Liberal Party", "Conservative Party",
"Norwegian Communist Party", "Norwegian Labour Party", "Liberal Party",
"Christian People’s Party", "Conservative Party", "Farmers’ Party",
"Communist Party of Luxembourg", "Socialist Workers’ Party of Luxembourg",
"Patriotic and Democratic Group", "Christian Social People’s Party",
"Danish Communist Party", "Social Democratic Party", "Danish Social-Liberal Party",
"Liberals", "Conservative People’s Party", "Justice Party",
"Danish Union", "Belgian Socialist Party", "Liberal Party", "Francophone Christian Social Party and Flemish Christian People’s Party",
"Communist Party of the Netherlands", "Labour Party", "Freedom Party",
"Catholic People’s Party", "Anti-Revolutionary Party", "Christian Historical Union",
"Reformed Political Party", "Italian Communist Party", "Italian Socialist Party",
"Italian Republican Party", "Italian Liberal Party", "Christian Democrats",
"United Socialist Party", "Social Democratic Party", "Independence Party",
"Progressive Party", "Institutional Revolutionary Party", "National Action Party",
"Australian Labor Party", "Liberal Party of Australia", "Country Party",
"French Communist Party", "French Section of the Workers' International",
"Radical Socialist Party", "Popular Republican Movement", "Rally for the French People - Gaullists",
"Republican Party of Liberty - Conservatives", "New Zealand Labour Party",
"New Zealand National Party", "United National Party", "Social Democratic Party of Switzerland",
"Independents’ Alliance"), partyabbrev = c("Democrats", "Republicans",
"UP", "NP", "Democrats", "Republicans", "", "NILP", "UP", "NP",
"Democrats", "Republicans", "UP", "NP", "Democrats", "Republicans",
"NILP", "UP", "Democrats", "Republicans", "NILP", "UP", "NP",
"Democrats", "Republicans", "SKP", "SAP", "FP", "", "", "Democrats",
"Republicans", "SKDL", "SSDP", "KE", "KK", "Maal", "RKP/SFP",
"NILP", "UP", "NP", "CCF", "LP", "PCP", "Socred", "Labour", "",
"Conservatives", "NKP", "DnA", "V", "KrF", "H", "", "KPL/PCL",
"LSAP/POSL", "", "CSV/PCS", "DKP", "SD", "RV", "V", "KF", "RF",
"DS", "BSP/PSB", "LP/PL", "PSC/CVP", "CPN", "PvdA", "PvdV", "KVP",
"ARP", "CHU", "SGP", "PCI", "PSI", "PRI", "PLI", "DC", "So",
"A", "Sj", "F", "PRI", "PAN", "ALP", "LPA", "CP", "PCF", "SIFO",
"RRRS", "MRP", "RPF", "PRL", "Labour", "National", "UNP", "SPS/PSS",
"LdU/AdI"), parfam = c(30, 60, 60, 70, 30, 60, 60, 30, 60, 70,
30, 60, 60, 70, 30, 60, 30, 60, 30, 60, 30, 60, 70, 30, 60, 20,
30, 40, 60, 80, 30, 60, 20, 30, 40, 60, 80, 90, 30, 60, 70, 30,
40, 60, 95, 30, 40, 60, 20, 30, 40, 50, 60, 80, 20, 30, 40, 50,
20, 30, 40, 40, 60, 95, 95, 30, 40, 50, 20, 30, 40, 50, 50, 50,
95, 20, 30, 40, 40, 50, 20, 30, 60, 80, 30, 60, 30, 60, 80, 20,
30, 40, 50, 60, 60, 30, 60, 60, 30, 30), coderid = c(101, 101,
120, 120, 101, 101, 101, 120, 120, 120, 101, 101, 120, 120, 101,
101, 120, 120, 101, 101, 120, 120, 120, 101, 101, 117, 117, 117,
117, 117, 101, 101, 209, 208, 998, 208, 209, 209, 120, 120, 120,
105, 105, 105, 105, 102, 102, 102, 116, 116, 116, 116, 116, 116,
998, 104, 998, 104, 107, 107, 107, 107, 107, 107, 107, 104, 104,
104, 326, 113, 113, 113, 113, 113, 326, 111, 111, 111, 111, 111,
212, 212, 212, 212, 262, 262, 104, 104, 104, 106, 106, 106, 106,
998, 106, 104, 104, 109, 229, 998), manual = c(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, 1, 998, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 998, 0, 998, 0, 999, 999, 999, 999, 999,
999, 999, 0, 0, 0, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 0, 0, 0, 999, 999, 999, 999, 998, 999, 0, 0, 999,
1, 998), coderyear = c(1981, 1981, 1980, 1980, 1981, 1981, 1981,
1980, 1980, 1980, 1981, 1981, 1980, 1980, 1981, 1981, 1980, 1980,
1981, 1981, 1980, 1980, 1980, 1981, 1981, 1983, 1983, 1983, 1983,
1983, 1981, 1981, 1995, 1993, NA, 1993, 1995, 1995, 1980, 1980,
1980, 1981, 1981, 1981, 1981, 1982, 1982, 1982, 1990, 1990, 1990,
1990, 1990, 1990, NA, 1983, NA, 1983, 1983, 1983, 1983, 1983,
1983, 1983, 1983, 1982, 1982, 1982, 2016, 1982, 1982, 1982, 1982,
1982, 2016, 1980, 1980, 1980, 1980, 1980, 1991, 1991, 1991, 1991,
1998, 1998, 1981, 1981, 1981, 1982, 1982, 1982, 1982, NA, 1982,
1982, 1982, 1980, 1991, NA), testresult = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.669, 0.341, NA,
0.341, 0.669, 0.669, NA, NA, NA, NA, NA, NA, NA, 0.961, 0.961,
0.961, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 0.94, NA, NA, NA, NA, NA, 0.94, NA, NA,
NA, NA, NA, 0.975, 0.975, 0.975, 0.975, 0.88, 0.88, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.412, NA), testeditsim = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0.471, NA, NA, NA, NA, NA, 0.471, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), voteest = c(0, 0, NA, NA, 0, 0, 0, NA, NA,
NA, 0, 0, NA, NA, 0, 0, NA, NA, 0, 0, NA, NA, NA, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA, NA, 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, 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, NA, 0, 0), presvote = c(34.2000007629395,
60.2999992370605, NA, NA, 28.7999992370606, 54.0999984741211,
16.6000003814697, NA, NA, NA, 40.7999992370605, 58.2000007629395,
NA, NA, 57.4000015258789, 39.5999984741211, NA, NA, 60.7999992370605,
36.5, NA, NA, NA, 54.7000007629395, 44.7999992370605, NA, NA,
NA, NA, NA, 53.4000015258789, 45.9000015258789, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
77.869839, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), absseat = c(131, 303, NA, NA, 184, 247, 0, NA, NA, NA,
166, 268, NA, NA, 313, 117, NA, NA, 334, 89, NA, NA, NA, 268,
162, 15, 115, 26, 39, 35, 243, 190, 49, 50, 9, 28, 49, 15, NA,
NA, NA, 28, 125, 67, 13, 393, 12, 199, 11, 76, 20, 8, 25, 10,
5, 11, 9, 25, 18, 48, 11, 38, 26, 3, 4, 69, 17, 92, 10, 29, 6,
32, 13, 8, 2, 104, 115, 25, 41, 207, 10, 9, 20, 13, 141, 4, 43,
17, 12, 166, 90, 55, 158, 5, 70, 42, 38, NA, 48, 8), totseats = c(435,
435, NA, NA, 435, 435, 435, NA, NA, NA, 435, 435, NA, NA, 435,
435, NA, NA, 435, 435, NA, NA, NA, 435, 435, 230, 230, 230, 230,
230, 435, 435, 200, 200, 200, 200, 200, 200, NA, NA, NA, 245,
245, 245, 245, 640, 640, 640, 150, 150, 150, 150, 150, 150, 51,
51, 51, 51, 148, 148, 148, 148, 148, 148, 148, 202, 202, 202,
100, 100, 100, 100, 100, 100, 100, 556, 556, 556, 556, 556, 52,
52, 52, 52, 147, 147, 74, 74, 74, 544, 544, 544, 544, 544, 544,
80, 80, NA, 194, 194), progtype = 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, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 1, 3, 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,
3, 1, 1, 1, 1, 1, 3), datasetorigin = c(30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 41, 41, 41, 41, 41, 30, 30, 10, 10, 10, 10, 10, 10,
30, 30, 30, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 110,
10, 10, 10, 10, 10, 110, 10, 10, 10, 10, 10, 10, 10, 10, 10,
30, 30, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 30, 10, 10
), corpusversion = c("", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "2020-1", "", "", "",
"", "", "2020-1", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", ""), total = c(242,
274, 26, 61, 272, 262, 139, 24, 54, 130, 275, 330, 58, 83, 93,
390, 15, 68, 147, 193, 25, 129, 46, 258, 194, 52, 90, 63, 57,
21, 109, 249, 205, 38, 139, 68, 91, 168, 144, 118, 34, 432, 75,
308, 93, 567, 250, 326, 156, 501, 153, 47, 183, 94, 49, 107,
118, 117, 128, 48, 64, 61, 133, 87, 136, 119, 52, 210, 125, 81,
72, 151, 67, 64, 50, 84, 264, 126, 32, 48, 128, 198, 173, 52,
141, 60, 277, 320, 74, 164, 464, 187, 287, 116, 365, 115, 734,
120, 617, 112), rile = c(-9.917, 13.869, 22.8, 4, -8.824, -4.962,
-28.058, -33.4, 3.9, -15.5, -13.091, -4.848, 26.7, -13.3, -1.075,
-7.179, -45.6, 4.6, -15.646, 1.036, 0, -13.3, 11.1, -15.891,
14.948, 9.6, -37.8, 9.5, 28, 23.81, -10.092, -0.803, -42.927,
78.947, 21.583, 4.412, 3.297, 4.167, 15.2, -25.7, -64.7, -36.1,
7.1, -8.8, -20.4, -31.3, -6.8, 15.2, -0.5, -32.8, -14.3, 12.6,
15.7, 33, -42.857, -43.925, -2.542, 29.06, -21.2, -23, -15.7,
16.4, -0.2, 6.8, -13.971, -13.5, 15.3, 30.1, -34.4, -19.8, 19.5,
3.9, 21, 22.1, 56, -22.619, -31.818, -5.556, -9.375, 29.167,
-1.7, -36.4, -9.4, 57.8, -18.44, -10, -16.4, 12.5, 41.9, 18.902,
-14.44, 37.433, 48.432, 25, 25.479, -32.174, -3.542, -15.1, 10.049,
-5.357), planeco = c(7.438, 1.825, 0, 0, 3.309, 2.29, 13.669,
0, 0, 0, 6.909, 3.636, 2, 5.7, 12.903, 4.103, 12.9, 0, 11.565,
4.663, 0, 7.1, 0, 8.915, 4.639, 1.9, 3.3, 3.2, 1.8, 0, 3.67,
3.614, 5.854, 0, 5.755, 5.882, 3.297, 4.167, 0, 7, 0, 7, 2.8,
2.7, 7.9, 13.9, 8.8, 4.8, 5.1, 14.4, 4.6, 2.1, 2.1, 0, 12.245,
11.215, 2.542, 0.855, 2.4, 12.5, 6.3, 9.8, 1.6, 4.6, 7.353, 5.9,
0, 8.5, 3.2, 4.9, 4.2, 3.4, 1.5, 1.6, 0, 3.571, 1.894, 0, 12.5,
0, 4, 11.1, 2.9, 3.8, 6.383, 1.667, 6.2, 0.9, 0, 3.049, 7.543,
2.139, 0, 1.724, 2.466, 15.652, 6.676, 10.1, 6.159, 9.821), markeco = c(10.331,
9.124, 0, 0, 2.574, 12.595, 2.158, 0, 0, 0, 9.818, 9.091, 0,
0, 9.677, 7.949, 0, 0, 11.565, 11.917, 0, 2, 0, 2.713, 15.464,
1.9, 2.2, 6.4, 22.8, 19.048, 5.505, 6.024, 0, 0, 8.633, 1.471,
9.89, 7.738, 0, 2, 0, 0.7, 9.9, 4, 8, 1.7, 10, 13.4, 0, 0.6,
1.9, 4.2, 11.4, 1.1, 0, 0, 6.78, 5.128, 0, 0, 1.6, 9.8, 2.3,
3.4, 3.676, 1.6, 15.3, 10, 2.4, 2.5, 12.5, 4.7, 7.5, 1.6, 0,
0, 0.758, 0, 15.625, 0, 0.8, 1, 2.9, 3.9, 0.709, 8.333, 1.1,
17.2, 28.4, 0, 1.94, 8.556, 0, 1.724, 9.863, 3.478, 7.357, 0.8,
0.324, 7.143), welfare = c(2.066, 5.839, 4, 0, 8.088, 1.908,
9.353, 33.3, 18.7, 31.7, 6.909, 2.121, 4.9, 19, 3.226, 5.897,
26.7, 15.3, 10.884, 6.218, 4, 16.2, 2, 10.078, 3.093, 0, 33.4,
14.3, 10.6, 0, 6.422, 4.418, 0.976, 0, 15.827, 4.412, 5.495,
14.881, 7.1, 27.7, 58.8, 24.8, 9.9, 18.5, 22.8, 14.3, 9.2, 5.5,
0.6, 14.8, 13.1, 19.2, 9.9, 9.6, 8.163, 16.822, 28.814, 8.547,
15.6, 10.4, 12.5, 1.6, 0.8, 3.4, 12.5, 5.9, 5.8, 10, 14.4, 9.9,
15.3, 15.2, 12, 9.6, 10, 8.333, 7.955, 1.587, 3.125, 8.333, 10.2,
14.2, 2.3, 0, 12.057, 6.667, 14.2, 2.8, 0, 10.366, 9.698, 3.209,
1.394, 0.862, 4.11, 16.522, 9.537, 7.5, 6.969, 15.179), intpeace = c(0.826,
3.65, 7.9, 2, 3.676, 5.725, 4.317, 0, 3.9, 1, 3.636, 3.03, 2,
0, 4.301, 4.103, 0, 1.2, 2.721, 1.554, 0, 4, 2, 1.55, 4.639,
1.9, 5.6, 1.6, 0, 4.762, 8.257, 2.811, 13.659, 0, 0, 5.882, 0,
0.595, 0, 1, 0, 0, 0, 0.3, 5.7, 5.5, 4, 0.9, 3.2, 0.4, 0, 0,
0.5, 0, 16.327, 3.738, 2.542, 6.838, 1.6, 4.2, 4.7, 1.6, 2.3,
3.5, 8.088, 1.7, 3.8, 0, 4, 0, 0, 0.7, 0, 0, 2, 3.571, 3.03,
0, 6.25, 0, 3.9, 2, 3.5, 0, 1.418, 1.667, 3.7, 0.3, 0, 3.659,
3.448, 2.674, 0, 1.724, 0.548, 0, 0.136, 0, 0.486, 0), datasetversion = c("2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a", "2020a", "2020a", "2020a", "2020a", "2020a", "2020a",
"2020a"), id_perm = c("R2XX9S", "2D829W", "TSAEYI", "T4LHGH",
"1YK8PA", "HO9FS1", "WFHDPR", "E7SUA1", "JOLKMR", "VZQDDH", "E5Z7JT",
"Y6BLD9", "K4RKCF", "PZB7CH", "FQQ88F", "1N56G3", "QMGCFN", "CR6LFI",
"XGWAUX", "SNXNNE", "YORPKV", "O6IQEJ", "Q9TAO9", "1PBVG5", "PF3F5D",
"JN1LZH", "CMR7F6", "Z6OL6C", "YMKVN2", "U4SCRD", "1BDCF8", "AGY4FF",
"Z8T6BR", "C1HESX", "PP1FLT", "VRQUU4", "BYBDN4", "TSWP7S", "87A16O",
"M5KFE1", "RW9KFZ", "2V3SRK", "LHZGG9", "22FV9F", "RA5LSZ", "4GDSR7",
"GSWOU2", "PM7TNC", "DYTZIJ", "VRYPF4", "45R9EV", "X3JG4S", "H9QEF1",
"FJ5DYQ", "9V96FY", "JDKL6J", "9DP64U", "8NB5C3", "VONCDV", "HJW2XP",
"ZCMMSZ", "K2B256", "G7GB47", "4A9XMW", "1QS6AT", "AOG6ZL", "AEY94Q",
"F5PMRC", "DCQMXK", "H2U7BY", "AOP8QA", "VSFCG5", "PJNKSC", "WPEI98",
"5IZK7H", "G9N2I8", "UBTI1E", "NUPH65", "PUN61D", "EYTEP7", "9STC2J",
"GNUP8H", "B5JVLZ", "NDPIGY", "HBDI2K", "OQ4HYY", "GZWEAU", "8I1FL1",
"3POAKF", "KTJQKG", "WSKKA7", "AXULXT", "VNYVIG", "DQFNM3", "CSKYBU",
"OHUKIF", "HK132K", "XXUREL", "4ILMPJ", "TNUUY7"), RRP = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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), pre.post = c(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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), treat = c(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,
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), first.treat = c(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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
-100L), groups = structure(list(party = c(11220, 11320, 11420,
11620, 11810, 12220, 12320, 12420, 12520, 12620, 12810, 13220,
13320, 13410, 13420, 13620, 13952, 13953, 14221, 14320, 14420,
14620, 14810, 14901, 15220, 15320, 15620, 15810, 21320, 21420,
21520, 22210, 22320, 22420, 22522, 22523, 22525, 22952, 23220,
23320, 23420, 23520, 31220, 31320, 31421, 31521, 31621, 31622,
32220, 32320, 32410, 32420, 32520, 43320, 43321, 51320, 51420,
51620, 52320, 52620, 52710, 61320, 61620, 61621, 62320, 62420,
62620, 62951, 63320, 63620, 63810, 64320, 64620, 73330, 171301,
171601), .rows = structure(list(26L, 27L, 28L, 29L, 30L, 49L,
50L, 51L, 52L, 53L, 54L, 59L, 60L, 61L, 62L, 63L, 64L, 65L,
33L, 34L, 35L, 36L, 37L, 38L, 81L, 82L, 83L, 84L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 55L, 56L, 57L, 58L,
90L, 91L, 92L, 93L, 94L, 95L, 76L, 77L, 78L, 79L, 80L, 99L,
100L, 46L, 47L, 48L, c(8L, 17L, 21L, 39L), c(3L, 9L, 13L,
18L, 22L, 40L), c(4L, 10L, 14L, 23L, 41L), c(1L, 5L, 11L,
15L, 19L, 24L, 31L), c(2L, 6L, 12L, 16L, 20L, 25L, 32L),
7L, 42L, 43L, 44L, 45L, 87L, 88L, 89L, 96L, 97L, 98L, 85L,
86L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), row.names = c(NA, 76L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
But I am not even able to get the first steps well.
Thanks!
We can write a small function which returns the year where treat values is 1 for the first time.
library(dplyr)
get_first_treat <- function(x, y) {
inds = match(1, x)
if(is.na(inds)) 0 else y[inds]
}
and apply this function for each party :
df %>%
group_by(party) %>%
mutate(first.treat1 = get_first_treat(treat, edate))
# party edate RRP absseats country treat first.treat
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 A 1991 1 0 11 0 1998
# 2 B 1991 0 20 11 0 1998
# 3 C 1991 0 30 11 0 1998
# 4 A 1995 1 5 11 0 1998
# 5 B 1995 0 25 11 0 1998
# 6 C 1995 0 20 11 0 1998
# 7 A 1998 1 0 11 1 1998
# 8 B 1998 0 10 11 1 1998
# 9 C 1998 0 28 11 1 1998
#10 D 1998 0 12 11 1 1998
# … with 24 more rows
I am running a path analysis in lavaan (with ordinal) and would like to use imputed data.
But whether I impute data separately and use runMI or let the original data be imputed as a part of sem.mi command, I get same error:
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?
If I run:
options(expressions = 100000)
the error message changes to: Error: protect(): protection stack overflow
I tried to change
--max-ppsize=500000
but in the command line I can't access rstudio.exe (says: the system cannot find the path specified, - even though I double-checked the path:
C:\Program Files\RStudio\bin\rstudio.exe --max-ppsize=500000)
What can I do to run my analysis with imputed data or to impute it as a part of the path analyses estimation?
Here is my code:
imp <- mice(dat2,m=5,print=F)
imputedData <- NULL
for(i in 1:5) {
imputedData[[i]] <- complete(x=imp, action=i, include=FALSE)
}
model5 <- 'ceadiff ~ mompa + cdpea + momabhx
mompa ~ b1*peadiff + c*momabhx + cdpea + b2*mommhpsi
peadiff ~ a1*momabhx + mommhpsi
cdpea ~ momabhx + mommhpsi
mommhpsi ~ a2*momabhx
peadiff ~~ cdpea
direct := c
indirect1 := a1 * b1
indirect1 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)'
fit5 <- runMI(model5, data = imputedData, fun="sem", ordered = "mompa")
summary(fit5, standardized = TRUE, fit = TRUE, ci = T)
# or:
fit5 <- sem.mi(model5, data = dat2, m=5, ordered = "mompa")
summary(fit5, standardized = TRUE, fit = TRUE, ci = T)
P.S. It prints summary with a warning in this scenario but doesn't print p-values or CIs, so I cannot determine what coefficients are sig.:
fit5 <- sem.mi(model5, data = dat2, m=5, ordered = "mompa")
summary(fit5)
** WARNING ** lavaan (0.5-23.1097) model has NOT been fitted
** WARNING ** Estimates below are simply the starting values
Thank you!
P.S. I don't know how to supply my data sample.
Here is the unimputed data output:
> dput(dat2)
structure(list(id = structure(c(145, 253, 189, 305, 149, 567,
151, 853, 272, 67, 111, 695, 1695, 1301, 2322, 1335, 1490, 580,
209, 1109, 1317, 812, 1459, 2150, 685, 1583, 839, 2156, 1627,
1103, 649, 2294, 1712, 1711, 793, 1425, 1114, 146, 1529, 985,
1889, 1974, 444, 1664, 1569, 859, 1947, 1219, 1427, 1533, 2143,
769, 256, 147, 1393, 1847, 1967, 1651, 1084, 1343, 996, 1765,
1596, 2157, 978, 1448, 915, 1411, 1412, 675, 1876, 53, 400, 2103,
1028, 663, 1090, 360, 2134, 1937, 1061, 1823, 935, 891, 1968,
34, 487, 207, 295, 1118, 1164, 1053, 1511, 777, 1760, 38, 480,
459, 307, 1962, 199, 499, 1375, 782, 1855, 1624, 109, 1481, 483,
536, 972, 1151, 19, 403, 543, 502, 2251, 254, 429, 2118, 1272,
1995, 982, 1748, 1641, 1994, 1718, 510, 494, 273, 602, 549, 293,
1796, 1497, 1197, 1874, 1179, 159, 205, 242, 299, 100, 1200,
579, 870, 1482, 2131, 33, 1319, 148, 1297, 626, 1051, 1948, 1057,
1581, 1349, 1284, 1178, 1178, 1044, 1001, 547, 276, 507, 871,
698, 1006, 1946, 2101, 68, 265, 1186, 1895, 1864, 1884, 1553,
1761, 2171, 168, 30, 1132, 1983, 1897, 1383, 1353, 1697, 1752,
505, 1605, 1144, 1358, 1052, 1645, 1346, 14, 439, 2154, 932,
971, 2104, 1345, 1821, 52, 1642, 1661, 1835, 1232, 2132, 809,
606, 54, 528, 59, 1848, 232, 1750, 2340, 882, 716, 2105, 711,
2109, 2353, 41, 2144, 552, 304, 2404, 1527, 1980, 927, 1586,
1805, 1982, 1181, 2163, 861, 198, 1404, 986, 1404, 238, 2115,
1125), format.spss = "F4.0", display_width = 11L), peadiff = structure(c(4,
7, 2, 2, 3, 4, 5, 5, 2, 6, 2, 6, 4, 3, 4, 5, 2, 3, 2, 1, 1, 3,
3, 3, 3, 5, 6, 3, 2, 2, 2, 4, 2, 2, 3, 5, 2, 4, 6, 2, 2, 3, 2,
1, 7, 7, 2, 5, 6, 4, 4, 4, 2, 9, 3, 4, 6, 7, 3, 3, 4, 3, 7, 5,
7, 4, 1, 1, 6, 14, 6, 2, 4, 3, 6, 4, 6, 7, 8, 5, 3, 4, 5, 1,
5, 4, 4, 9, 6, 3, 4, 3, 6, 6, 3, 1, 2, 2, 5, 4, 4, 1, 1, 3, 3,
3, 3, 7, 5, 4, 3, 4, 3, 4, 3, 4, 4, 4, 6, 3, 1, 1, 6, 4, 6, 9,
2, 3, 3, 7, 4, 1, 2, 9, 2, 3, 6, 1, 5, 3, 8, 4, 0, 4, 4, 6, 2,
4, 2, 7, 6, 8, 5, 3, 10, 3, 1, 4, 6, 6, 6, 5, 4, 5, 3, 7, 3,
4, 8, 4, 7, 4, 15, 4, 0, 2, 5, 3, 3, 3, 5, 7, 4, 7, 5, 2, 3,
2, 8, 5, 2, 5, 4, 5, 2, 4, 3, 3, 5, 4, 4, 3, 5, 2, 4, 3, 2, 1,
6, 2, 8, 2, 6, 3, 0, NA, 6, 3, 4, 2, 9, 3, 4, 4, 2, 12, 5, 4,
0, 2, 2, 5, 2, 1, 3, 3, 4, 3, 2, 4, 7, 9, 5, 4, 6, 8), format.spss = "F8.2", display_width = 10L),
ceadiff = structure(c(5, 4, 2, 1, 2, 2, 3, 4, 3, 4, 0, 2,
2, 1, 4, 2, 6, 4, 2, 2, 2, 3, 4, 2, 6, 4, 4, 4, 5, 3, 2,
4, 4, 3, 1, 7, 3, 6, 8, 2, 3, 2, 2, 1, 4, 5, 0, 4, 2, 3,
4, 4, 1, 5, 3, 1, 4, 3, 5, 2, 0, 4, 0, 5, 4, 2, 4, 3, 2,
7, 7, 0, 5, 0, 4, 5, 2, 4, 4, 3, 2, 4, 2, 2, 3, 4, 4, 3,
1, 3, 4, 6, 8, 2, 2, 5, 2, 6, 6, 2, 4, 0, 2, 4, 2, 2, 2,
5, 2, 2, 7, 6, 3, 6, 4, 8, 2, 2, 5, 1, 1, 1, 2, 1, 3, 3,
4, 3, 5, 8, 2, 1, 4, 3, 1, 3, 5, 5, 2, 4, 4, 5, 1, 1, 8,
6, 1, 4, 12, 5, 7, 8, 3, 6, 5, 6, 3, 5, 4, 3, 3, 4, 6, 4,
2, 6, 2, 3, 4, 2, 7, 4, 7, 4, 3, 0, 3, 0, 2, 2, 1, 3, 5,
1, 4, 2, 1, 2, 7, 4, 4, 4, 8, 6, 2, 6, 1, 1, 5, 3, 0, 5,
8, 4, 8, 3, 0, 3, 4, 5, 5, 2, 6, 0, 6, NA, 4, 4, 1, 3, 12,
2, 0, 4, 0, 5, 4, 3, 2, 1, 1, 5, 5, 6, 3, 1, 2, 1, 4, 2,
8, 6, 3, 0, 1, 3), format.spss = "F8.2", display_width = 10L),
cdpea = structure(c(22, 18, 17, 13, 19, 20, 19, 20, 17, 17,
17, 14, 17, 15, 21, 12, 16, 15, 14, 17, 19, 18, 17, 18, 19,
16, 18, 15, 16, 18, 17, 19, 18, 15, 16, 18, 18, 17, 22, 18,
18, 12, 19, 16, 15, 17, 14, 17, 15, 19, 17, 18, 14, 17, 19,
20, 16, 6, 12, 17, 17, 16, 13, 20, 18, 16, 16, 18, 21, 17,
21, 13, 17, 14, 18, 15, 18, 17, 23, 19, 17, 18, 15, 17, 19,
15, 21, 17, 20, 16, 15, 18, 15, 18, 17, 18, 16, 18, 21, 16,
19, 21, 18, 16, 19, 18, 18, 18, 18, 18, 19, 20, 20, 22, 14,
19, 18, 16, 22, 14, 16, 17, 18, 15, 16, 19, 16, 19, 18, 18,
15, 18, 19, 16, 16, 18, 15, 13, 12, 20, 19, 18, 19, 13, 19,
19, 16, 20, 18, 18, 18, 18, 18, 18, 19, 15, 14, 18, 16, 15,
15, 18, 18, 18, 18, 20, 17, 16, 19, 18, 19, 17, 18, 18, 16,
16, 18, 15, 19, 19, 17, 17, 16, 15, 15, 15, 17, 12, 17, 17,
19, 14, 21, 19, 19, 18, 23, 18, 21, 18, 16, 17, 18, 13, 14,
17, 18, 16, 18, 16, 18, 18, 17, 17, 6, 22, 17, 18, 20, 18,
10, 18, 15, 10, 16, 16, 18, 18, 17, 21, 18, 18, 15, 13, 15,
17, 12, 16, 16, 16, 15, 20, 17, 14, 17, 17), format.spss = "F8.2", display_width = 10L),
mompa = structure(c(0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0,
0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1,
0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0,
1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1,
0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,
1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1,
0, 0, 1, 0, 0), format.spss = "F8.2", display_width = 10L),
momabhx = structure(c(0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1,
1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1,
1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,
1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 0, 1), format.spss = "F8.2", display_width = 10L),
capiabr1 = structure(c(36, 43, NA, NA, 90, 95, 128, 137,
136, 245, 322, 154, 87, 111, 181, 278, 173, 137, 69, 24,
27, 70, 34, 27, 11, 53, 31, 49, 14, 54, 131, 35, 43, 43,
60, 58, 55, 60, 18, 38, 76, 98, 41, 20, 117, 58, 98, 10,
16, 101, 120, 165, 44, 96, 23, 19, 53, 57, 77, 41, 53, 100,
90, 96, 91, 29, 54, 134, 134, 105, 106, NA, 125, 61, 72,
34, 215, 42, NA, 106, 47, 45, 107, 208, 191, NA, 50, 56,
222, 47, 89, 134, 204, 211, 228, NA, 24, 34, 34, 135, 174,
112, 239, 104, 102, 129, 71, 100, 159, 280, 97, 105, NA,
56, 76, 120, 176, 89, 154, 46, 59, 214, 53, 245, 197, 60,
425, 25, 62, 137, 199, 171, 191, 46, 49, 117, 183, 79, 47,
76, NA, 158, 151, 47, 70, 118, 198, 94, 43, 296, 108, 56,
277, 214, 331, NA, 293, 277, 41, 134, 134, 283, 87, 96, 126,
305, 152, 82, 308, 168, 274, NA, 48, 171, 98, 90, 84, 257,
144, 255, NA, 106, 67, 184, 173, 156, 243, 357, 116, 132,
226, 260, 308, 358, 225, 312, 102, 244, 87, 176, 270, 224,
136, 243, NA, 117, 234, 280, 133, 143, 234, 273, NA, 169,
145, 310, 255, 280, 58, 152, 239, 254, 322, 342, 288, NA,
155, 179, 206, 270, 173, 319, 194, 206, 319, 111, 408, 310,
324, 296, 288, 391, 409, 379, 311, 338), format.spss = "F3.0", display_width = 11L),
cbclint = structure(c(51, 55, NA, NA, 65, 57, 46, 58, 53,
56, 75, 65, 33, NA, 65, NA, 51, 65, 34, 60, 45, 29, 43, 37,
65, 49, 56, 64, 53, 51, 39, 43, 64, 61, 74, 29, 60, 53, 45,
43, 45, 49, 47, 47, 66, 57, 73, 41, 56, 37, 65, 45, 53, 60,
53, 33, 43, 51, 53, 45, 47, 59, NA, 47, 79, 68, 56, 66, 70,
47, 63, 61, 61, 56, 33, 53, 56, 43, 51, 55, 51, 73, 56, 88,
56, 59, 30, 54, 82, 50, 63, 51, 58, 37, 67, 58, 51, 52, 40,
72, 63, NA, 43, 56, 60, 48, 66, NA, 55, 47, 61, 56, 55, 51,
55, 40, 64, 40, 66, 76, 45, 63, 53, 47, 51, 70, 80, 40, 53,
51, 43, 54, 64, 53, 64, 58, 56, 60, 55, 40, 40, 49, 48, 41,
47, 56, 60, 53, 55, 49, 55, 33, 67, 58, 41, 46, 67, 63, 64,
73, 73, 60, 49, 40, 51, 45, 53, 49, 65, 54, 58, 51, 68, 45,
41, 53, 60, 55, 61, 66, 69, 66, 67, 70, 66, NA, 56, 58, 61,
67, 73, 47, 74, 65, 62, 72, 59, 60, 73, 64, 48, 56, 53, 81,
65, 65, 65, 65, 59, 56, 70, 68, 63, 64, 74, 60, 75, 58, 63,
43, 72, 69, 59, 71, 71, 64, 66, 63, 46, 66, 66, 66, 53, NA,
73, 68, 65, 68, 62, 57, 68, 69, 74, 65, 78, 47), format.spss = "F8.0", display_width = 10L),
bpsidrr1 = structure(c(NA, 21, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 18, NA, NA, NA, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 8, 9, 10, 10, 10, 11,
11, 11, 9, 11, 8, 11, 9, 10, 12, 11, 13, 10, 8, 11, 10, 13,
12, 14, 9, 10, 13, 11, 11, 10, 13, 13, 13, 12, 10, 11, 13,
10, 13, 16, 12, 15, 10, 12, 13, 13, 11, 14, 15, 13, 13, 14,
13, 14, 13, 18, 13, 14, 14, 14, 15, 16, 17, 16, 14, 15, 14,
14, 15, 14, 20, 16, 16, 13, 17, 16, 15, 14, 16, 18, 17, 17,
19, 14, 17, 16, 16, 17, 16, 14, 14, 15, 17, 18, 17, 14, 14,
18, 17, 19, 16, 16, 17, 18, 15, 19, 16, 21, 18, 17, 19, 15,
20, 18, 19, 16, 18, 23, 15, 18, 20, 19, 12, 12, 21, 16, 17,
17, 20, 20, 19, 19, 22, 20, 19, 22, 14, 19, 19, 23, 19, 20,
19, 19, 20, 20, 23, 18, 19, 25, 20, 23, 20, 21, 22, 21, 21,
24, 22, 24, 22, 22, 18, 23, 24, 22, 22, 24, 21, 23, 21, 20,
21, 23, 23, 25, 24, 22, 23, 26, 23, 26, 26, 23, 26, 26, 23,
25, 24, 22, 27, 25, 24, 27, 23, 25, 25, 26, 23, 27, 30, 28,
29, 27, 31, 34, 32, 31, 34), format.spss = "F2.0", display_width = 11L),
ecbiir1 = structure(c(177, 197, 148, 133, 172, 133, 129,
NA, 159, 67, 141, 167, 111, 190, 174, NA, 137, 93, 99, 136,
54, 36, 36, 75, 126, 97, 68, 205, 110, NA, 109, 47, 93, 200,
183, 42, 73, 132, 82, 91, 154, 157, 82, 124, 207, 84, 188,
76, 104, 73, 185, 108, 140, 183, 52, 48, 100, 110, 109, 56,
88, 69, 189, 82, 210, 159, 68, 144, 119, 81, 190, 180, 199,
206, 72, 153, 151, NA, 115, 111, NA, 161, 118, 159, 127,
124, 136, 174, 232, 48, 161, 54, 74, 53, NA, 112, 148, 135,
137, 159, 75, 74, 36, 101, 142, 83, 132, 99, 141, 117, 117,
134, 105, 134, 147, 54, 206, 170, 69, 134, 64, 55, 129, 79,
110, 173, 159, 113, 163, 139, 111, 103, 93, 86, 179, 144,
167, 118, 124, 118, 91, 166, 66, 127, 54, 177, 108, 125,
115, 142, 130, 156, 152, 51, 132, 76, 155, 185, 148, 132,
146, 147, 134, 50, 158, 143, 142, 98, 111, 150, 138, NA,
221, 150, 167, 145, 146, 63, 201, 195, 192, 183, 168, 162,
170, NA, 87, 119, 171, 136, 66, 183, 162, NA, 168, 153, 151,
109, 147, 214, 156, 147, 148, 117, NA, 140, 124, 165, 175,
106, 198, 141, 183, 208, 201, 139, 171, 170, 165, 116, 226,
102, 157, 182, 161, 169, 208, 144, 140, 139, 128, 174, 158,
231, 168, 181, 211, 176, 159, 180, 110, 188, 151, 206, 205,
67), format.spss = "F3.0", display_width = 11L), mommhpsi = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 35.75, 32.75, 32.75, 32.75, 32.75, 38.5, 38.5,
32.75, 32.75, 32.75, 32.75, 34.25, 36.5, 43, 43, 49, 33,
38, NA, 33.5, 36.5, 36.75, 43.75, NA, 33.75, 50, 35.75, 49.25,
34, 39, 45.25, 50.75, 50, NA, NA, 34.25, 34.25, 34.25, 38.25,
42.75, NA, 34.5, 42.75, 36.25, 43, NA, 34.75, 34.75, 39.5,
39.5, 39, 48, NA, NA, 35, 35, 38.5, 50.5, NA, 41.5, 38.25,
43.5, 44.5, 43, 51.75, 44.5, NA, NA, NA, NA, 35.5, 38.5,
35.5, 38.5, 42.75, 50.25, NA, NA, NA, NA, NA, NA, 35.75,
35.75, 45, 40.5, 46, NA, NA, NA, NA, 47, 45.75, NA, NA, NA,
NA, NA, NA, NA, 47, 39.25, 50.75, 42.25, 42.25, 44.75, 44,
43.75, NA, NA, NA, NA, NA, NA, 45.75, 40.5, 38.25, 42.25,
51.75, NA, NA, NA, NA, NA, 39.75, 43.25, 50.5, 53.5, 54,
NA, 52.75, NA, 37.25, 41.5, 46.5, NA, 55.25, NA, 59.75, 42.25,
44.25, 44.25, 48.25, 47, NA, NA, NA, 46.5, 49.75, 50, 49.25,
56.25, NA, NA, NA, 39.75, 47, 44, 41, 54.75, 55.25, NA, NA,
38.25, 51, 48.75, NA, 43.75, 50.25, NA, NA, 46.25, 57, 59.75,
58.5, 62.5, 62.25, NA, NA, 46.75, 46, 56.25, 55, 55.75, 58.25,
NA, 44.75, 49.5, 46.5, 57.25, 53, 60.5, 63, NA, NA, NA, 56.75,
NA, 60.5, 43.75, 39.75, 59.25, 58.75, 57.5, 56.5, 63, NA,
NA, NA, NA, 55.5, 50, NA, 61.25, 61.5, 61, 62.75, 66.5, 57,
64.75, NA, 59.25, 68.25, 65.25, NA, 68.75, 50)), .Names = c("id",
"peadiff", "ceadiff", "cdpea", "mompa", "momabhx", "capiabr1",
"cbclint", "bpsidrr1", "ecbiir1", "mommhpsi"), row.names = c(NA,
-246L), class = "data.frame")
Your code works correctly. The problem in given by the version of lavaan and semTools that you are using.
Following the suggestions given here by Terrence D. Jorgensen (one of the authors of semTools), start a new session of R and reinstall the two packages as follows:
install.packages("lavaan", repos = "http://www.da.ugent.be", type = "source")
# if necessary: install.packages("devtools")
devtools::install_github("simsem/semTools/semTools")
Now the commands:
fit5 <- runMI(model5, data = imputedData, fun="sem", ordered = "mompa")
summary(fit5, standardized = TRUE, ci = T)
give the following output:
Rubin's (1987) rules were used to pool point and SE estimates across 5 imputed data sets, and to calculate degrees of freedom for each parameter's t test and CI.
lavaan.mi object based on 5 imputed data sets.
See class?lavaan.mi help page for available methods.
Convergence information:
The model converged on 5 imputed data sets
Parameter Estimates:
Information Expected
Information saturated (h1) model
Standard Errors Robust.sem
Regressions:
Estimate Std.Err t df P(>|z|) ci.lower ci.upper Std.lv Std.all
ceadiff ~
mompa 0.473 0.165 2.863 2016.256 0.004 0.149 0.797 0.473 0.223
cdpea 0.137 0.038 3.589 2507.509 0.000 0.062 0.212 0.137 0.157
momabhx -0.251 0.302 -0.831 Inf 0.406 -0.843 0.341 -0.251 -0.059
mompa ~
peadiff (b1) 0.108 0.035 3.091 Inf 0.002 0.039 0.176 0.108 0.245
momabhx (c) 0.548 0.165 3.324 Inf 0.001 0.225 0.871 0.548 0.273
cdpea -0.048 0.031 -1.525 Inf 0.127 -0.109 0.014 -0.048 -0.116
mommhpsi (b2) -0.022 0.009 -2.365 61.332 0.021 -0.040 -0.003 -0.022 -0.192
...
I have casted (reshape2) a dataframe table with the following structure:
Region | school
|Region | school | Year1 | Year2 |
-----------------------------------
|R1 | S1 | Value1| Value2 |
|R1 | S2 | Value3| Value4 |
|R1 | S3 | Value5| Value6 |
|R2 | S4 | Value7| Value8 |
|R2 | S5 | Value9| Value10|
So for each region, there are severeal schools. I would would like to only show the region once, like this:
|Region | school | Year1 | Year2 |
-----------------------------------
|R1 | S1 | Value1| Value2 |
| | S2 | Value3| Value4 |
| | S3 | Value5| Value6 |
|R2 | S4 | Value7| Value8 |
| | S5 | Value9| Value10|
I am pretty sure I have seen this in many xtables - but I can't find and example now. Any ideas? If your are in doubt as to what I am trying to do; In Excel this is option can be found (it is the default i think) under pivottable tools -> design -> Report layout.
Below my casted datafram (dput):
structure(list(region = c("Hovedstaden", "Hovedstaden", "Hovedstaden",
"Hovedstaden", "Hovedstaden", "Midtjylland", "Midtjylland", "Midtjylland",
"Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland",
"Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland", "Midtjylland",
"Midtjylland", "Midtjylland", "Nordjylland", "Nordjylland", "Nordjylland",
"Nordjylland", "Nordjylland", "Sjælland", "Sjælland", "Sjælland",
"Sjælland", "Sjælland", "Sjælland", "Sjælland", "Syddanmark",
"Syddanmark", "Syddanmark", "Syddanmark", "Syddanmark", "Syddanmark",
"Syddanmark"), school = c("Campus Bornholm", "CPH - Uddannelsescenter København Vest",
"Erhvervsskolen Nordsjælland", "Københavns Tekniske Skole",
"TEC Teknisk Erhvervsskole Center", "Den jydske Haandværkerskole",
"Herningsholm Erhvervsskole", "Holstebro Tekniske Skole", "Learnmark Horsens",
"Professionshøjskolen VIA University College", "Skive Tekniske Skole",
"Teknisk Skole Silkeborg", "Tradium", "Tradium, Tekniske erhvervsudd. og Teknisk Gymnasium, HTX",
"Uddannelsescenter Holstebro", "UddannelsesCenter Ringkøbing-Skjern",
"UddannelsesCenter Ringkøbing-Skjern, teknisk skole", "Viden Djurs",
"Vitus Bering Danmark", "AARHUS TECH", "Erhvervsskolerne Aars",
"EUC Nord", "EUC Nordvest", "Nordvestjysk Uddannelsescenter",
"Tech College Aalborg", "CELF - Center for erhv.rettede udd. Lolland-Falster",
"CEUS", "EUC Nordvestsjælland", "EUC Sjælland", "Roskilde Tekniske Skole",
"Roskilde Tekniske Skole - Pulsen", "Selandia - CEU", "EUC Syd",
"EUC Vest", "HANSENBERG", "Odense Tekniske Skole", "Svendborg Erhvervsskole",
"Syddansk Erhvervsskole Odense-Vejle", "Vejle Tekniske Skole"
), `2003` = c(0, 0, 83, 0, 294, 120, 73, 73, 0, 0, 28, 28, 0,
29, 0, 0, 29, 0, 29, 47, 0, 25, 0, 25, 78, 0, 36, 25, 52, 0,
55, 18, 66, 45, 45, 64, 15, 0, 72), `2004` = c(1243, 0, 71, 0,
296, 122, 79, 79, 0, 0, 39, 39, 0, 30, 0, 0, 30, 0, 5, 53, 0,
36, 0, 36, 82, 0, 53, 36, 58, 0, 76, 31, 56, 54, 54, 92, 30,
0, 82), `2005` = c(1397, 0, 80, 0, 348, 132, 123, 123, 0, 0,
44, 0, 0, 37, 0, 0, 37, 0, 37, 61, 0, 65, 65, 0, 143, 0, 56,
40, 61, 0, 57, 26, 76, 53, 53, 119, 34, 0, 102), `2006` = c(1676,
7, 98, 0, 423, 176, 132, 7, 0, 0, 48, 48, 0, 42, 0, 0, 42, 0,
5, 69, 0, 87, 87, 0, 154, 0, 69, 47, 70, 0, 89, 42, 74, 81, 81,
122, 38, 0, 119), `2007` = c(6, 6, 94, 94, 416, 190, 118, 13,
0, 13, 57, 0, 0, 62, 0, 62, 0, 62, 0, 71, 7, 72, 72, 0, 193,
0, 69, 53, 71, 78, 0, 42, 82, 68, 68, 108, 52, 0, 142), `2008` = c(2090,
2090, 79, 79, 345, 158, 113, 9, 0, 6, 42, 0, 0, 31, 0, 0, 0,
31, 0, 62, 423, 76, 76, 0, 141, 53, 0, 31, 43, 90, 0, 26, 84,
62, 62, 0, 28, 193, 0), `2009` = c(1687, 1687, 58, 0, 237, 90,
55, 0, 0, 55, 23, 23, 28, 0, 28, 0, 0, 28, 0, 42, 244, 37, 37,
0, 68, 38, 0, 44, 34, 59, 0, 21, 41, 39, 39, 0, 15, 104, 0),
`2010` = c(1043, 1043, 70, 70, 285, 113, 56, 0, 56, 0, 32,
0, 29, 0, 29, 0, 0, 29, 0, 52, 287, 52, 52, 0, 75, 41, 0,
23, 34, 56, 0, 27, 52, 35, 0, 0, 35, 145, 0), `2011` = c(1223,
0, 66, 0, 279, 130, 73, 0, 73, 0, 22, 0, 23, 0, 23, 0, 0,
0, 0, 53, 0, 44, 44, 0, 90, 51, 0, 35, 32, 65, 0, 28, 35,
45, 0, 0, 31, 141, 0), `2012` = c(0, 0, 72, 0, 349, 130,
93, 0, 93, 0, 46, 0, 26, 0, 9, 0, 0, 0, 0, 56, 0, 56, 0,
0, 101, 41, 0, 38, 28, 76, 0, 34, 44, 47, 0, 0, 32, 186,
0), `2013` = c(0, 0, 56, 0, 286, 142, 79, 0, 79, 0, 25, 0,
27, 0, 27, 0, 0, 0, 0, 65, 0, 56, 0, 0, 112, 41, 0, 19, 39,
72, 0, 36, 44, 49, 0, 0, 19, 143, 0), `2014` = c(0, 0, 18,
0, 49, 38, 26, 0, 26, 0, 7, 0, 7, 0, 7, 0, 0, 0, 0, 7, 0,
8, 0, 0, 20, 9, 0, 18, 10, 9, 0, 10, 11, 15, 0, 0, 7, 36,
0)), .Names = c("region", "school", "2003", "2004", "2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014"), row.names = c(NA, -39L), class = "data.frame")
the function htmlTable ind Gmisc package can provide two-level coloumnlabels and rowlabels (using cgroup/rgroup and n.croup/n.rgroup. See e.g. http://timelyportfolio.blogspot.dk/2013/04/tables-are-like-cockroaches.html and http://gforge.se/2013/02/tables-from-r-into-word/