预测95%CI图表的值 - 代码错误

时间:2018-03-16 12:05:13

标签: r glm predict

我试图从GLM生成预测值的图表,置信区间为95%。我一直在调整我的主管的一些代码,它只运行一次,所以我可以继续生成下面的图表。但是,从那时起代码就停止了工作。即使在同一项目中,数据框架没有任何变化,它现在只是给出了一条错误信息。 为什么现在这样做,我该如何解决?

#Reordering factor levels for the models
river$SpeciesF<-river$Species
river$SpeciesF<-as.factor(river$SpeciesF)
river$SpeciesF<-relevel(river$SpeciesF,"UP")
View(river)
print(levels(river$SpeciesF))
#Models
massvlength<-glm(river$Mass~river$Length*river$SpeciesF,family=gaussian())
shellvlength<-glm(river$Log.shell~river$Log.length*river$SpeciesF,family=gaussian())
massvshell<-glm(river$Log.mass~river$Log.shell*river$SpeciesF,family=gaussian())

#Predicting values for massvlength model
pframe <- with(river,expand.grid(Length=seq(min(Length),max(Length),length=100), SpeciesF=levels(SpeciesF)))
pd7 <- cbind(pframe, predict(massvlength,newdata=pframe,type="response", se.fit = TRUE)[1:2])
pd7 <- transform(pd7, Fitted = fit, Upper = fit + (1.96 * se.fit), Lower = fit - (1.96 * se.fit))

这曾经工作过一次 - 我知道它有效,因为pd7存在,我用它来绘制图形(下面链接)。我现在需要使用其他两个模型的代码,但不能。它给出了错误:

pd7.2 <- cbind(pframe2, predict(shellvlength,newdata=pframe2,type="response", se.fit = TRUE)[1:2])

data.frame(...,check.names = FALSE)出错:   参数意味着不同的行数:300,95 另外:警告信息: &#39; newdata&#39;有300行,但找到的变量有95行

很抱歉,稍微有些新的,所以不确定我应该如何向帖子添加数据 - 数据:

