我有两个问题,显然是相关的:
首先,在Mac上运行时,R中的lsmeans给了我两个不同的输出 与PC相比。
其次,该脚本在PC和Mac上的函数之外工作。 但是,它不适用于Mac上的功能。
我已经和这个项目进行了数周的斗争,而且我最终试图让它发挥作用。
一些背景知识:
实验数据来自全州范围内的研究试验 多年和地点。
该脚本正在构建为自动生成的函数 输出指定的数据子集。
原始数据和功能通过Box Sync共享,并且需要 使用Mac和PC可供多人访问和使用。
我在两台计算机上打开相同的数据和脚本 电脑运行R 3.3。所有套餐已更新至最新版本。
以下是示例数据:
df<-structure(list(YEAR=structure(c(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,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,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,3L,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,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,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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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,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L),.Label="KERN",class="factor"),LAT=c(35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.3695756,35.3695756,35.3695756,35.3695756,
35.3695756,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.394386,35.394386,
35.394386,35.394386,35.394386,35.394386,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156,35.137156,35.137156,
35.137156,35.137156,35.137156,35.137156),LONG=c(-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.3332198,-119.3332198,-119.3332198,-119.3332198,
-119.3332198,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.341943,-119.341943,-119.341943,-119.341943,-119.341943,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647,-119.032647,-119.032647,-119.032647,
-119.032647,-119.032647),PLOT_NUM=c(1L,2L,3L,4L,5L,
6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,16L,17L,18L,
19L,20L,21L,22L,23L,24L,25L,26L,27L,28L,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,134L,136L,104L,92L,135L,27L,4L,71L,122L,91L,
59L,90L,102L,69L,26L,38L,7L,58L,70L,101L,133L,
124L,39L,121L,60L,28L,29L,123L,37L,36L,103L,6L,
5L,42L,75L,56L,107L,22L,24L,106L,88L,40L,118L,
23L,25L,11L,9L,57L,55L,10L,86L,89L,139L,41L,74L,
119L,73L,43L,105L,8L,137L,72L,120L,138L,87L,54L,
115L,85L,52L,109L,83L,141L,77L,47L,79L,82L,117L,
78L,21L,12L,13L,14L,142L,108L,44L,20L,51L,84L,
76L,116L,110L,19L,46L,18L,140L,50L,15L,53L,45L,
281L,200L,291L,198L,192L,232L,196L,301L,289L,279L,
302L,243L,251L,230L,207L,199L,220L,186L,277L,303L,
187L,197L,247L,205L,255L,257L,188L,305L,260L,269L,
278L,245L,299L,222L,218L,204L,308L,288L,290L,307L,
248L,272L,280L,203L,297L,228L,201L,214L,194L,246L,
233L,227L,286L,263L,250L,275L,261L,253L,231L,271L,
221L,287L,270L,224L,190L,234L,226L,276L,216L,256L,
191L,189L,195L,258L,292L,284L,219L,242L,282L,273L,
235L,283L,304L,262L,252L,229L,300L,254L,259L,298L,
306L,274L,206L,249L,202L,217L,225L,285L,215L,223L,
193L,244L),ENTRY_NUM=structure(c(20L,14L,51L,42L,
48L,45L,44L,46L,8L,43L,9L,45L,46L,8L,18L,9L,15L,
52L,51L,42L,50L,46L,49L,47L,19L,43L,50L,36L,12L,
8L,47L,10L,6L,21L,8L,10L,2L,48L,17L,49L,12L,20L,
13L,43L,19L,13L,45L,22L,47L,17L,15L,36L,18L,52L,
6L,48L,46L,15L,18L,22L,13L,7L,50L,18L,19L,7L,
2L,44L,47L,51L,22L,6L,7L,10L,14L,44L,36L,52L,
2L,22L,10L,19L,49L,21L,7L,45L,44L,2L,14L,9L,51L,
15L,12L,20L,21L,42L,52L,17L,48L,50L,36L,14L,6L,
49L,21L,43L,12L,20L,13L,9L,17L,42L,1L,2L,3L,4L,
5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,17L,18L,
19L,20L,21L,22L,24L,28L,29L,30L,36L,37L,38L,39L,
40L,41L,42L,44L,1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,
11L,12L,13L,14L,15L,17L,18L,19L,20L,21L,22L,24L,
28L,29L,30L,36L,37L,38L,39L,40L,41L,42L,44L,1L,
2L,3L,4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,
15L,17L,18L,19L,20L,21L,22L,24L,28L,29L,30L,36L,
37L,38L,39L,40L,41L,42L,44L,1L,1L,1L,2L,2L,2L,
3L,3L,3L,4L,4L,4L,5L,5L,5L,6L,6L,6L,7L,7L,7L,
8L,8L,8L,9L,9L,9L,11L,11L,11L,12L,12L,12L,13L,
13L,13L,14L,14L,14L,15L,15L,15L,16L,16L,16L,17L,
17L,17L,18L,18L,18L,19L,19L,19L,20L,20L,20L,21L,
21L,21L,22L,22L,22L,23L,23L,23L,24L,24L,24L,25L,
25L,25L,26L,26L,26L,27L,27L,27L,28L,28L,28L,29L,
29L,29L,30L,30L,30L,31L,31L,31L,32L,32L,32L,33L,
33L,33L,34L,34L,34L,35L,35L,35L),.Label=c("878",
"951","1166","1210","1211","1215","1375","1429","1431",
"1440","1473","1479","1484","1582","1583","1585","1607",
"1627","1640","1654","1690","1697","1698","1721","1733",
"1756","1758","1770","1771","1776","1782","1783","1784",
"1785","1786","1796","1797","1798","1799","1800","1801",
"1810","1812","1813","1822","1823","1824","1825","1826",
"1827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818,
4689.599352,5987.74792,5666.825975,6071.348724,5478.883003,
7124.783728,5656.242954,3610.181643,4331.324396,4904.546884,
4447.489172,4106.787872,4658.348018,6259.997274,6153.207065,
6313.497876,5676.779979,5357.37221,6466.719177,6230.109145,
6688.341036,6442.7157,6100.712992,4621.929508,5755.886769,
6604.254137,4954.200669,5867.5903,3991.831947,6005.947762,
6650.642431,5068.746236,3389.123183,4665.019064,5061.072647,
4478.621635,3941.029714,4229.627327,5402.926203,4245.353341,
5637.312903,5518.179277,4389.871419,4215.083338,4897.365913,
6086.120238,4502.709961,5024.207751,5723.2024,4795.406733,
5589.303172,4157.52343,4694.93871,5977.432504,4828.809253,
4207.050908,5533.320623,3808.889598,5191.0883,5565.5,
5967.716748,4695.892909,3745.651761,2326.036846,5520.691951,
3616.957981,6062.2518,5539.725094,4599.123634,4539.368454,
6400.747636,3883.129209,6770.653931,6197.140663,5676.23605,
5778.724469,5696.726085,4583.617968,4006.825885,5611.114514,
5077.577925,6096.838084,3900.072863,4227.004389,3784.270665,
4244.82975,5221.064527,6211.31643,5790.782248,6239.067852,
2557.838189,2827.312193,3869.848855,4470.534568,7274.162574,
5137.88216,6012.906525,5530.157816,6173.018569,5228.493966,
5781.589525,5887.075038,5244.684963,5063.663862,4275.956581,
5568.79725,6649.127517,4069.433962,5760,5241.904762,
5195.294118,5482.105263,7206.233766,5491.2,4979.2,6739.328859,
5202.162162,5734.736842,6568.421053,6227.027027,5296.107383,
5222.4,4458.947368,5564.081633,6829.530201,6778.77551,
6973.333333,5667.945205,5942.857143,6044.903226,6349.803922,
6101.917808,7069.090909,5682.162162,5476.510067,5220.392157,
5166.315789,6669.473684,6055.384615,6233.6,4869.933775,
5733.146853,4718.873239,4746.335404,5397.482517,6014.304636,
5681.632653,7018.378378,6003.2,4618.039216,7048.767123,
5409.811321,6632.727273,5952,4966.4,5322.531646,5423.376623,
6662.781457,5823.157895,7210.06993,5216.842105,6063.157895,
7000.816327,5687.088608,6896.326531,6630.4,5734.228188,
6136.470588,4522.95302,5446.27451,5987.368421,5060.662252,
5027.913669,5844.528302,4882.285714,5169.230769,5290.06993,
4463.046358,5570.704225,5106.938776,6088.767123,5435.492958,
4940.8,6170.482759,5368.421053,6048.627451,6006.486486,
4539.428571,5347.555556,6637.350993,6271.168831,5838.367347,
6057.464789,5018.482759,6498.461538,5450.738255,6168.510638,
5106.666667,6404.475524,5357.837838,4850.526316,3458.630137,
4717.350993,5859.74026,5354.366197,7409.079755,6442.105263,
7450,6203.076923,6362.790698,6693.333333,7211.707317,
7481.806452,6858.666667,7572.670807,8274.285714,7886.769231,
6328.888889,6395.761589,6507.789474,6295.272727,6681.156069,
6965.106383,7521.019108,9086.369427,8196.923077,5969.454545,
5376,6107.234043,5934.545455,6514.682081,7135.828877,
5954.43038,5539.459459,5513.142857,5701.818182,6594.206897,
7101.176471,8905.030675,6950.614525,8660.425532,7515.092025,
7316.078431,6471.111111,3473.121019,4295.028902,5422.417582,
6970.181818,7819.441341,8506.490066,5772.715232,5268.837209,
5335.578947,7600.941176,8048.813559,6842.553191,7773.658537,
8278.441558,7326.315789,7333.012048,7232,6662.295082,
8436.363636,7949.662921,7182.222222,9399.215686,8777.142857,
7222.38342,8258.313253,7755.505618,7975.384615,6864,
7437.241379,7251.06383,7655.541401,7718.145695,7926.857143,
7959.493671,8582.013423,8792.275862,5636.923077,6573.774834,
7036.595745,8457.423313,6860.8,8353.507853,7821.349693,
9017.142857,8494.545455,7289.101796,7855.542857,7302.47191,
7917.037037,8035.955056,8543.492063,6984,5975.730337,
7298.931298,8024.615385,7200,7923.380282,7749.818182,
6758.843931,8354.198473,1110.943396,1356.8,1421.837838
),PROTEIN=c(15.03867,14.82126,15.748876,15.437255,
16.589528,14.640085,16.575034,15.582195,15.524219,14.973447,
15.408267,NA,NA,NA,NA,NA,15.002435,16.792444,17.343216,
15.770617,15.734382,15.212598,14.995188,16.219931,15.47349,
15.524219,15.567701,15.879322,15.198104,17.002607,15.661912,
15.03867,16.430094,15.959039,17.162041,14.828507,15.502478,
16.988113,17.633096,NA,NA,NA,NA,NA,NA,16.05325,15.25608,
15.190857,16.915643,17.517144,15.937298,15.567701,16.748962,
16.082238,16.647504,16.99536,15.632924,16.248919,17.335969,
15.306809,16.661998,16.857667,15.988027,17.437427,15.748876,
15.814099,NA,NA,NA,NA,NA,NA,NA,NA,14.379193,16.495317,
15.828593,16.328636,15.285068,15.357538,15.451749,15.785111,
15.321303,16.517058,16.980866,15.379279,16.473576,15.487984,
15.922804,17.379451,16.872161,15.560454,15.83584,15.705394,
NA,NA,NA,NA,NA,NA,NA,NA,16.843173,15.089399,16.212684,
15.937298,15.683653,15.959039,16.234425,15.966286,17.28524,
16.183696,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,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,11.56,NA,
NA,12.07,NA,NA,11.4,NA,NA,11.7,NA,NA,11.3,NA,
NA,12.07,NA,NA,12.5,NA,NA,12.57,NA,NA,12.08,NA,
NA,11.87,NA,NA,12.37,NA,NA,11.26,NA,NA,11.3,NA,
NA,11.32,NA,NA,11.98,NA,NA,12,NA,NA,11.99,NA,
NA,11.53,NA,NA,11.82,NA,NA,12.16,NA,NA,10.82,NA,
NA,10.3,NA,NA,11.24,NA,NA,10.79,NA,NA,11.17,NA,
NA,13.06,NA,NA,11.07,NA,NA,11.65,NA,NA,12,NA,
NA,10.26,NA,NA,11.38,NA,NA,10.61,NA,NA,11.24,NA,
NA,14.12,NA,NA)),.Names=c("YEAR","LOCATION","LAT",
"LONG","PLOT_NUM","ENTRY_NUM","BLOCK","YIELD","PROTEIN"),row.names=c(NA,
-313L),class="data.frame")
数据设置:
df$ENTRY_NUM <- as.factor(df$ENTRY_NUM)
df$YEAR <- as.factor(df$YEAR)
df$LOCATION <- as.factor(df$LOCATION)
df$BLOCK <- as.factor(df$BLOCK)
require(nlme)
require(lsmeans)
生成所有年份数据的lsmeans估算值。这在PC和Mac上都可以在函数内外使用。
m.lme <- lme((PROTEIN) ~ ENTRY_NUM,
data = df, random = ~1|YEAR/LOCATION,
na.action = na.exclude)
org.lsm <- lsmeans(m.lme, "ENTRY_NUM")
YIELD.lsm <<- data.frame(summary(org.lsm))
# A function to do the same thing.
Data_3Yr <- df
Three_yr_fn_Y <- function(Data_3Yr)
{
m.lme <- lme((PROTEIN) ~ ENTRY_NUM,
data = df, random = ~1|YEAR/LOCATION,
na.action = na.exclude)
org.lsm <- lsmeans(m.lme, "ENTRY_NUM")
YIELD.lsm <<- data.frame(summary(org.lsm))
}
ThreeYear_r <- Three_yr_fn_Y(Data_3Yr)
单年是它开始失败的地方。数据按年分组,每年运行以获得一个lsmeans表。这在Mac和PC上运行良好。但是,当每年在Mac上的某个功能中运行时,它会失败。
# Individual years.
Y1 <- subset(df, YEAR == "2014")
Y2 <- subset(df, YEAR == "2015")
Y3 <- subset(df, YEAR == "2016")
# These all work fine for me.
# Single years
Data_1Yr <- Y1
m.lme <- lme((YIELD) ~ ENTRY_NUM,
data = Data_1Yr, random = ~1|LOCATION/BLOCK,
#weights = varPower(form = ~fitted(.)),
na.action = na.exclude)
org.lsm <- lsmeans(m.lme, "ENTRY_NUM")
YIELD.lsm <- data.frame(summary(org.lsm))
Data_1Yr <- Y2
m.lme <- lme((YIELD) ~ ENTRY_NUM,
data = Data_1Yr, random = ~1|LOCATION/BLOCK,
#weights = varPower(form = ~fitted(.)),
na.action = na.exclude)
org.lsm <- lsmeans(m.lme, "ENTRY_NUM")
YIELD.lsm <- data.frame(summary(org.lsm))
Data_1Yr <- Y3
m.lme <- lme((YIELD) ~ ENTRY_NUM,
data = Data_1Yr, random = ~1|LOCATION/BLOCK,
#weights = varPower(form = ~fitted(.)),
na.action = na.exclude)
org.lsm <- lsmeans(m.lme, "ENTRY_NUM")
YIELD.lsm <- data.frame(summary(org.lsm))
# A function to do the same thing.
Single_yr_fn_Y <- function(Data_1Yr)
{
m.lme <- lme((YIELD) ~ ENTRY_NUM,
data = Data_1Yr, random = ~1|LOCATION/BLOCK,
na.action = na.exclude)
org.lsm <- lsmeans(m.lme, "ENTRY_NUM")
YIELD.lsm <- data.frame(summary(org.lsm))
}
# Each year subset run in the function.
Y1_r <- Single_yr_fn_Y(Y1)
Y2_r <- Single_yr_fn_Y(Y2)
Y3_r <- Single_yr_fn_Y(Y3)
在Mac上,会返回以下错误消息。
Error in `contrasts<-`(`*tmp*`, ncol(ca), value = ca) :
wrong number of contrast matrix rows
Called from: `contrasts<-`(`*tmp*`, ncol(ca), value = ca)
在PC上它会返回以下警告,但无论如何都会生成输出:
Warning message:
In qt((1 - level)/adiv, df) : NaNs produced
请注意:Mac不会始终生成错误消息。如果它对同一个数据子集运行多次,有时它会生成与PC相同的消息(!?!)。
最后,在Mac上,调试返回以下消息。我用google搜索了这个,只返回包的GitHub脚本。
stop("wrong number of contrast matrix rows")
如果你能提出解决方案,你将成为我最喜欢的人!
更新
df2<-structure(list(YEAR = 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, 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, 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 5L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L), .Label = c("1723", "1748", "1766", "1807", "1820",
"3178", "3179"), class = "factor"), BLOCK = c(1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L
), PROTEIN = c(NA, NA, 11.17, NA, NA, NA, 11.87, NA, NA,
NA, 7.98, NA, 11.87, NA, NA, NA, 11.6, NA, NA, NA, 11.79,
NA, NA, NA, 12.06, NA, NA, NA, 13.46, NA, NA, NA, 11.34,
NA, NA, NA, 12.41, NA, NA, NA, 13.27, NA, NA, 13.42, NA,
NA, 12.45, NA, NA, 12.97, NA, NA, 13.074733, 12.328292, 13.024004,
12.922546, 13.58927, 13.125462, 14.277735, 12.987769, 12.364527,
12.038412, 12.480479, 13.386354, 13.538541, 13.074733, 13.364613,
13.364613, 13.096474, 12.929793, 13.248661, 13.176191, 10.357108,
10.378849, 10.871645, 11.118043, 13.813927, 14.263241, 15.922804,
NA, 12.712383, 12.734124, 12.400762, NA, 13.582023, 13.176191,
13.212426, NA, 11.973189, 11.792014, 11.799261, NA, 12.422503,
12.060153, 12.154364, NA, 12.581937, 12.748618, 12.480479,
NA, 10.770187, 10.704964, 11.089055, NA, 13.726963, 12.603678,
12.915299, NA)), .Names = c("YEAR", "LOCATION", "ENTRY_NUM",
"BLOCK", "PROTEIN"), row.names = c(6125L, 6126L, 6127L, 6128L,
6165L, 6166L, 6167L, 6168L, 6181L, 6182L, 6183L, 6184L, 6541L,
6542L, 6543L, 6544L, 6581L, 6582L, 6583L, 6584L, 6597L, 6598L,
6599L, 6600L, 3411L, 3472L, 3533L, 3594L, 3417L, 3478L, 3539L,
3600L, 3421L, 3482L, 3543L, 3604L, 3439L, 3500L, 3561L, 3622L,
3899L, 3960L, 4021L, 3905L, 3966L, 4027L, 3909L, 3970L, 4031L,
3927L, 3988L, 4049L, 501L, 431L, 399L, 445L, 541L, 509L, 397L,
406L, 427L, 550L, 512L, 402L, 545L, 430L, 515L, 405L, 465L, 416L,
400L, 558L, 457L, 454L, 553L, 404L, 502L, 546L, 398L, 525L, 905L,
855L, 843L, 841L, 823L, 821L, 845L, 892L, 932L, 830L, 858L, 867L,
854L, 847L, 870L, 918L, 904L, 802L, 911L, 812L, 875L, 872L, 895L,
888L, 930L, 950L, 832L, 893L), class = "data.frame")
答案 0 :(得分:1)
这绝对是一个范围问题。我没有理由相信它与PC和Mac有任何关系。在我的Windows PC上,我按照显示的顺序输入了您提供的代码,以下是它的结束方式:
> # Each year subset run in the function.
> Y1_r <- Single_yr_fn_Y(Y1)
Error in `contrasts<-`(`*tmp*`, ncol(ca), value = ca) :
wrong number of contrast matrix rows
> Y2_r <- Single_yr_fn_Y(Y2)
Error in `contrasts<-`(`*tmp*`, ncol(ca), value = ca) :
wrong number of contrast matrix rows
> Y3_r <- Single_yr_fn_Y(Y3)
Warning message:
In qt((1 - level)/adiv, df) : NaNs produced
特别注意我做会在第1年和第2年时收到对比错误消息,但我不获取错误消息(仅警告)第3年。这是因为数据框Data_1Yr
存在于全局环境中,并且函数正在访问它。此外,它最后定义的只是第3年,因此它是正确的数据集。如果删除该数据框,我会得到不同的结果:
> remove(list = "Data_1Yr")
> Y3_r <- Single_yr_fn_Y(Y3)
Error in ref.grid(object = list(modelStruct = list(reStruct = list(BLOCK = -0.92001152041278, :
Perhaps a 'data' or 'params' argument is needed
正在发生的事情是lsmeans
由于某种原因无法访问函数体内的数据集,并且当数据在全局环境中可用时它有用。这似乎是lsmeans
函数的范围问题,我将对此进行研究。但这并不是因为Mac和PC之间存在差异,而是因为您的全球环境存在差异。请参阅最后的解决方法,了解您现在可以对包执行的操作。
现在,警告信息也很重要:
> head(YIELD.lsm)
ENTRY_NUM lsmean SE df lower.CL upper.CL
1 951 6371.463 394.5384 0 NaN NaN
2 1215 5076.817 394.5384 0 NaN NaN
3 1375 4960.407 394.5384 0 NaN NaN
4 1429 4472.692 394.5384 0 NaN NaN
5 1431 4918.525 394.5384 0 NaN NaN
6 1440 5956.541 394.5384 0 NaN NaN
你收到警告,因为d.f.为零。对于您的估计,使得无法获得置信限制。只看一个ENTRY_NUM
级别......
> subset(df, ENTRY_NUM == "951")
YEAR LOCATION LAT LONG PLOT_NUM ENTRY_NUM BLOCK YIELD PROTEIN
37 2016 KERN 35.36958 -119.3332 37 951 3 6650.642 15.50248
67 2016 KERN 35.36958 -119.3332 67 951 4 5967.717 NA
79 2016 KERN 35.36958 -119.3332 79 951 2 6770.654 15.28507
88 2016 KERN 35.36958 -119.3332 88 951 1 6096.838 15.48798
114 2015 KERN 35.39439 -119.3419 136 951 2 4069.434 NA
147 2015 KERN 35.39439 -119.3419 75 951 3 4869.934 NA
180 2015 KERN 35.39439 -119.3419 85 951 4 5844.528 NA
215 2014 KERN 35.13716 -119.0326 198 951 1 6203.077 12.07000
216 2014 KERN 35.13716 -119.0326 192 951 2 6362.791 NA
217 2014 KERN 35.13716 -119.0326 232 951 3 6693.333 NA
你注意到只涉及一个LOCATION
- 因此为零d.f.如果你阻止它!这是模型中的错误,而不是lsmeans
中的错误。也许你想要的是random = ~ 1 | BLOCK
,但我不确定。确保您正确理解如何指定适当的模型。
Single_yr_fn_Y <- function(data)
{
m.lme <- lme(YIELD ~ ENTRY_NUM,
data = data, random = ~1|BLOCK,
na.action = na.exclude)
org.lsm <- lsmeans(m.lme, "ENTRY_NUM", data = data)
org.lsm
}
我将模型的随机部分修复为更合理的部分。没有必要在模型中括起响应变量。对lsmeans
的调用显式指定了要使用的数据集,该数据集适用于范围界定问题。顺便说一下,我没有看到需要将结果包装在summary
或data.frame
中,因为结果的print
方法只显示您想要查看的内容。这是第1年结果的第一部分:
> Single_yr_fn_Y(Y1)
ENTRY_NUM lsmean SE df lower.CL upper.CL
878 7100.395 335.3031 2 5657.7021 8543.088
951 6419.734 335.3031 2 4977.0407 7862.427
1166 7184.060 335.3031 2 5741.3672 8626.753
1210 7911.242 335.3031 2 6468.5490 9353.935
. . .