New variable based on conditional arithmetic by group - r

I have a data.frame df where I want to create a new variable that is the proportion of another by group. That is for each Species ID Plot Sub paring I'd like to find the proportion of Area by Type. If Type = 0, then PropArea == 1, if Type does not equal 0 (i.e. 1 or 2), then, for example, PropArea = Area (Type 1) / Area (Type 0). An sample data.frame is below. I know how to do this with if statements in excel, but was hoping to find a way to do this within r.
df <- structure(list(Species = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("BFGR", "RNNN"), class = "factor"),
ID = c(201L, 201L, 201L, 201L, 201L, 201L, 219L, 219L, 219L,
219L, 219L, 219L, 220L, 220L), Plot = c(1L, 1L, 2L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), Sub = c(2L, 2L, 2L,
2L, 3L, 3L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L), Type = c(0L,
1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 2L), Area = c(0.78,
0.445, 0.023, 0.015, 0.79, 0.235, 1.29, 1.29, 2.555, 1.065,
1.365, 1.365, 2.678, 1.305), PropArea = c(1, 0.570512821,
1, 0.652173913, 1, 0.297468354, 1, 1, 1, 0.416829746, 1,
1, 1, 0.487303958)), .Names = c("Species", "ID", "Plot",
"Sub", "Type", "Area", "PropArea"), class = "data.frame", row.names = c(NA,
-14L))
## A more complete data set
df_more <- structure(list(Species = structure(c(3L, 3L, 3L, 3L, 3L, 3L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("ACRU", "DIVI",
"LIST", "LITU", "PEPA", "QULA"), class = "factor"), ID = c(205L,
205L, 205L, 205L, 205L, 205L, 219L, 219L, 219L, 219L, 219L, 219L,
219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L,
219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L, 219L,
219L, 219L, 221L, 221L, 222L, 222L, 222L, 222L, 222L, 222L, 222L,
222L, 222L, 222L, 222L, 222L, 222L, 222L, 222L, 222L, 222L, 222L,
222L, 222L, 222L, 222L, 222L, 222L, 227L, 227L, 227L, 227L, 227L,
227L, 227L, 227L, 227L, 227L, 227L, 227L, 228L, 228L, 228L, 228L,
228L, 228L, 228L, 228L, 228L, 228L, 228L, 228L, 228L, 228L, 228L,
228L, 228L, 228L, 228L, 228L, 228L, 228L, 228L, 229L, 229L, 229L,
229L, 229L, 229L, 229L, 229L, 229L, 229L, 229L, 229L, 229L, 229L
), Plot = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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, 1.12, 1.23, 1.23, 0.88, 0.86, 0.86,
0.11, 1.36, 1.36, 1.17, 2.33, 2.33, 1.15, 1.15, 1.23, 1.23, 1.27,
1.27, 0.97, 0.97, 1.39, 1.39, 1.07, 1.07, 1.49, 1.49, 1.33, 1.33,
2.35, 2.35, 1.8, 1.8, 7.5, 7.42, 6.35, 6.82, 0.37, 0.48, 8.67,
8.57, 5.47, 5.66, 2.35, 2.42, 11.99, 12.8, 6.18, 6.19, 2.56,
2.71, 25.77, 25.6, 16.01, 16.56, 3.36, 3.35, 1.08, 0.12, 5.34,
5.34, 6.15, 6.15, 6.93, 6.93, 8.91, 8.91, 10.91, 10.91, 2.31,
1.21, 3.2, 2.42, 2.41, 2.41, 2.32, 2.32, 2.48, 2.48, 0.7, 2.89,
2.89, 1.27, 3.66, 3.66, 0.75, 8, 8, 8.85, 8.85, 11.22, 11.22,
5.08, 2.96, 0.22, 5, 3.01, 0.92, 6.94, 3.88, 4.48, 1.18, 9.03,
4.19, 0.5, 9.97)), .Names = c("Species", "ID", "Plot", "Sub",
"Type", "Area"), row.names = c(NA, 111L), class = "data.frame")

As long as you're OK with your data.frame being resorted, this should work:
library(plyr)
df2 <- ddply(df_more, .(Species, ID, Plot, Sub), function(groupdf) {
denominator <- groupdf[groupdf$Type==0,"Area"]
if(length(denominator) == 0) denominator <- groupdf[groupdf$Type==1,"Area"]
transform(groupdf, PropArea=Area/denominator)
})
And if you want to keep the same ordering, add these lines:
df1 <- df2[match(
interaction(df[c("Species", "ID", "Plot", "Sub", "Type")]),
interaction(df2[c("Species", "ID", "Plot", "Sub", "Type")])),]

If you can guarantee alternation of 0s with 1s and 2s like in your example, you could use ifelse:
df$PropArea <- ifelse(df$Type == 0, 1, df$Area / c(1, df$Area[-nrow(df)]))

There are duplicates in the df_more dataset.
E.g. DIVI/22/1/2/0 is having an area of both 7.50 and 7.42.
This will lead to errors.

Related

Error using 'poly': 'degree' must be less than number of unique points while using 'effects' package

