我试图使用我发现的库dplyr通过因子级别(StationID)循环非线性模型的示例。当我尝试将代码与非线性模型一起使用时,我不断收到错误消息。
示例:
library(dplyr)
set.seed(4)
data<-data.frame(group=rep(c("A","B","C"),each=3),res=c(rnorm(9,sd=1)),
covar=c(rnorm(9,sd=2)))
#make the dataframe a grouped by dataframe by variable group
by_cyl <- group_by(data, group)
#model
models <- by_cyl %>% do(mod = lm(res ~ covar, data = .))
models
#get model results
summarise(models, rsq = summary(mod)$r.squared)
models %>% do(data.frame(coef = coef(.$mod)))
models %>% do(data.frame(
var = names(coef(.$mod)),
coef(summary(.$mod)))
)
我的模特:
数据样本。数据已使用group_by按StationID分组。
dput(sample_d)
structure(list(StationID = structure(c(94L, 315L, 421L, 133L,
51L, 322L, 237L, 368L, 441L, 53L, 128L, 222L, 146L, 249L, 124L,
95L, 176L, 403L, 247L, 383L, 404L, 330L, 99L, 105L, 66L), .Label = c("201501002",
"201501003", "201501004", "201501005", "201501008", "201501009",
"201501010", "201501011", "201501012", "201501013", "201501014",
"201501015", "201501016", "201501017", "201501018", "201501019",
"201501020", "201501023", "201501024", "201501025", "201501026",
"201501027", "201501029", "201501030", "201501031", "201501033",
"201501034", "201501035", "201501036", "201501037", "201501038",
"201501039", "201501040", "201501042", "201501043", "201501044",
"201501045", "201501046", "201501047", "201501048", "201501049",
"201501050", "201501051", "201501052", "201501053", "201501054",
"201501055", "201501056", "201501057", "201501058", "201501059",
"201501060", "201501061", "201501062", "201501063", "201501066",
"201501067", "201501068", "201501069", "201501070", "201501071",
"201501072", "201501073", "201501074", "201501078", "201501079",
"201501080", "201501081", "201501082", "201501083", "201501084",
"201501086", "201501087", "201501088", "201501089", "201501090",
"201501091", "201501092", "201501094", "201501095", "201501098",
"201501099", "201501100", "201501101", "201501103", "201501105",
"201501107", "201501108", "201501110", "201501111", "201501112",
"201501117", "201501118", "201501119", "201501120", "201501122",
"201501123", "201501125", "201501128", "201501129", "201501130",
"201501133", "201501134", "201501137", "201501141", "201501142",
"201501143", "201501144", "201501145", "201501146", "201501147",
"201501150", "201501151", "201501152", "201501153", "201501155",
"201501158", "201501159", "201501160", "201501161", "201501162",
"201501163", "201501166", "201501172", "201501173", "201501174",
"201501175", "201501179", "201501180", "201501181", "201501182",
"201501184", "201501185", "201501186", "201501187", "201501188",
"201501189", "201501190", "201501191", "201501192", "201501196",
"201501197", "201501198", "201501199", "201501200", "201501201",
"201501202", "201501203", "201501206", "201502001", "201502002",
"201502003", "201502004", "201502005", "201502006", "201502011",
"201502013", "201502014", "201502015", "201502016", "201502017",
"201502019", "201502020", "201502021", "201502022", "201502023",
"201502024", "201502025", "201502026", "201502027", "201502028",
"201502029", "201502030", "201502031", "201502032", "201502033",
"201502034", "201502037", "201502038", "201502039", "201502040",
"201502041", "201502043", "201502044", "201502045", "201502046",
"201502047", "201502048", "201502049", "201502050", "201502051",
"201502054", "201502055", "201502057", "201502058", "201502059",
"201502060", "201502061", "201502062", "201502063", "201502064",
"201502065", "201502066", "201502067", "201502068", "201502069",
"201502070", "201502072", "201502073", "201502074", "201502075",
"201502076", "201502077", "201502078", "201502079", "201502080",
"201502081", "201502085", "201502086", "201502087", "201502088",
"201502089", "201502090", "201502091", "201502092", "201502093",
"201502095", "201502096", "201502097", "201502098", "201502099",
"201502100", "201502101", "201502102", "201502103", "201502104",
"201502105", "201502106", "201502107", "201502108", "201502109",
"201502110", "201502111", "201502112", "201502113", "201502114",
"201502115", "201502116", "201502117", "201502118", "201502119",
"201502120", "201502121", "201502122", "201502124", "201502126",
"201502127", "201502128", "201502129", "201502130", "201502131",
"201502132", "201502133", "201502134", "201502135", "201502136",
"201502137", "201502138", "201502142", "201502144", "201502145",
"201502146", "201502149", "201502150", "201502153", "201502154",
"201502155", "201502156", "201502158", "201502159", "201502164",
"201502165", "201502167", "201502175", "201502176", "201502182",
"201502183", "201502184", "201502185", "201502186", "201502187",
"201502188", "201502189", "201502190", "201502191", "201502192",
"201502193", "201502194", "201502195", "201502196", "201502197",
"201502198", "201502199", "201502200", "201502201", "201502202",
"201502203", "201502204", "201503014", "201503015", "201503017",
"201503018", "201503019", "201503020", "201503021", "201503022",
"201503023", "201503024", "201503025", "201503028", "201503029",
"201503031", "201503032", "201503033", "201503034", "201503035",
"201503036", "201503037", "201503038", "201503039", "201503040",
"201503041", "201503042", "201503044", "201503045", "201503046",
"201503047", "201503048", "201503049", "201503050", "201503051",
"201503052", "201503053", "201503054", "201503055", "201503057",
"201503058", "201503060", "201503061", "201503062", "201503066",
"201503067", "201503068", "201503069", "201503070", "201503071",
"201503072", "201503073", "201503074", "201503075", "201503076",
"201503077", "201503078", "201503079", "201503080", "201503081",
"201503082", "201503084", "201503085", "201503086", "201503087",
"201503088", "201503089", "201503090", "201503091", "201503092",
"201503093", "201503094", "201503096", "201503097", "201503098",
"201503099", "201503100", "201503101", "201503102", "201503103",
"201503104", "201503105", "201503106", "201503108", "201503109",
"201503112", "201503113", "201503114", "201503115", "201503116",
"201503117", "201503118", "201503119", "201503120", "201503121",
"201503122", "201503123", "201503125", "201503129", "201503131",
"201503132", "201503135", "201503136", "201503137", "201503138",
"201503142", "201503143", "201503144", "201503145", "201503146",
"201503147", "201503148", "201503149", "201503150", "201503151",
"201503152", "201503153", "201503157", "201503159", "201503164",
"201503165", "201503166", "201503167", "201503168", "201503169",
"201503172", "201503173", "201503174", "201503175", "201503179",
"201503180", "201503182", "201503183", "201503184", "201503186",
"201503187", "201503188", "201503190", "201503191", "201503192",
"201503194", "201503195", "201503196", "201503197", "201503199",
"201503200", "201503201", "201503202", "201503203", "201503204",
"201503205", "201503206", "201503210", "201503211", "201503212",
"201503218", "201503219"), class = "factor"), Length = c(117.5,
42.5, 47.5, 32.5, 57.5, 62.5, 37.5, 102.5, 117.5, 82.5, 122.5,
92.5, 52.5, 127.5, 57.5, 107.5, 102.5, 122.5, 37.5, 22.5, 97.5,
77.5, 72.5, 137.5, 97.5), Number_com = c(27, 0, 0, 0, 0, 0, 2,
2, 2, 1, 10, 3, 0, 12, 0, 17, 5, 48, 0, 0, 1, 1, 0, 4, 2), Number_survey = c(3,
2, 2, 1, 12, 25, 22, 8, 4, 12, 1, 1, 144, 13, 7, 4, 3, 16, 38,
1, 6, 1, 15, 0, 0)), .Names = c("StationID", "Length", "Number_com",
"Number_survey"), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -25L), vars = list(StationID), drop = TRUE, indices = list(
4L, 9L, 24L, 0L, 15L, 22L, 23L, 14L, 10L, 3L, 12L, 16L, 11L,
6L, 18L, 13L, 1L, 5L, 21L, 7L, 19L, 17L, 20L, 2L, 8L), group_sizes = 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), biggest_group_size = 1L, labels = structure(list(
StationID = structure(c(51L, 53L, 66L, 94L, 95L, 99L, 105L,
124L, 128L, 133L, 146L, 176L, 222L, 237L, 247L, 249L, 315L,
322L, 330L, 368L, 383L, 403L, 404L, 421L, 441L), .Label = c("201501002",
"201501003", "201501004", "201501005", "201501008", "201501009",
"201501010", "201501011", "201501012", "201501013", "201501014",
"201501015", "201501016", "201501017", "201501018", "201501019",
"201501020", "201501023", "201501024", "201501025", "201501026",
"201501027", "201501029", "201501030", "201501031", "201501033",
"201501034", "201501035", "201501036", "201501037", "201501038",
"201501039", "201501040", "201501042", "201501043", "201501044",
"201501045", "201501046", "201501047", "201501048", "201501049",
"201501050", "201501051", "201501052", "201501053", "201501054",
"201501055", "201501056", "201501057", "201501058", "201501059",
"201501060", "201501061", "201501062", "201501063", "201501066",
"201501067", "201501068", "201501069", "201501070", "201501071",
"201501072", "201501073", "201501074", "201501078", "201501079",
"201501080", "201501081", "201501082", "201501083", "201501084",
"201501086", "201501087", "201501088", "201501089", "201501090",
"201501091", "201501092", "201501094", "201501095", "201501098",
"201501099", "201501100", "201501101", "201501103", "201501105",
"201501107", "201501108", "201501110", "201501111", "201501112",
"201501117", "201501118", "201501119", "201501120", "201501122",
"201501123", "201501125", "201501128", "201501129", "201501130",
"201501133", "201501134", "201501137", "201501141", "201501142",
"201501143", "201501144", "201501145", "201501146", "201501147",
"201501150", "201501151", "201501152", "201501153", "201501155",
"201501158", "201501159", "201501160", "201501161", "201501162",
"201501163", "201501166", "201501172", "201501173", "201501174",
"201501175", "201501179", "201501180", "201501181", "201501182",
"201501184", "201501185", "201501186", "201501187", "201501188",
"201501189", "201501190", "201501191", "201501192", "201501196",
"201501197", "201501198", "201501199", "201501200", "201501201",
"201501202", "201501203", "201501206", "201502001", "201502002",
"201502003", "201502004", "201502005", "201502006", "201502011",
"201502013", "201502014", "201502015", "201502016", "201502017",
"201502019", "201502020", "201502021", "201502022", "201502023",
"201502024", "201502025", "201502026", "201502027", "201502028",
"201502029", "201502030", "201502031", "201502032", "201502033",
"201502034", "201502037", "201502038", "201502039", "201502040",
"201502041", "201502043", "201502044", "201502045", "201502046",
"201502047", "201502048", "201502049", "201502050", "201502051",
"201502054", "201502055", "201502057", "201502058", "201502059",
"201502060", "201502061", "201502062", "201502063", "201502064",
"201502065", "201502066", "201502067", "201502068", "201502069",
"201502070", "201502072", "201502073", "201502074", "201502075",
"201502076", "201502077", "201502078", "201502079", "201502080",
"201502081", "201502085", "201502086", "201502087", "201502088",
"201502089", "201502090", "201502091", "201502092", "201502093",
"201502095", "201502096", "201502097", "201502098", "201502099",
"201502100", "201502101", "201502102", "201502103", "201502104",
"201502105", "201502106", "201502107", "201502108", "201502109",
"201502110", "201502111", "201502112", "201502113", "201502114",
"201502115", "201502116", "201502117", "201502118", "201502119",
"201502120", "201502121", "201502122", "201502124", "201502126",
"201502127", "201502128", "201502129", "201502130", "201502131",
"201502132", "201502133", "201502134", "201502135", "201502136",
"201502137", "201502138", "201502142", "201502144", "201502145",
"201502146", "201502149", "201502150", "201502153", "201502154",
"201502155", "201502156", "201502158", "201502159", "201502164",
"201502165", "201502167", "201502175", "201502176", "201502182",
"201502183", "201502184", "201502185", "201502186", "201502187",
"201502188", "201502189", "201502190", "201502191", "201502192",
"201502193", "201502194", "201502195", "201502196", "201502197",
"201502198", "201502199", "201502200", "201502201", "201502202",
"201502203", "201502204", "201503014", "201503015", "201503017",
"201503018", "201503019", "201503020", "201503021", "201503022",
"201503023", "201503024", "201503025", "201503028", "201503029",
"201503031", "201503032", "201503033", "201503034", "201503035",
"201503036", "201503037", "201503038", "201503039", "201503040",
"201503041", "201503042", "201503044", "201503045", "201503046",
"201503047", "201503048", "201503049", "201503050", "201503051",
"201503052", "201503053", "201503054", "201503055", "201503057",
"201503058", "201503060", "201503061", "201503062", "201503066",
"201503067", "201503068", "201503069", "201503070", "201503071",
"201503072", "201503073", "201503074", "201503075", "201503076",
"201503077", "201503078", "201503079", "201503080", "201503081",
"201503082", "201503084", "201503085", "201503086", "201503087",
"201503088", "201503089", "201503090", "201503091", "201503092",
"201503093", "201503094", "201503096", "201503097", "201503098",
"201503099", "201503100", "201503101", "201503102", "201503103",
"201503104", "201503105", "201503106", "201503108", "201503109",
"201503112", "201503113", "201503114", "201503115", "201503116",
"201503117", "201503118", "201503119", "201503120", "201503121",
"201503122", "201503123", "201503125", "201503129", "201503131",
"201503132", "201503135", "201503136", "201503137", "201503138",
"201503142", "201503143", "201503144", "201503145", "201503146",
"201503147", "201503148", "201503149", "201503150", "201503151",
"201503152", "201503153", "201503157", "201503159", "201503164",
"201503165", "201503166", "201503167", "201503168", "201503169",
"201503172", "201503173", "201503174", "201503175", "201503179",
"201503180", "201503182", "201503183", "201503184", "201503186",
"201503187", "201503188", "201503190", "201503191", "201503192",
"201503194", "201503195", "201503196", "201503197", "201503199",
"201503200", "201503201", "201503202", "201503203", "201503204",
"201503205", "201503206", "201503210", "201503211", "201503212",
"201503218", "201503219"), class = "factor")), class = "data.frame", row.names = c(NA,
-25L), .Names = "StationID", vars = list(StationID)))
Arthur Millar的trawlfunctions代码用于非线性模型
hood3par=function(x,catch) {
lenclass=catch[,2]; nwide=catch[,3]; nfine=catch[,3]
expo=exp(x[1]+x[2]*lenclass)
cselect=x[3]*expo/( (1-x[3]) + expo)
-sum( nwide*log(cselect) + nfine*log(1-cselect) ) }
非线性模型。数据争论留空,因为它是一个。在下面的代码中。
nlm(hood3par,p=x,iterlim=300, data = )
x<-c(-11,0.1,0.65)
#model
models <- sample_d %>% do(mod = nlm(hood3par,p=x,iterlim=300, data = .))
models
f(x,...)中的错误: unused参数(data = list(StationID = 51,Length = 57.5,Number_com = 0,Number_survey = 12))
在我看来,错误消息表明数据未被使用。
R和计算机信息:
sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] datasets utils stats graphics grDevices methods
[7] base
other attached packages:
[1] nlme_3.1-120 plotrix_3.5-12 reshape2_1.4.1
[4] reshape_0.8.5 qpcR_1.4-0 Matrix_1.2-1
[7] robustbase_0.92-5 rgl_0.95.1435 minpack.lm_1.2-0
[10] lmtest_0.9-34 zoo_1.7-12 dplyr_0.4.2
[13] plyr_1.8.3 MASS_7.3-40 RODBC_1.3-12
[16] latticeExtra_0.6-26 RColorBrewer_1.1-2 lattice_0.20-31
loaded via a namespace (and not attached):
[1] Rcpp_0.11.6 magrittr_1.5 R6_2.1.0
[4] stringr_1.0.0 tools_3.2.1 parallel_3.2.1
[7] grid_3.2.1 DBI_0.3.1 lazyeval_0.1.10
[10] assertthat_0.1 stringi_0.5-5 DEoptimR_1.0-4
任何帮助将不胜感激。谢谢