dput(river)
structure(list(Species = c("AA", "AA", "AA", "AA", "AA", "AA", 
"AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", 
"AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AC", "AC", "AC", 
"AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", 
"AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", 
"AC", "AC", "AC", "AC", "AC", "UP", "UP", "UP", "UP", "UP", "UP", 
"UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", 
"UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", 
"UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", 
"UP"), Mass = c(0.5117, 0.788, 0.722, 0.588, 1.58, 0.73, 0.784, 
0.3176, 0.4838, 0.5254, 0.737, 0.5622, 0.6244, 0.919, 0.961, 
0.91, 0.39, 0.37, 0.8, 0.37, 0.47, 0.51, 0.99, 0.56, 0.82, 1.755, 
2.906, 0.5928, 1.522, 1.793, 0.2909, 1.714, 2.272, 0.924, 2.49, 
0.804, 2.777, 1.788, 1.37, 1.07, 1.52, 1.51, 1.28, 2.59, 1.91, 
1.55, 0.81, 1.8, 1.03, 2.34, 1.37, 1.78, 0.95, 2.59, 1.03, 1.882, 
1.162, 1.294, 1.087, 1.206, 1.399, 1.112, 0.983, 1.511, 0.717, 
0.873, 1.189, 0.91, 1.123, 1.674, 2.365, 2.74, 1.47, 1.64, 0.96, 
1.12, 1.24, 1.49, 1.13, 1.46, 0.84, 1.12, 1.58, 0.96, 1.02, 0.91, 
1.2, 1.4, 1.82, 1.14, 1.43, 0.82, 0.89, 2.07, 2.08), Length = c(55.42, 
59.23, 56.69, 60.56, 74.69, 61.81, 74.47, 46.83, 53.58, 59.18, 
65.86, 60.47, 58.4, 62.44, 59.01, 72.9, 59.61, 54.17, 69.68, 
50.55, 57.37, 53.63, 75.84, 61.39, 64.26, 93.82, 105.79, 70.43, 
92.2, 83.49, 56.36, 106.66, 97.73, 81.09, 100.34, 77.39, 114.36, 
98.63, 93.61, 84.09, 95.75, 91.4, 87.88, 115.41, 109.77, 93.52, 
78.2, 95.7, 79.73, 102.7, 91.37, 101.74, 73.64, 108.02, 87.11, 
80.15, 82.09, 73.66, 65.66, 64.1, 78.47, 63.91, 62.87, 74.88, 
56.63, 55.81, 73.97, 62.68, 69.8, 90.08, 85.91, 97.02, 76.69, 
79.49, 67.22, 69.1, 66.71, 74.88, 68.32, 77.15, 64.54, 67.3, 
77.22, 61.48, 64.14, 70.32, 75.64, 80.19, 92.69, 71.29, 71.86, 
59.36, 64.09, 87.87, 79.53), Width = c(17.36, 18.59, 19.3, 19.51, 
26.31, 20.71, 25.07, 14.1, 16.37, 21.42, 21.97, 17.73, 20.41, 
20.92, 20.51, 23.85, 17.55, 16.67, 23.34, 14.59, 17.64, 18.62, 
24.6, 19.85, 19.87, 27.18, 32.2, 17.99, 24.79, 25.96, 13.53, 
34.76, 28.99, 24.39, 27.77, 20.01, 34.49, 33.42, 26.04, 24.18, 
28, 26.3, 23.94, 33.8, 33.37, 26.47, 21.72, 27.77, 20.77, 30.01, 
27.89, 30.8, 20.45, 32.16, 22.7, 26.17, 24.87, 21.59, 22.16, 
22.66, 26.61, 19.31, 22.15, 23.42, 19.03, 17.75, 23.68, 18.32, 
21.08, 30.52, 26.47, 27.76, 24.63, 25.98, 19.96, 23.2, 21.2, 
23.03, 21.74, 21.82, 18.83, 21.55, 22.35, 18.51, 19.95, 22.31, 
21.76, 25.21, 28.28, 20.94, 22.49, 19.52, 20.34, 29.12, 26.63
), Height = c(34.02, 35.22, 35.94, 35.95, 43.32, 36.68, 41.85, 
29.98, 30.45, 33.08, 37.42, 38.55, 34.3, 37.61, 39.56, 41.06, 
35.13, 32.38, 43.8, 32.74, 35.44, 33.77, 43.52, 35.47, 38.17, 
53.47, 59.19, 40.84, 55.06, 49.6, 32.72, 58.04, 56.39, 45.01, 
55.64, 43.65, 64.48, 53.25, 50.66, 47.47, 53.14, 53.6, 51.86, 
65.52, 58.58, 51.74, 44.37, 49.88, 45.82, 58.78, 48.61, 59.25, 
40.99, 56.79, 48.47, 34.83, 36.78, 33.66, 31.63, 30.76, 34.77, 
28.83, 30.19, 32.78, 28.1, 25.35, 32.75, 29.71, 29.85, 42.24, 
39.9, 43.25, 35.82, 38.47, 30.69, 33.7, 30.38, 32.37, 31.72, 
34.15, 31.27, 30.05, 34.7, 28.39, 29.07, 31.49, 34.01, 34.58, 
40.15, 30.76, 32.74, 28.18, 28.72, 39.87, 37.76), Shell = c(2.56, 
4.21, 2.93, 4.63, 7.16, 4.62, 8.26, 1.7, 2.49, 4.2, 4.85, 3.3, 
3.82, 4.41, 3.41, 6.6, 4.11, 2.74, 8.23, 1.89, 2.87, 3.04, 9.21, 
3.87, 5.38, 8.65, 14.99, 3.76, 9.17, 7.51, 1.79, 17.29, 9.2, 
6.45, 10.97, 5.07, 18.35, 14.16, 8.52, 6.4, 12.65, 7.77, 7.09, 
24.59, 14.55, 7.43, 4.6, 9.88, 4.88, 15.64, 8.75, 13.08, 4.67, 
13.23, 5.82, 18.61, 15.06, 9.99, 9.87, 10.34, 13.46, 7.23, 8.99, 
10.46, 6.15, 5.56, 10.7, 5.73, 8.79, 26.88, 20.04, 17.2, 12.52, 
18.8, 9.28, 11.45, 9.16, 10.12, 11.52, 14.4, 9.92, 7.82, 11.79, 
6.62, 8.7, 11.88, 11.02, 16.5, 22.17, 8.26, 12.4, 7.36, 7.39, 
19.04, 17.3), Log.mass = c(-0.290984583027883, -0.103473782510445, 
-0.141462802430361, -0.230622673923862, 0.198657086954423, -0.136677139879544, 
-0.105683937315562, -0.498119506244941, -0.315334135974139, -0.279509931549949, 
-0.132532512140949, -0.250109158728578, -0.20453710560962, -0.0366844886138888, 
-0.0172766123314546, -0.0409586076789064, -0.408935392973501, 
-0.431798275933005, -0.0969100130080564, -0.431798275933005, 
-0.327902142064283, -0.292429823902064, -0.00436480540245004, 
-0.2518119729938, -0.0861861476162833, 0.244277120801843, 0.463295609962003, 
-0.227091805028728, 0.182414652434554, 0.253580289562183, -0.536256278752941, 
0.234010817587179, 0.356408327038981, -0.0343280287798933, 0.396199347095736, 
-0.0947439512515487, 0.443575879750258, 0.252367514459899, 0.136720567156407, 
0.0293837776852096, 0.181843587944773, 0.17897694729317, 0.107209969647868, 
0.413299764081252, 0.281033367247728, 0.190331698170291, -0.0915149811213503, 
0.255272505103306, 0.0128372247051722, 0.369215857410143, 0.136720567156407, 
0.250420002308894, -0.0222763947111523, 0.413299764081252, 0.0128372247051721, 
0.274619619091238, 0.0652061280543119, 0.111934276332682, 0.0362295440862946, 
0.0813473078041325, 0.145817714491828, 0.0461047872460386, -0.00744648216786434, 
0.179264464339025, -0.1444808443322, -0.0589857562944303, 0.0751818546186916, 
-0.0409586076789063, 0.0503797562614578, 0.223755453657241, 0.37383114507383, 
0.437750562820388, 0.167317334748176, 0.214843848047698, -0.0177287669604316, 
0.0492180226701817, 0.0934216851622351, 0.173186268412274, 0.0530784434834197, 
0.164352855784437, -0.0757207139381184, 0.0492180226701817, 0.198657086954423, 
-0.0177287669604316, 0.00860017176191757, -0.0409586076789064, 
0.0791812460476248, 0.146128035678238, 0.260071387985075, 0.0569048513364726, 
0.155336037465062, -0.0861861476162833, -0.0506099933550873, 
0.315970345456918, 0.318063334962762), Log.shell = c(0.40823996531185, 
0.624282095835668, 0.46686762035411, 0.665580991017953, 0.854913022307856, 
0.664641975556125, 0.916980047320382, 0.230448921378274, 0.396199347095736, 
0.6232492903979, 0.685741738602264, 0.518513939877887, 0.582063362911709, 
0.644438589467838, 0.532754378992498, 0.819543935541869, 0.613841821876069, 
0.437750562820388, 0.91539983521227, 0.276461804173244, 0.457881896733992, 
0.482873583608754, 0.964259630196849, 0.587710965018911, 0.730782275666389, 
0.937016107464814, 1.17580163284828, 0.575187844927661, 0.962369335670021, 
0.875639937004168, 0.252853030979893, 1.23779499327392, 0.963787827345555, 
0.809559714635268, 1.04020662757471, 0.705007959333336, 1.26363606858811, 
1.15106325335375, 0.9304395947667, 0.806179973983887, 1.10209052551184, 
0.890421018800914, 0.850646235183066, 1.39075852873872, 1.16286299332193, 
0.870988813760575, 0.662757831681574, 0.994756944587628, 0.688419822002711, 
1.19423674872383, 0.942008053022313, 1.11660774398825, 0.669316880566112, 
1.1215598441875, 0.764922984649889, 1.26974637313077, 1.17782497186468, 
0.999565488225982, 0.994317152669637, 1.01452053875792, 1.12904505988796, 
0.859138297294531, 0.953759691733229, 1.01953168453126, 0.788875115775417, 
0.745074791582057, 1.02938377768521, 0.75815462196739, 0.943988875073772, 
1.42942926438179, 1.30189771719521, 1.23552844690755, 1.09760432887441, 
1.27415784926368, 0.967547976218862, 1.05880548667591, 0.96189547366785, 
1.00518051250378, 1.06145247908719, 1.15836249209525, 0.996511672154179, 
0.893206753059848, 1.07151380509509, 0.8208579894397, 0.939519252618618, 
1.07481644064517, 1.04218159451577, 1.21748394421391, 1.34576569311449, 
0.916980047320382, 1.09342168516224, 0.866877814337499, 0.868644438394826, 
1.27966694404846, 1.2380461031288), Log.length = c(1.74366652144621, 
1.77254173264094, 1.75350645699097, 1.78218586649202, 1.87326245943873, 
1.79105874359183, 1.87198135384337, 1.67052415778208, 1.72900270927219, 
1.77217496082461, 1.81862172637589, 1.78153996860594, 1.7664128471124, 
1.79546289439038, 1.770925614639, 1.86272752831797, 1.77531912182948, 
1.7337588355872, 1.84310814199961, 1.70372115992702, 1.75868484988244, 
1.72940779696307, 1.87989832433001, 1.7880976333803, 1.8079407212155, 
1.97229542861114, 2.02444461713135, 1.84775768839233, 1.96473092105363, 
1.92163446105371, 1.75097098443732, 2.02800157934686, 1.99002789877029, 
1.90896730041839, 2.00147409669173, 1.8886848466597, 2.05827414668595, 
1.99400903312361, 1.97132224524281, 1.92474435247995, 1.98113878264066, 
1.96094619573383, 1.94389004824847, 2.06224344102648, 2.04048366420627, 
1.97090449815378, 1.89320675305985, 1.98091193777684, 1.90162176409336, 
2.01157044359728, 1.96080362491177, 2.00749173329534, 1.86711377983198, 
2.03350417294517, 1.94006801373935, 1.90390352669016, 1.91429025566595, 
1.86723171451889, 1.81730087839332, 1.80685802951882, 1.89470365260923, 
1.80556881754856, 1.79844346035019, 1.87436583573005, 1.75304656162653, 
1.74671202251666, 1.86905561870191, 1.79712898779655, 1.84385542262316, 
1.95462837750727, 1.93404371903553, 1.98686127029004, 1.88473873776963, 
1.90031249698373, 1.82749850813346, 1.8394780473742, 1.82419094065258, 
1.87436583573005, 1.83454785768095, 1.88733593039917, 1.80982896106789, 
1.82801506422398, 1.88772979728803, 1.78873385882771, 1.80712895559242, 
1.84707886206572, 1.878751520173, 1.9041202134762, 1.9670328821587, 
1.85302861471299, 1.85648721286863, 1.77349389227097, 1.80679027158407, 
1.94384062640126, 1.90053098245276), SpeciesF = 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, 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, 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), .Label = c("UP", 
"AA", "AC"), class = "factor")), .Names = c("Species", "Mass", 
"Length", "Width", "Height", "Shell", "Log.mass", "Log.shell", 
"Log.length", "SpeciesF"), row.names = c(NA, -95L), class = c("tbl_df", 
"tbl", "data.frame"))

2 个答案:

答案 0 :(得分:0)

predict.glm的帮助文件中有一条注释:

  

首先在newdata中查找变量,然后在中查找变量   通常的方式(将包括使用的公式的环境   适合)。如果找到的变量不是,则会发出警告   如果提供的话,长度与newdata中的长度相同。

这意味着您获得的错误是因为您的newdata缺少变量,而predict正在尝试使用长度不同的原始数据中的变量。因此pframe2可能会遗漏您正在使用的模型规范中的变量。

顺便说一句,既然你只是在做一个线性模型,为什么不使用lm并使用predict.lminterval="confidence"为你计算CI?

答案 1 :(得分:0)

我不知道为什么这样可行,但对于其他有类似问题的人 - 只要我改变了模型公式

massvlength&LT; -glm(河流$质谱〜河流$长度*河流$ SpeciesF,家族=高斯())

massvlength&LT; -glm(质量〜长度* SpeciesF,家族=高斯(),数据=河流)

它工作得很完美 - 即使pframe等没有改变。