I am trying to use the effects package to create plots of effects in a linear mixed model. I specify the model
fit1 <- lmer(Values ~ stimuli + timeperiod + scale(poly(distance.code,3,raw=FALSE))*habitat + wind.speed + (1|location.code), data=ex.df, REML=FALSE)
For this model I can generate results for analysis as I expect using summary or anova but when I try to look at specific effects:
allEffects(fit1)
#or
plot(allEffects(fit1))
#or
emmeans(fit1, pairwise ~ stimuli)
An error is returned:
Error in poly(distance.code, 3, raw = FALSE) :
'degree' must be less than number of unique points
(with the plot function the error is different but is probably arising from the error with allEffects)
I understand, based on the responses to this question and this question, that "numerical overflow" can be an issue with poly terms. However, I am not clear on what this means or how to overcome the issue.
I also saw in this post and in another post about lme4 that I can no longer find, that I might need to update packages so I have updated 'effects' and 'lme4' in an attempt to remedy this but to no avail.
So if this error is happening because of "numerical overflow" how can I remedy the problem? or if it is not numerical overflow what is happening and how can I work around this?
a subset of my data using dput is:
structure(list(location.code = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 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 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("before",
"during", "after"), class = "factor"), timeperiod = c(6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L), distance.code = c(0L, 30L, 60L,
120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L,
60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L,
30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L,
0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L,
60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L,
30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L,
0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L,
120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 60L, 120L, 0L, 30L,
60L, 120L, 0L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L,
30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L,
60L, 120L, 0L, 30L, 60L, 120L, 0L, 60L, 120L, 0L, 30L, 60L, 120L,
0L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L,
120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L,
0L, 30L, 60L, 120L, 0L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 60L,
120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L,
30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L,
60L, 120L, 0L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 60L, 120L,
0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L,
60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L,
120L, 0L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 60L, 120L, 0L, 30L,
60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L,
0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 120L, 0L,
60L, 120L, 0L, 30L, 60L, 120L, 0L, 60L, 120L, 0L, 30L, 60L),
Values = c(910.721895276374, 922.652711611841, 926.219785713456,
918.776924477918, 1030.28919690464, 1121.98321368732, 992.741416151102,
910.878353926705, 920.201901019659, 922.134996121665, 917.610324052986,
992.059286431433, 1042.05240231832, 1018.99804250179, 911.976009884021,
918.215389274037, 931.037495260958, 913.49701806948, 981.032280455129,
983.700699744073, 989.716307418049, 911.476759038955, 918.554393750162,
920.391856289719, 911.795802370903, 994.583211567691, 1006.58290843226,
1005.52479816571, 908.665064025178, 917.940176257067, 922.746174825048,
921.752449434568, 986.419049170517, 1042.41789735969, 1082.89658057517,
916.02310296116, 918.254868924698, 931.01648294424, 924.221021573334,
982.154409713674, 1008.54477137219, 996.577798511801, 912.914857937818,
916.937508116615, 920.933077377339, 917.443294381608, 997.669828575817,
1007.44452218386, 1151.25894192961, 909.463528658898, 915.293665875472,
921.917039784441, 912.073280663674, 983.866984633392, 1002.04551764872,
986.791628665069, 907.695668282537, 917.845214744473, 932.330755620455,
917.500330773026, 972.609449456089, 1155.55960936774, 1083.40557091613,
909.903267624225, 914.846316952797, 921.279328283221, 914.498616645498,
1000.3672969178, 1021.78461788922, 1011.40975353271, 915.037273600535,
914.099859036178, 924.116937361394, 913.523739017819, 994.428182266452,
1123.09745015276, 1004.1485272116, 914.431649376896, 915.27037594587,
929.411251949862, 910.549315840806, 974.273124973661, 1145.99211507205,
1013.58184367388, 913.467056616881, 920.213007520924, 919.794369158301,
912.333012054637, 983.816025282468, 1103.11322201674, 974.792027063404,
910.532609655114, 917.616832229923, 923.462599912213, 913.432298686233,
1015.24811721269, 1070.61183211249, 1016.57332551186, 910.196695694198,
923.403802532832, 905.400995326023, 934.612035397761, 1036.98011238981,
963.147077473505, 953.792949959199, 916.899569521736, 931.240844862156,
919.11781354823, 938.028220926723, 995.408916523572, 960.825305234446,
993.019295484939, 1026.22960551445, 1000.13773127026, 962.347584090332,
1074.31979099791, 904.090295814044, 908.836747102913, 928.867625382891,
918.100799763641, 906.282906701285, 913.146312873635, 921.224088728859,
977.094140033575, 972.599778534534, 964.658406857446, 1197.35130424458,
921.91272768213, 910.507770576621, 942.269786765654, 922.718235872787,
1014.34022271036, 1128.29327664605, 1043.1365958913, 1238.18704569961,
919.185972424773, 925.486310755197, 908.769520270226, 919.644447501213,
1030.20866627018, 956.104935565803, 955.159231718685, 922.01947330213,
934.451182538208, 928.626906337293, 941.089746683706, 986.326936258622,
1003.40797963907, 1007.57786522109, 1021.91264348048, 995.68658929192,
993.102343807935, 1114.80420865448, 901.633626404701, 908.255562868123,
922.840049924103, 917.012733437446, 907.541530752433, 915.050696506642,
925.95358291661, 983.542956895186, 972.236377246083, 965.082329354352,
1205.36753472358, 918.337944633569, 910.137012141557, 952.89462134025,
923.334999242316, 977.420371016686, 1154.17994731565, 1022.82998099991,
1186.66254220951, 927.061613377597, 926.745527716988, 908.284054932259,
921.213190559531, 966.157586219165, 974.986841619676, 959.421220417498,
916.559494755925, 935.817296050643, 918.835719171662, 912.457217113586,
1023.62078549133, 1009.23121097376, 978.938675917385, 1005.81651905991,
981.715747809821, 953.127134375762, 1088.16577366048, 902.809201411559,
907.462229880533, 921.595454423298, 919.198277947855, 904.969515265664,
913.438353334218, 918.91715550342, 974.889830301362, 970.58615968713,
963.029605541189, 1182.94093491074, 915.889893279581, 908.147726780027,
942.742415528349, 928.20319656241, 979.939535179807, 1153.51966568673,
1020.93502990084, 1154.799618481, 916.246150801212, 936.016759720656,
914.4488779132, 918.823772018551, 962.397352323664, 986.957848140285,
972.131488585193, 985.364195731404, 932.548910038465, 917.363220594089,
919.124801182577, 1085.89850605988, 1031.66330597084, 974.763804119707,
1005.64983154588, 991.988118229379, 975.384741587994, 1064.14809010237,
902.60240793926, 907.989086075871, 923.287310593779, 912.878571722023,
904.107623756648, 905.563259817979, 917.423553921906, 991.530368160932,
975.190212414434, 965.951810135591, 1192.3330908297, 915.334621878897,
910.857441830446, 936.093336975328, 932.960789822422, 972.074491630181,
1106.77459226532, 993.45400883741, 1138.94109332484, 951.911391767329,
927.688604859773, 915.194279622847, 920.98264624041, 971.414103170297,
956.138106650696, 969.385400747507, 965.458656222347, 944.097918792458,
947.157460200658, 915.929397317864, 1029.14870726558, 992.151638322899,
964.680220137879, 954.129642526236, 981.48182339388, 968.10870393618,
1097.48462256339, 906.941701681267, 917.956716926981, 923.05649603805,
934.459432014683, 922.801034508827, 920.724850575215, 935.811146196027,
981.478432929603, 1012.67364507927, 966.471299899978, 1192.4066704659,
912.640460101352, 906.34455384334, 923.738349342148, 916.883929696437,
970.987788560016, 1210.42940542072, 975.753397539076, 1138.97675920151,
911.747488522664, 928.34872697947, 910.852487444859, 916.227875349016,
982.304620375747, 1028.52794775628, 999.236663664046, 913.408967803895,
934.334726415048, 916.354017093653, 918.660674732388, 1036.08727658415,
974.408618327141, 1006.21629092128, 1004.71633485176, 995.142763465394,
987.00017276687), wind.speed = c(0.8, 0.8, 0.8, 0.8, 0.2,
0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8,
0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8,
0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2,
0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8,
0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8,
0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2,
0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2,
0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 65, 65, 65,
65, 65, 65, 65, 55, 55, 55, 55, 50, 50, 50, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.9, 0.9, 0.9,
0.9, 65, 65, 65, 65, 65, 65, 65, 55, 55, 55, 55, 50, 50,
50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.9, 0.9, 0.9, 0.9, 65, 65, 65, 65, 65, 65, 65, 55, 55,
55, 55, 50, 50, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.9, 0.9, 0.9, 0.9, 65, 65, 65, 65, 65,
65, 65, 55, 55, 55, 55, 50, 50, 50, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.9, 0.9, 0.9, 0.9, 65,
65, 65, 65, 65, 65, 65, 55, 55, 55, 55, 50, 50, 50, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.9, 0.9,
0.9, 0.9, 65, 65, 65, 65, 65, 65, 65, 55, 55, 55, 55, 50,
50, 50, 0, 0, 0)), row.names = c(85L, 86L, 87L, 88L, 89L,
90L, 91L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 113L, 114L,
115L, 116L, 117L, 118L, 119L, 127L, 128L, 129L, 130L, 131L, 132L,
133L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 155L, 156L, 157L,
158L, 159L, 160L, 161L, 169L, 170L, 171L, 172L, 173L, 174L, 175L,
183L, 184L, 185L, 186L, 187L, 188L, 189L, 197L, 198L, 199L, 200L,
201L, 202L, 203L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 225L,
226L, 227L, 228L, 229L, 230L, 231L, 239L, 240L, 241L, 242L, 243L,
244L, 245L, 253L, 254L, 255L, 256L, 257L, 258L, 259L, 267L, 268L,
269L, 270L, 271L, 272L, 273L, 615L, 616L, 617L, 618L, 619L, 620L,
621L, 622L, 623L, 624L, 625L, 626L, 627L, 628L, 629L, 630L, 631L,
632L, 640L, 641L, 642L, 643L, 644L, 645L, 646L, 647L, 648L, 649L,
650L, 651L, 652L, 653L, 654L, 655L, 656L, 657L, 658L, 659L, 660L,
661L, 662L, 663L, 664L, 665L, 666L, 667L, 668L, 669L, 670L, 671L,
672L, 673L, 674L, 675L, 676L, 684L, 685L, 686L, 687L, 688L, 689L,
690L, 691L, 692L, 693L, 694L, 695L, 696L, 697L, 698L, 699L, 700L,
701L, 702L, 703L, 704L, 705L, 706L, 707L, 708L, 709L, 710L, 711L,
712L, 713L, 714L, 715L, 716L, 717L, 718L, 719L, 720L, 728L, 729L,
730L, 731L, 732L, 733L, 734L, 735L, 736L, 737L, 738L, 739L, 740L,
741L, 742L, 743L, 744L, 745L, 746L, 747L, 748L, 749L, 750L, 751L,
752L, 753L, 754L, 755L, 756L, 757L, 758L, 759L, 760L, 761L, 762L,
763L, 764L, 772L, 773L, 774L, 775L, 776L, 777L, 778L, 779L, 780L,
781L, 782L, 783L, 784L, 785L, 786L, 787L, 788L, 789L, 790L, 791L,
792L, 793L, 794L, 795L, 796L, 797L, 798L, 799L, 800L, 801L, 802L,
803L, 804L, 805L, 806L, 807L, 808L, 816L, 817L, 818L, 819L, 820L,
821L, 822L, 823L, 824L, 825L, 826L, 827L, 828L, 829L, 830L, 831L,
832L, 833L, 834L, 835L, 836L, 837L, 838L, 839L, 840L, 841L, 842L,
843L, 844L, 845L, 846L, 847L, 848L, 849L, 850L, 851L), class = "data.frame")
> ex.df <- head(ex.df, 100)
> dput(ex.df)
structure(list(location.code = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c("BSF1",
"BSG1", "RLF3", "RLG3", "CCBSF1", "CCBSG1", "CPF1", "CPF2", "CPG1",
"CPG2", "OSG1", "OSG2", "RLF4", "RLF5", "RLF1", "RLF2", "RLG1",
"RLG2", "BNPF1", "BNPG1", "OSG3", "OSF1", "CMG3", "CMF1", "BSG2",
"BSG3", "WSF1", "WSF2", "HPG1", "HPG2"), class = "factor"), stimuli = structure(c(3L,
3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L,
2L, 1L, 1L), .Label = c("FOSP", "BHCO", "COHA", "YEWA", "TUTI"
), class = "factor"), habitat = structure(c(2L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L), .Label = c("Grassland",
"Forest"), class = "factor"), exp.period = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
1L, 1L), .Label = c("before", "during", "after"), class = "factor"),
timeperiod = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
6L, 6L), distance.code = c(0L, 30L, 60L, 120L, 0L, 30L, 60L,
0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L,
30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L,
120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L,
30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L,
60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L,
0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L,
60L, 120L, 0L, 30L, 60L, 0L, 30L, 60L, 120L, 0L, 30L, 60L,
0L, 30L, 60L, 120L, 0L, 30L, 60L, 0L, 30L), Values = c(910.721895276374,
922.652711611841, 926.219785713456, 918.776924477918, 1030.28919690464,
1121.98321368732, 992.741416151102, 910.878353926705, 920.201901019659,
922.134996121665, 917.610324052986, 992.059286431433, 1042.05240231832,
1018.99804250179, 911.976009884021, 918.215389274037, 931.037495260958,
913.49701806948, 981.032280455129, 983.700699744073, 989.716307418049,
911.476759038955, 918.554393750162, 920.391856289719, 911.795802370903,
994.583211567691, 1006.58290843226, 1005.52479816571, 908.665064025178,
917.940176257067, 922.746174825048, 921.752449434568, 986.419049170517,
1042.41789735969, 1082.89658057517, 916.02310296116, 918.254868924698,
931.01648294424, 924.221021573334, 982.154409713674, 1008.54477137219,
996.577798511801, 912.914857937818, 916.937508116615, 920.933077377339,
917.443294381608, 997.669828575817, 1007.44452218386, 1151.25894192961,
909.463528658898, 915.293665875472, 921.917039784441, 912.073280663674,
983.866984633392, 1002.04551764872, 986.791628665069, 907.695668282537,
917.845214744473, 932.330755620455, 917.500330773026, 972.609449456089,
1155.55960936774, 1083.40557091613, 909.903267624225, 914.846316952797,
921.279328283221, 914.498616645498, 1000.3672969178, 1021.78461788922,
1011.40975353271, 915.037273600535, 914.099859036178, 924.116937361394,
913.523739017819, 994.428182266452, 1123.09745015276, 1004.1485272116,
914.431649376896, 915.27037594587, 929.411251949862, 910.549315840806,
974.273124973661, 1145.99211507205, 1013.58184367388, 913.467056616881,
920.213007520924, 919.794369158301, 912.333012054637, 983.816025282468,
1103.11322201674, 974.792027063404, 910.532609655114, 917.616832229923,
923.462599912213, 913.432298686233, 1015.24811721269, 1070.61183211249,
1016.57332551186, 910.196695694198, 923.403802532832), wind.speed = c(0.8,
0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2,
0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8,
0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8,
0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2,
0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2,
0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8,
0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8,
0.8, 0.8, 0.8, 0.2, 0.2, 0.2, 0.8, 0.8, 0.8, 0.8, 0.2, 0.2,
0.2, 65, 65)), row.names = c(85L, 86L, 87L, 88L, 89L, 90L,
91L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 113L, 114L, 115L,
116L, 117L, 118L, 119L, 127L, 128L, 129L, 130L, 131L, 132L, 133L,
141L, 142L, 143L, 144L, 145L, 146L, 147L, 155L, 156L, 157L, 158L,
159L, 160L, 161L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 183L,
184L, 185L, 186L, 187L, 188L, 189L, 197L, 198L, 199L, 200L, 201L,
202L, 203L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 225L, 226L,
227L, 228L, 229L, 230L, 231L, 239L, 240L, 241L, 242L, 243L, 244L,
245L, 253L, 254L, 255L, 256L, 257L, 258L, 259L, 267L, 268L, 269L,
270L, 271L, 272L, 273L, 615L, 616L), class = "data.frame")
Thanks for any help!
EDIT!!
I ran terms(fit1) as suggested in the comments, the results were as follows:
terms(fit1)
Values ~ stimuli + timeperiod + scale(poly(distance.code, 3,
raw = FALSE)) * habitat + wind.speed
attr(,"variables")
list(Values, stimuli, timeperiod, scale(poly(distance.code, 3,
raw = FALSE)), habitat, wind.speed)
attr(,"factors")
stimuli timeperiod scale(poly(distance.code, 3, raw = FALSE)) habitat wind.speed
Values 0 0 0 0 0
stimuli 1 0 0 0 0
timeperiod 0 1 0 0 0
scale(poly(distance.code, 3, raw = FALSE)) 0 0 1 0 0
habitat 0 0 0 1 0
wind.speed 0 0 0 0 1
scale(poly(distance.code, 3, raw = FALSE)):habitat
Values 0
stimuli 0
timeperiod 0
scale(poly(distance.code, 3, raw = FALSE)) 1
habitat 1
wind.speed 0
attr(,"term.labels")
[1] "stimuli" "timeperiod"
[3] "scale(poly(distance.code, 3, raw = FALSE))" "habitat"
[5] "wind.speed" "scale(poly(distance.code, 3, raw = FALSE)):habitat"
attr(,"order")
[1] 1 1 1 1 1 2
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
attr(,"predvars")
list(Values, stimuli, timeperiod, scale(poly(distance.code, 3,
raw = FALSE)), habitat, wind.speed)
Here is a simple parallel example illustrating that wrapping poly() in scale() is the culprit:
> library(emmeans)
> mod1 = lm(mpg ~ am + poly(disp, 3), data = mtcars)
> ref_grid(mod1)
'emmGrid' object with variables:
am = 0, 1
disp = 230.72
> mod2 = lm(mpg ~ am + scale(poly(disp, 3)), data = mtcars)
> ref_grid(mod2)
Error in poly(disp, 3) :
'degree' must be less than number of unique points
Specifically, the call to scale() messes up the predvars attribute in the model's terms component:
> attr(terms(mod1), "predvars")
list(mpg, am, poly(disp, 3, coefs = list(alpha = c(230.721875,
279.549822668452, 298.198735227759), norm2 = c(1, 32, 476184.7946875,
5315202742.2241, 64139299346388.8))))
This provides the coefficients needed to construct the orthogonal polynomial basis; whereas...
> attr(terms(mod2), "predvars")
list(mpg, am, scale(poly(disp, 3)))
That information is excluded.
Note that the scale() call is completely unnecessary anyway, as poly() generates an orthonormal matrix of predictors.

