Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

linear regression - Why does the number of rows change during AIC in R? How to ensure that this doesn't happen?

I'm trying to find a minimal adequate model using AIC in R. I keep getting the following error: Error in step(model) : number of rows in use has changed: remove missing values?

My data:

data<-structure(list(ID = c(1L, 2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 27L, 28L, 29L, 30L, 31L, 33L, 34L, 35L, 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, 98L, 99L, 100L, 102L, 103L, 104L, 105L, 106L, 
107L, 108L, 109L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 
119L, 120L, 121L, 122L, 123L), QnWeight_initial = c(158L, 165L, 
137L, 129L, 155L, 150L, 119L, 153L, 137L, 153L, 158L, 163L, 159L, 
151L, 145L, 144L, 157L, 154L, 144L, 133L, 148L, 151L, 151L, 147L, 
158L, 178L, 164L, 166L, 134L, 151L, 151L, 157L, 148L, 142L, 127L, 
179L, 162L, 142L, 150L, 151L, 153L, 163L, 155L, 163L, 170L, 159L, 
151L, 149L, 154L, 129L, 165L, 128L, 160L, 162L, 134L, 145L, 147L, 
148L, 160L, 165L, 131L, 155L, 169L, 143L, 123L, 153L, 151L, 152L, 
146L, 157L, 154L, 144L, 163L, 153L, 141L, 133L, 167L, 151L, 155L, 
142L, 164L, 158L, 141L, 179L, 146L, 149L, 164L, 156L, 153L, 132L, 
159L, 139L, 139L, 163L, 160L, 155L, 163L, 154L, 135L, 152L, 149L, 
143L, 140L, 160L, 150L, 143L, 160L, 159L, 144L, 169L, 152L, 146L, 
152L, 148L, 138L, 152L), QnWeight_initial_mg = c(15.8, 16.5, 
13.7, 12.9, 15.5, 15, 11.9, 15.3, 13.7, 15.3, 15.8, 16.3, 15.9, 
15.1, 14.5, 14.4, 15.7, 15.4, 14.4, 13.3, 14.8, 15.1, 15.1, 14.7, 
15.8, 17.8, 16.4, 16.6, 13.4, 15.1, 15.1, 15.7, 14.8, 14.2, 12.7, 
17.9, 16.2, 14.2, 15, 15.1, 15.3, 16.3, 15.5, 16.3, 17, 15.9, 
15.1, 14.9, 15.4, 12.9, 16.5, 12.8, 16, 16.2, 13.4, 14.5, 14.7, 
14.8, 16, 16.5, 13.1, 15.5, 16.9, 14.3, 12.3, 15.3, 15.1, 15.2, 
14.6, 15.7, 15.4, 14.4, 16.3, 15.3, 14.1, 13.3, 16.7, 15.1, 15.5, 
14.2, 16.4, 15.8, 14.1, 17.9, 14.6, 14.9, 16.4, 15.6, 15.3, 13.2, 
15.9, 13.9, 13.9, 16.3, 16, 15.5, 16.3, 15.4, 13.5, 15.2, 14.9, 
14.3, 14, 16, 15, 14.3, 16, 15.9, 14.4, 16.9, 15.2, 14.6, 15.2, 
14.8, 13.8, 15.2), QnIdentityConfused = 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, 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), .Label = " No", class = "factor"), Match_mated_status = structure(c(NA, 
NA, NA, NA, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, 
NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, NA, 1L, 
1L, NA, NA, 1L, 1L, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 
1L, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 
NA, NA, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 1L, 
NA, 1L, NA), .Label = " Yes", class = "factor"), Days_till_1st_Wrkr = c(NA, 
21L, NA, 26L, NA, 23L, 22L, 20L, 22L, NA, 20L, 21L, 20L, 20L, 
20L, 21L, NA, NA, 20L, 27L, 21L, 20L, 21L, 20L, 21L, 22L, 22L, 
NA, NA, NA, 22L, NA, 23L, 22L, 22L, NA, 20L, NA, 21L, NA, NA, 
20L, 20L, 20L, 20L, NA, NA, 23L, NA, NA, 23L, 23L, NA, NA, 21L, 
20L, 22L, NA, 21L, NA, 20L, 21L, 21L, 23L, 21L, NA, 22L, 20L, 
22L, 21L, 20L, 26L, 20L, NA, 20L, NA, 20L, 21L, 21L, 21L, 20L, 
21L, 21L, 21L, NA, 20L, 22L, 20L, NA, NA, 20L, NA, 20L, 20L, 
20L, 20L, 21L, 20L, NA, NA, 21L, 20L, 22L, 21L, NA, 22L, 21L, 
21L, 22L, 21L, 21L, NA, NA, 21L, NA, NA), Days_before_max_Wrkr_Eclosion = c(NA, 
12L, NA, 7L, NA, 10L, 11L, 13L, 11L, NA, 13L, 12L, 13L, 13L, 
13L, 12L, NA, NA, 13L, 6L, 12L, 13L, 12L, 13L, 12L, 11L, 11L, 
NA, NA, NA, 11L, NA, 10L, 11L, 11L, NA, 13L, NA, 12L, NA, NA, 
13L, 13L, 13L, 13L, NA, NA, 10L, NA, NA, 10L, 10L, NA, NA, 12L, 
13L, 11L, NA, 12L, NA, 13L, 12L, 12L, 10L, 12L, NA, 11L, 13L, 
11L, 12L, 13L, 7L, 13L, NA, 13L, NA, 13L, 12L, 12L, 12L, 13L, 
12L, 12L, 12L, NA, 13L, 11L, 13L, NA, NA, 13L, NA, 13L, 13L, 
13L, 13L, 12L, 13L, NA, NA, 12L, 13L, 11L, 12L, NA, 11L, 12L, 
12L, 11L, 12L, 12L, NA, NA, 12L, NA, NA), Wrkr_Eclosion_Bin = c(NA, 
3L, NA, 1L, NA, 1L, 2L, 3L, 2L, NA, 3L, 3L, 3L, 3L, 3L, 3L, NA, 
NA, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, NA, NA, NA, 2L, NA, 1L, 
2L, 2L, NA, 3L, NA, 3L, NA, NA, 3L, 3L, 3L, 3L, NA, NA, 1L, NA, 
NA, 1L, 1L, NA, NA, 3L, 3L, 2L, NA, 3L, NA, 3L, 3L, 3L, 1L, 3L, 
NA, 2L, 3L, 2L, 3L, 3L, 1L, 3L, NA, 3L, NA, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, NA, 3L, 2L, 3L, NA, NA, 3L, NA, 3L, 3L, 3L, 3L, 3L, 
3L, NA, NA, 3L, 3L, 2L, 3L, NA, 2L, 3L, 3L, 2L, 3L, 3L, NA, NA, 
3L, NA, NA), QnMass_At_Wrkr_Eclosion = c(NA, 83L, NA, 73L, NA, 
67L, 53L, 78L, 56L, NA, 73L, 90L, 81L, 69L, 66L, 73L, NA, NA, 
70L, 63L, 76L, 78L, 88L, 79L, 75L, 77L, 71L, NA, NA, NA, 86L, 
NA, 66L, 69L, 69L, NA, 88L, NA, 69L, NA, 93L, 71L, 82L, 70L, 
80L, NA, NA, 73L, NA, NA, 93L, 66L, NA, NA, 64L, 72L, 78L, NA, 
76L, NA, 60L, 87L, 89L, 62L, 62L, NA, 63L, 74L, 78L, 71L, 70L, 
118L, 76L, NA, 74L, NA, 96L, 108L, 77L, 68L, 79L, 70L, 67L, 85L, 
115L, 76L, 72L, 81L, 113L, NA, 89L, NA, 75L, 81L, 89L, 82L, 74L, 
81L, NA, NA, 74L, 73L, 80L, 84L, NA, 65L, 73L, 70L, 69L, 76L, 
74L, NA, NA, 80L, NA, NA), ColonyMass_At_Wrkr_Eclosion = c(NA, 
117L, NA, 53L, NA, 91L, 85L, 111L, 96L, NA, 112L, 90L, 112L, 
120L, 110L, 109L, NA, NA, 99L, 86L, 108L, 109L, 87L, 108L, 116L, 
137L, 108L, NA, NA, NA, 93L, NA, 96L, 98L, 87L, NA, 111L, NA, 
114L, NA, 11L, 123L, 113L, 130L, 134L, NA, NA, 96L, NA, NA, 15L, 
74L, NA, NA, 75L, 96L, 88L, NA, 122L, NA, 101L, 83L, 123L, 89L, 
85L, NA, 112L, 98L, 87L, 123L, 115L, 16L, 125L, NA, 91L, NA, 
85L, 76L, 122L, 95L, 113L, 116L, 102L, 132L, 11L, 105L, 112L, 
102L, 8L, NA, 113L, NA, 93L, 104L, 119L, 116L, 112L, 77L, NA, 
NA, 105L, 105L, 41L, 99L, NA, 113L, 120L, 130L, 98L, 122L, 118L, 
NA, NA, 97L, NA, NA), Adult_Wrkrs_At_Wrkr_Eclosion = c(NA, 9L, 
NA, 5L, NA, 1L, 7L, 3L, 2L, NA, 7L, 3L, 6L, 9L, 1L, 5L, NA, NA, 
2L, 1L, 5L, 4L, 6L, 6L, 4L, 5L, 1L, NA, NA, NA, 4L, NA, 3L, 3L, 
2L, NA, 4L, NA, 4L, NA, 0L, 5L, 4L, 3L, 14L, NA, NA, 1L, NA, 
NA, 2L, 1L, NA, NA, 3L, 7L, 2L, NA, 1L, NA, 3L, 7L, 1L, 1L, 5L, 
NA, 1L, 7L, 2L, 4L, 8L, 1L, 2L, NA, 6L, NA, 4L, 5L, 7L, 3L, 6L, 
7L, 5L, 13L, 0L, 4L, 6L, 2L, 0L, NA, 7L, NA, 6L, 1L, 3L, 7L, 
3L, 8L, NA, NA, 6L, 1L, 2L, 6L, NA, 2L, 4L, 4L, 4L, 3L, 7L, NA, 
NA, 5L, NA, NA), Mature_Brood_At_Wrkr_Eclosion = c(NA, 25L, NA, 
13L, NA, 17L, 18L, 27L, 28L, NA, 21L, 22L, 25L, 22L, 35L, 28L, 
NA, NA, 26L, 25L, 26L, 27L, 17L, 26L, 28L, 38L, 30L, NA, NA, 
NA, 22L, NA, 23L, 25L, 27L, NA, 26L, NA, 31L, NA, 2L, 26L, 22L, 
27L, 25L, NA, NA, 26L, NA, NA, 2L, 21L, NA, NA, 20L, 18L, 25L, 
NA, 35L, NA, 26L, 18L, 35L, 27L, 20L, NA, 31L, 22L, 17L, 30L, 
27L, 3L, 35L, NA, 21L, NA, 19L, 27L, 31L, 28L, 24L, 24L, 27L, 
28L, 6L, 27L, 29L, 28L, 1L, NA, 24L, NA, 18L, 31L, 31L, 23L, 
27L, 15L, NA, NA, 30L, 25L, 11L, 32L, NA, 29L, 34L, 36L, 26L, 
33L, 31L, NA, NA, 22L, NA, NA), Sum_wrkrsPlusBrood_At_Wrkr_Eclosion = c(0L, 
34L, 0L, 18L, 0L, 18L, 25L, 30L, 30L, 0L, 28L, 25L, 31L, 31L, 
36L, 33L, 0L, 0L, 28L, 26L, 31L, 31L, 23L, 32L, 32L, 43L, 31L, 
0L, 0L, 0L, 26L, 0L, 26L, 28L, 29L, 0L, 30L, 0L, 35L, 0L, 2L, 
31L, 26L, 30L, 39L, 0L, 0L, 27L, 0L, 0L, 4L, 22L, 0L, 0L, 23L, 
25L, 27L, 0L, 36L, 0L, 29L, 25L, 36L, 28L, 25L, 0L, 32L, 29L, 
19L, 34L, 35L, 4L, 37L, 0L, 27L, 0L, 23L, 32L, 38L, 31L, 30L, 
31L, 32L, 41L, 6L, 31L, 35L, 30L, 1L, 0L, 31L, 0L, 24L, 32L, 
34L, 30L, 30L, 23L, 0L, 0L, 36L, 26L, 13L, 38L, 0L, 31L, 38L, 
40L, 30L, 36L, 38L, 0L, 0L, 27L, 0L, 0L), QnMass_2wksLater = c(NA, 
124L, NA, NA, NA, 111L, NA, NA, NA, NA, NA, 98L, NA, NA, 107L, 
NA, NA, NA, 126L, NA, 115L, NA, NA, NA, 112L, 121L, NA, NA, NA, 
NA, 142L, NA, NA, 132L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 122L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 123L, 
NA, NA, NA, 89L, NA, NA, NA, 100L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 123L, NA, NA, 112L, NA, 126L, 112L, 105L, NA, NA, 129L, 
NA, NA, NA, NA, NA, NA, NA, 134L, NA, NA, NA, NA, NA, NA, 95L, 
85L, NA, NA, 115L, NA, NA, 119L, 122L, NA, NA, NA, 124L, NA, 
NA), QnMass_4wksLater = c(NA, 117L, NA, NA, NA, 88L, NA, NA, 
NA, NA, NA, 111L, NA, NA, 97L, NA, NA, NA, 125L, NA, 119L, NA, 
NA, NA, 104L, 127L, NA, NA, NA, NA, 125L, NA, NA, 126L, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 106L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 104L, NA, NA, NA, 95L, NA, NA, NA, 
94L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 113L, NA, NA, 120L, 
NA, 120L, 104L, 103L, NA, NA, 120L, NA, NA, NA, NA, NA, NA, NA, 
129L, NA, NA, NA, NA, NA, NA, 102L, 75L, NA, NA, 107L, NA, NA, 
137L, 99L, NA, NA, NA, 111L, NA, NA), ColonyMass_4wksLater = c(NA, 
571L, NA, NA, NA, 736L, NA, NA, NA, NA, NA, 438L, NA, NA, 711L, 
NA, NA, NA, 537L, NA, 844L, NA, NA, NA, 560L, 561L, NA, NA, NA, 
NA, 594L, NA, NA, 457L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 714L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 417L, 
NA, NA, NA, 701L, NA, NA, NA, 25L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, 866L, NA, NA, 291L, NA, 659L, 354L, 743L, NA, NA, 696L, 
NA, NA, NA, NA, NA, NA, NA, 518L, NA, NA, NA, NA, NA, NA, 907L, 
27L, NA, NA, 625L, NA, NA, 957L, 804L, NA, NA, NA, 650L, NA, 
NA), Adult_Wrkr_4wksLater = c(NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), QnMass_2mnthsLater = c(NA, 
118L, NA, NA, NA, 86L, NA, NA, NA, NA, NA, 93L, NA, NA, 98L, 
NA, NA, NA, 105L, NA, 101L, NA, NA, NA, 100L, 111L, NA, NA, NA, 
NA, NA, NA, NA, 100L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 99L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 106L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 103L, NA, NA, NA, NA, 114L, 103L, 99L, NA, NA, 86L, NA, 102L, 
NA, NA, NA, NA, NA, 125L, NA, NA, NA, NA, NA, NA, 70L, NA, NA, 
NA, 98L, NA, NA, 111L, 115L, NA, NA, NA, NA, NA, NA), ColonyMass_2mnthsLater = c(NA, 
445L, NA, NA, NA, 1817L, NA, NA, NA, NA, NA, 2683L, NA, NA, 1775L, 
NA, 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the Warnings section of ?step:

The model fitting must apply the models to the same dataset. This may be a problem if there are missing values and R's default of na.action = na.omit is used. We suggest you remove the missing values first.

So you should do:

no.na.data <- na.omit(data[c(predictors, response)])
model <- lm(formula=as.formula(paste(paste(response,'~', sep=''),
                                     paste(predictors,collapse='+'), sep='')),
            no.na.data)
step(model)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...