Is there a way to provide n counts for a ggplot bar graph that uses a factor variable with the fill aesthetic?

I am working with a set of data that uses a factor variable that has "Yes" and "No" as levels of response. I've figured out how to create a bar graph based on this data, but I can't seem to get an n/count of each bar to work with the graph.
While the y-axis is "count", it's showing the proportion of yes and no as I intend it to. However, when I try to add a line to label the count, it goes far above the bars at the actual "count" on the y-axis.
The figure above is created with the code:
gun_oppo_plot <- ggplot(data = gun_survey_oppo, aes(x = condition, fill = gun_DV)) +
geom_bar(position = "fill", na.rm = TRUE) + theme_bw()
When I try to add a line such as geom_text(aes(label=..count..),stat="count"), I get the following figure:
Is there a way to get the same counts as in the lower figure, while maintaing the first one's focus on y from (0:1) and having the counts be on the bars themselves?
Thanks in advance for the help!
Data for replication:
structure(list(condition = structure(c(4L, 1L, 5L, 4L, 2L, 4L,
5L, 4L, 3L, 4L, 4L, 3L, 2L, 3L, 3L, 1L, 5L, 3L, 3L, 1L, 2L, 2L,
3L, 1L, 3L, 3L, 4L, 3L, 1L, 5L, 1L, 3L, 2L, 5L, 3L, 4L, 3L, 5L,
4L, 5L, 5L, 4L, 1L, 3L, 1L, 1L, 3L, 5L, 5L, 3L, 5L, 3L, 5L, 5L,
4L, 5L, 5L, 2L, 5L, 1L, 3L, 1L, 2L, 5L, 5L, 1L, 1L, 2L, 4L, 2L,
3L, 5L, 4L, 5L, 4L, 4L, 1L, 1L, 5L, 5L, 3L, 5L, 5L, 3L, 2L, 3L,
2L, 5L, 1L, 4L, 2L, 5L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 5L, 4L, 3L,
1L, 4L, 1L, 2L, 1L, 5L, 3L, 4L, 2L, 4L, 1L, 2L, 1L, 3L, 2L, 4L,
5L, 3L, 1L, 5L, 1L, 2L, 3L, 1L, 4L, 5L, 2L, 5L, 4L, 5L, 4L, 3L,
2L, 4L, 5L, 3L, 1L, 1L, 4L, 5L, 2L, 4L, 1L, 3L, 1L, 5L, 5L, 3L,
1L, 5L, 2L, 2L, 2L, 1L, 5L, 3L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 2L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 1L), .Label = c("No", "Yes"), class = "factor")), class = "data.frame", row.names = c(2L,
4L, 8L, 9L, 12L, 13L, 17L, 21L, 25L, 26L, 28L, 31L, 35L, 37L,
44L, 46L, 47L, 49L, 52L, 53L, 56L, 59L, 66L, 71L, 74L, 76L, 79L,
81L, 83L, 85L, 94L, 97L, 98L, 104L, 109L, 110L, 114L, 116L, 117L,
120L, 122L, 124L, 129L, 130L, 133L, 136L, 138L, 144L, 147L, 152L,
157L, 158L, 162L, 167L, 169L, 171L, 178L, 188L, 195L, 198L, 203L,
206L, 209L, 211L, 213L, 217L, 219L, 222L, 225L, 228L, 230L, 231L,
235L, 237L, 240L, 256L, 257L, 259L, 260L, 262L, 267L, 269L, 271L,
272L, 278L, 279L, 285L, 289L, 294L, 295L, 297L, 299L, 300L, 302L,
304L, 310L, 311L, 314L, 318L, 319L, 321L, 323L, 326L, 328L, 333L,
341L, 342L, 343L, 348L, 357L, 359L, 360L, 363L, 364L, 372L, 375L,
377L, 379L, 387L, 391L, 392L, 394L, 397L, 399L, 401L, 404L, 405L,
411L, 416L, 418L, 421L, 423L, 427L, 428L, 430L, 434L, 438L, 446L,
454L, 456L, 458L, 460L, 463L, 465L, 477L, 479L, 482L, 485L, 490L,
493L, 497L, 500L, 501L, 503L, 507L, 512L, 514L, 516L, 519L, 522L,
525L, 531L, 533L, 539L, 541L, 543L, 552L, 555L, 556L, 559L, 563L,
566L, 569L, 570L, 572L, 574L, 576L, 579L, 581L, 584L, 589L, 590L,
596L, 598L, 599L, 603L, 607L, 609L, 611L, 613L, 618L, 620L, 621L,
624L, 625L, 628L, 629L, 638L, 641L, 644L, 645L, 647L, 651L, 653L,
658L, 663L, 665L, 666L, 675L, 677L, 678L, 680L, 686L, 693L, 697L,
699L, 700L, 704L, 705L, 708L, 709L, 713L, 715L, 717L, 718L, 721L,
724L, 726L, 728L, 735L, 739L, 741L, 748L, 750L, 753L, 756L, 758L,
759L, 762L, 769L, 772L, 780L, 782L, 786L, 788L, 790L, 793L, 796L,
799L, 801L, 804L, 806L, 808L, 809L, 818L, 820L, 823L, 825L, 832L,
835L, 836L, 842L, 844L, 846L, 847L, 855L, 856L, 858L, 860L, 861L,
865L, 867L, 872L, 875L, 876L, 878L, 884L, 887L, 891L, 893L, 896L
))
There might be some way to do this within ggplot itself but here is another way where we "prepare" the data first before plotting.
library(dplyr)
library(ggplot2)
gun_survey_oppo %>%
count(condition, gun_DV) %>%
group_by(condition) %>%
mutate(prop = prop.table(n)) %>%
ggplot(aes(condition, prop, fill = gun_DV, label = n)) +
geom_col(position = "fill", na.rm = TRUE) +
geom_text(position = position_stack(vjust = .5)) +
theme_bw()

How to remove duplicate x-axis labels in R

I am trying to obtain a barplot representing mean percentage of coloration (valores) grouped both by sex and size intervals (class). However, labels in the x-axis appear duplicated. I would like to get one single label ("50-55" for the first and second columns together, "55-60" for the third and fourth columns together, and so on) for each class level. How could I do this?
Here is my code:
par(mar=c(7,4,4,2)+0.1)
class<-factor(coloration$clase.2,levels=c("50-55","55-60","60-65","65-70","70-75","75-80"))
sex<-factor(coloration$sexo,levels=c("M","H"))
valores<-coloration$perc.greenblue
graf<-barplot(tapply(valores,list(sex,class),mean),beside=T,axes=F,ylim=c(0,50),col=c(grey.colors(2)),axisnames=F ,xlab=("Sex and size"),ylab=("% mean coloration"),las=1)
axis(2,at=c(0,5,10,15,20,25,30,35,40,45,50),labels=c(0,5,10,15,20,25,30,35,40,45,50),las=1)
labs<-as.character(class)
text(graf,par("usr")[3]-0.25,srt=0,adj = c(0,2),labels=labs,xpd=T,cex=1)
legend(locator(1),c("Adult males","Adult females"),fill=c(grey.colors(2)),bty="n")
EDIT: here's some reproducible code:
structure(list(edad = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "ADU", class = "factor"),
sexo = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("H", "M"), class = "factor"),
clase.2 = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L,
6L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), .Label = c("50-55",
"55-60", "60-65", "65-70", "70-75", "75-80"), class = "factor"),
perc.greenblue = c(0.09, 0.32, 12.8, 94.32, 34.83, 0.04,
45.83, 12.34, 0.75, 34.82, 0.5, 0.05, 3.46, 0, 1.72, 0.07,
0.09, 0.2)), row.names = c(9L, 10L, 12L, 13L, 48L, 49L, 109L,
110L, 194L, 195L, 263L, 264L, 266L, 267L, 332L, 333L, 408L, 409L
), class = "data.frame")

Plotting lmer model without covariance matrix

I am trying to plot a number of lmer models for a paper. I had to simplify the random effect structure by dropping the correlation between the random slopes and intercept (Barr et al., 2013). However, when I try to plot using the sjp.lmer funtion, I get the following error:
Error in array(NA, c(J, K)) : 'dims' cannot be of length 0
In addition: Warning message:
In ranef.merMod(object, condVar = TRUE) :
conditional variances not currently available via ranef when there are multiple terms per factor
Is there a potential work-around for this? Any help would be greatly appreciated.
Hi Ben,
Here is some of the data I am working with:
> dput(df)
structure(list(Subject = c(1L, 2L, 3L, 5L, 6L, 6L, 6L, 7L, 7L,
7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 11L, 11L, 11L, 12L, 12L,
13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L,
18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 23L,
24L, 24L, 25L, 25L, 25L, 26L, 26L, 26L, 27L, 27L, 28L, 28L, 29L,
29L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L,
67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L,
80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L,
93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L,
105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L,
116L), A = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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.Label = c("1", "2", "3"), class = "factor"), C = c(9.58,
9.75, 15, 10.75, 13.3, 14.42, 15.5, 9.25, 10.33, 11.33, 9.55,
11, 11.92, 14.25, 15.5, 16.42, 14.92, 16.17, 10.83, 11.92, 12.92,
7.5, 8.5, 10.33, 11.25, 13.08, 13.83, 14.92, 15.92, 9.58, 14.83,
11.92, 8.33, 9.5, 10.5, 6.8, 7.92, 9, 13.5, 10.92, 10, 11, 13,
15.58, 12.92, 11.8, 5.75, 6.75, 7.83, 11.12, 12.25, 12.08, 13.08,
14.58, 8.08, 9.17, 10.67, 10.6, 12.67, 7.83, 8.83, 9.67, 10.58,
11.75, 7, 17.17, 11.25, 13.75, 11.83, 16.92, 8.83, 7.07, 7.83,
15.08, 15.83, 16.67, 18.87, 11.92, 12.83, 7.83, 12.33, 10, 11.08,
12.08, 15.67, 11.75, 15, 14.308, 15.9064, 16.161, 16.9578, 8.90197,
16.2897, 9.05805, 10.5969, 5.15334, 9.1046, 14.1019, 18.9736,
10.9447, 14.5455, 16.172, 6.65389, 11.3171, 12.2864, 17.9929,
10.5778, 16.9195, 7.6, 7.8, 7.2, 16.7, 17, 16.5, 17, 15.1, 16,
16.4, 13.8, 13.8, 14.5, 16.1, 15.8, 15, 14.1, 15, 14.7, 15, 14.5,
10.8, 11.4, 11.3, 10.9, 11.2, 9.3, 10.8, 9.7, 8, 8.2, 8.2, 17.5,
12.6, 11.6, 10.8, 11.8, 12.3, 16.3, 17.1, 9.626283368, 14.6,
13.7), D = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"2"), class = "factor"), Frontal_FA = c(0.4186705, 0.4151535,
0.4349945, 0.4003705, 0.403488, 0.407451, 0.3997135, 0.38826,
0.3742275, 0.3851655, 0.3730715, 0.3825115, 0.3698805, 0.395406,
0.39831, 0.4462415, 0.413532, 0.419088, 0.4373975, 0.4633915,
0.4411375, 0.3545255, 0.389322, 0.349402, 0.352029, 0.367792,
0.365298, 0.3790775, 0.379298, 0.36231, 0.3632755, 0.357868,
0.3764865, 0.3726645, 0.351422, 0.3353255, 0.334196, 0.3462365,
0.367369, 0.3745925, 0.3610755, 0.360576, 0.357035, 0.3554905,
0.3745615, 0.38828, 0.3293275, 0.3246945, 0.3555345, 0.375563,
0.38116, 0.387508, 0.357707, 0.413193, 0.3658075, 0.3776355,
0.362678, 0.3824945, 0.3771, 0.375347, 0.362468, 0.367618, 0.3630925,
0.3763995, 0.359458, 0.3982755, 0.3834765, 0.386135, 0.3691575,
0.388099, 0.350435, 0.3629045, 0.3456775, 0.4404815, 0.4554165,
0.425763, 0.4491515, 0.461206, 0.453745, 0.4501255, 0.4451875,
0.4369835, 0.456838, 0.437759, 0.4377635, 0.44434, 0.4436615,
0.437532, 0.4335325, 0.4407995, 0.470447, 0.4458525, 0.440322,
0.4570775, 0.4410335, 0.436045, 0.4721345, 0.4734515, 0.4373905,
0.4139465, 0.440213, 0.440281, 0.425746, 0.454377, 0.4457435,
0.488561, 0.4393565, 0.4610565, 0.3562055, 0.381041, 0.353253,
0.4265975, 0.4069595, 0.40092, 0.4261365, 0.429605, 0.425479,
0.4331755, 0.3981285, 0.4206245, 0.3798475, 0.3704155, 0.395192,
0.404436, 0.4148915, 0.416144, 0.384652, 0.3916045, 0.41005,
0.3940605, 0.3926085, 0.383909, 0.391792, 0.372398, 0.3531025,
0.414441, 0.404335, 0.3682095, 0.359976, 0.376681, 0.4173705,
0.3492685, 0.397057, 0.3940605, 0.398825, 0.3707115, 0.400228,
0.3946595, 0.4278775, 0.384037, 0.43577)), .Names = c("Subject",
"A", "B", "C", "D", "Frontal_FA"), class = "data.frame", row.names = c(NA,
-151L))
Here is the code that I am running
lmer fit
FA <- lmer(Frontal_FA ~ poly(C) + A + B + D + (poly(C)||Subject), data = df)
plot lmer fit
sjp.lmer(FA)
Thanks for your help.
sjp.lmer, by default, plots the random effects of a model. However, it plots random effects (BLUPs) with confidence intervals, using the arm:se.ranef function. This function causes the first error message you get:
arm::se.ranef(FA)
> Error in array(NA, c(J, K)) : 'dims' cannot be of length 0
Then, the se.ranef functions calls the lme4::ranef function with argument condVar = TRUE, which is not yet implemented for specific conditions (like yours) in lme4. Hence you get the additional warning
In ranef.merMod(object, condVar = TRUE) :
conditional variances not currently available via ranef when there are multiple terms per factor
If you are especially interested in plotting the random effects, you could use the lme4-implemented dotplot-function:
lattice::dotplot(ranef(FA))
If you are interested in any other plot type (fixed effects, marginal effects, predictions, ...), see ?sjp.lmer or some examples at his page.
Edit
If you don't mind installing from GitHub (devtools::install_github("sjPlot/devel"), I have committed a small update, so you can use show.ci = FALSE to avoid computing confidence intervals for random effects:
sjp.lmer(FA, type = "re", show.ci = F, sort.est = "(Intercept)")

How can I calculate the slope of multiple subsets of a data frame more efficiently? [duplicate]

This question already has answers here:
Linear Regression and group by in R
(10 answers)
Closed 6 years ago.
I have a data set that contains the optical absorption (a) across a range of wavelengths (wl) for 16 different samples (bod) on 5 different days (day). The dput output for samples (bod) 1 - 3 is below.
I need the slope of the natural log of the absorption by wavelength for each sample on each day.
My current approach has been to do this manually:
# calculate and extract the slope for each sample and date
s275.1.0 <- lm(log(a) ~ wl, data = spec275, subset = bod == 1 & day == "2014-06-10")
s275.1.0.slope <- coef(s275.1.0)["wl"]
s275.2.0 <- lm(log(a) ~ wl, data = spec275, subset = bod == 2 & day == "2014-06-10")
s275.2.0.slope <- coef(s275.1.0)["wl"]
# etc...
# combine slopes into a vector
s275.slopes <- c(s275.1.0, s275.2.0) # etc...
Obviously this is rather tedious. Is there a straightforward way to streamline this code to get R to iterate these calculations over all of the samples and days?
Dput Output
structure(list(bod = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
CPOM = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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L, 3L, 5L, 2L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L), .Label = c("2014-06-10", "2014-06-12",
"2014-06-17", "2014-06-24", "2014-07-01"), class = "factor"),
wl = c(295L, 279L, 283L, 286L, 289L, 292L, 284L, 279L, 282L,
276L, 288L, 291L, 294L, 276L, 275L, 278L, 281L, 284L, 287L,
290L, 293L, 275L, 278L, 281L, 284L, 287L, 290L, 293L, 288L,
281L, 284L, 287L, 277L, 290L, 294L, 282L, 283L, 287L, 289L,
285L, 277L, 275L, 281L, 284L, 287L, 290L, 276L, 279L, 282L,
285L, 288L, 291L, 294L, 293L, 275L, 278L, 281L, 284L, 287L,
290L, 293L, 275L, 278L, 292L, 295L, 288L, 277L, 279L, 282L,
285L, 277L, 280L, 283L, 286L, 289L, 292L, 295L, 284L, 287L,
290L, 293L, 279L, 293L, 275L, 278L, 281L, 277L, 291L, 293L,
276L, 286L, 289L, 295L, 279L, 287L, 275L, 281L, 284L, 292L,
278L, 287L, 280L, 284L, 288L, 276L), abs = c(0.083, 0.105,
0.098, 0.093, 0.089, 0.086, 0.206, 0.221, 0.212, 0.109, 0.196,
0.19, 0.181, 0.436, 0.231, 0.224, 0.415, 0.397, 0.379, 0.361,
0.341, 0.124, 0.12, 0.114, 0.109, 0.105, 0.1, 0.096, 0.111,
0.124, 0.116, 0.113, 0.131, 0.108, 0.1, 0.123, 0.122, 0.115,
0.112, 0.118, 0.131, 0.136, 0.106, 0.1, 0.096, 0.093, 0.131,
0.128, 0.122, 0.117, 0.114, 0.11, 0.106, 0.086, 0.122, 0.118,
0.113, 0.109, 0.105, 0.103, 0.099, 0.114, 0.111, 0.099, 0.095,
0.103, 0.119, 0.116, 0.111, 0.107, 0.056, 0.055, 0.051, 0.048,
0.046, 0.043, 0.043, 0.144, 0.139, 0.135, 0.13, 0.153, 0.099,
0.097, 0.093, 0.088, 0.157, 0.102, 0.076, 0.159, 0.082, 0.078,
0.074, 0.119, 0.108, 0.122, 0.116, 0.11, 0.08, 0.097, 0.086,
0.094, 0.089, 0.084, 0.1), a = c(19.1149, 24.1815, 22.5694,
21.4179, 20.4967, 19.8058, 47.4418, 50.8963, 48.8236, 25.1027,
45.1388, 43.757, 41.6843, 100.4108, 53.1993, 51.5872, 95.5745,
91.4291, 87.2837, 83.1383, 78.5323, 28.5572, 27.636, 26.2542,
25.1027, 24.1815, 23.03, 22.1088, 25.5633, 28.5572, 26.7148,
26.0239, 30.1693, 24.8724, 23.03, 28.3269, 28.0966, 26.4845,
25.7936, 27.1754, 30.1693, 31.3208, 24.4118, 23.03, 22.1088,
21.4179, 30.1693, 29.4784, 28.0966, 26.9451, 26.2542, 25.333,
24.4118, 19.8058, 28.0966, 27.1754, 26.0239, 25.1027, 24.1815,
23.7209, 22.7997, 26.2542, 25.5633, 22.7997, 21.8785, 23.7209,
27.4057, 26.7148, 25.5633, 24.6421, 12.8968, 12.6665, 11.7453,
11.0544, 10.5938, 9.9029, 9.9029, 33.1632, 32.0117, 31.0905,
29.939, 35.2359, 22.7997, 22.3391, 21.4179, 20.2664, 36.1571,
23.4906, 17.5028, 36.6177, 18.8846, 17.9634, 17.0422, 27.4057,
24.8724, 28.0966, 26.7148, 25.333, 18.424, 22.3391, 19.8058,
21.6482, 20.4967, 19.3452, 23.03)), .Names = c("bod", "CPOM",
"Nutrient", "day", "wl", "abs", "a"), row.names = c(151L, 378L,
382L, 385L, 388L, 391L, 406L, 522L, 525L, 618L, 774L, 777L, 780L,
1105L, 1126L, 1129L, 1474L, 1477L, 1480L, 1483L, 1486L, 1656L,
1659L, 1662L, 1665L, 1668L, 1671L, 1674L, 2717L, 2832L, 2835L,
2838L, 2949L, 2963L, 3052L, 3143L, 3150L, 3169L, 3184L, 3194L,
3213L, 3293L, 3724L, 3727L, 3730L, 3733L, 4582L, 4585L, 4588L,
4591L, 4594L, 4597L, 4600L, 4829L, 5003L, 5006L, 5009L, 5012L,
5015L, 5018L, 5021L, 5541L, 5544L, 5807L, 5810L, 6127L, 6185L,
6200L, 6203L, 6206L, 6637L, 7006L, 7009L, 7012L, 7015L, 7018L,
7021L, 7157L, 7524L, 7527L, 7530L, 7881L, 7884L, 7975L, 7978L,
7981L, 8366L, 8369L, 8480L, 8486L, 8594L, 8597L, 8603L, 8844L,
8852L, 8961L, 8967L, 8970L, 9158L, 9268L, 9304L, 9310L, 9314L,
9345L, 9389L), class = "data.frame")
Suppose your dataset is named as "data":
library(dplyr)
library(broom) # to change model results into data frame
results = data %>%
group_by(bod, day) %>%
do(tidy(lm(log(a) ~ wl, data = .))) %>%
filter(term == "wl")
Results will be like this:
> head(results)
Source: local data frame [6 x 7]
Groups: bod, day
bod day term estimate std.error statistic p.value
1 1 2014-06-10 wl -0.01456420 0.0008484573 -17.16550 6.757132e-05
2 1 2014-06-12 wl -0.01285560 0.0002345532 -54.80887 2.476989e-09
3 1 2014-06-17 wl -0.01554979 0.0006418376 -24.22699 2.233117e-06
4 1 2014-06-24 wl -0.01446049 0.0002585413 -55.93107 3.455827e-08
5 1 2014-07-01 wl -0.01478953 0.0004929797 -30.00027 7.718303e-07
6 2 2014-06-10 wl -0.01545840 0.0007932582 -19.48723 6.567167e-06
This is a typical split-apply-combine type operation:
split the data into the relevant chunks, in this case by day then bod
apply a function to each chunk of data; in this case a linear model & extract the slope coefficient,
combine the slope parameters plus the aggregation data into a summary output object, usually a data frame.
There are many ways to do this, especially say using the plyr and more recently the dplyr packages, but I still feel a base R, by-hand version is instructive.
First, split the data up. Here I use split() and spl ends up being a list of data frames, one per combination of day and bod:
spl <- with(spec275, split(spec275, list(day = day, bod = bod)))
Now we want a wrapper function to fit the lm() model and return the slope coefficient. This wrapper does that:
coefLM <- function(x) {
coef(lm(log(a) ~ wl, data = x))[2]
}
We apply, in this case via sapply(), but vapply() would be safer, the wrapper function to each component of spl.
coefs <- sapply(spl, coefLM)
We get back a vector of slopes:
head(coefs)
> head(coefs)
2014-06-10.1.wl 2014-06-12.1.wl 2014-06-17.1.wl 2014-06-24.1.wl 2014-07-01.1.wl
-0.01456420 -0.01285560 -0.01554979 -0.01446049 -0.01478953
2014-06-10.2.wl
-0.01545840
It is then required to put this vector back into an object with the unique combinations of day and bod.
If you want all this together, do:
spl <- with(spec275, split(spec275, list(day = day, bod = bod)))
out <- unique(spec275[, c("day", "bod")])
out <- transform(out, slope = sapply(spl, coefLM))
> out
day bod slope
151 2014-07-01 1 -0.01456420
406 2014-06-12 1 -0.01285560
1105 2014-06-10 1 -0.01554979
1656 2014-06-24 1 -0.01446049
2717 2014-06-17 1 -0.01478953
3143 2014-06-12 2 -0.01545840
3724 2014-06-10 2 -0.01359000
4582 2014-06-17 2 -0.01197854
5003 2014-06-24 2 -0.01157193
5807 2014-07-01 2 -0.01237455
6637 2014-06-10 3 -0.01652268
7157 2014-06-12 3 -0.01177575
7884 2014-06-17 3 -0.01194247
7975 2014-07-01 3 -0.01388849
9158 2014-06-24 3 -0.01365501
library(data.table)
setDT(spec275)[,.(slope=coef(lm(log(a) ~ wl))["wl"]),by=c("bod","day")]
bod day slope
1: 1 2014-07-01 -0.01478953
2: 1 2014-06-12 -0.01285560
3: 1 2014-06-10 -0.01456420
4: 1 2014-06-24 -0.01446049
5: 1 2014-06-17 -0.01554979
6: 2 2014-06-12 -0.01359000
7: 2 2014-06-10 -0.01545840
8: 2 2014-06-17 -0.01197854
9: 2 2014-06-24 -0.01157193
10: 2 2014-07-01 -0.01237455
11: 3 2014-06-10 -0.01652268
12: 3 2014-06-12 -0.01177575
13: 3 2014-06-17 -0.01194247
14: 3 2014-07-01 -0.01365501
15: 3 2014-06-24 -0.01388849
baseR solution:
spec275$group<-with(spec275,interaction(bod,day))
do.call(rbind,lapply(unique(spec275$group),function(i){
data.frame(cbind(model=paste0("model",i),slope=coef(lm(log(a) ~ wl, data = spec275[spec275$group==i,]))["wl"]))}))
model slope
wl model1.2014-07-01 -0.0147895254594997
wl1 model1.2014-06-12 -0.0128555970222881
wl2 model1.2014-06-10 -0.0145641963972831
wl3 model1.2014-06-24 -0.0144604920742437
wl4 model1.2014-06-17 -0.015549793252286
wl5 model2.2014-06-12 -0.0135899954428342
wl6 model2.2014-06-10 -0.0154584020879208
wl7 model2.2014-06-17 -0.0119785366650048
wl8 model2.2014-06-24 -0.0115719324383674
wl9 model2.2014-07-01 -0.0123745534390794
wl10 model3.2014-06-10 -0.0165226797671165
wl11 model3.2014-06-12 -0.0117757454366332
wl12 model3.2014-06-17 -0.0119424723122258
wl13 model3.2014-07-01 -0.013655010998267
wl14 model3.2014-06-24 -0.0138884860775378
Using dplyr:
library(dplyr)
spec275 %>% group_by(bod, day) %>%
do(model = lm(log(a) ~ wl, data = .)) %>%
mutate(coef=coef(model)["wl"])
Gives:
head(results)
Source: local data frame [6 x 4]
bod day model coef
1 1 2014-06-10 <S3:lm> -0.01456420
2 1 2014-06-12 <S3:lm> -0.01285560
3 1 2014-06-17 <S3:lm> -0.01554979
4 1 2014-06-24 <S3:lm> -0.01446049
5 1 2014-07-01 <S3:lm> -0.01478953
6 2 2014-06-10 <S3:lm> -0.01545840
The model column contains the raw lm if you want other parameters.

Resources