[Rd] stepAIC() (PR#2655)

slteng at stat.berkeley.edu slteng at stat.berkeley.edu
Wed Mar 19 11:47:15 MET 2003


Full_Name: Melinda Teng
Version: 1.6.2
OS: windows M E
Submission from: (NULL) (136.152.196.249)


Greatly appreciate for any advice on the following. Many grateful thanks. Please
do not hesitate to contact me for further details.
-------------------------------------------------------------------

Error message :
"Error in model.frame.default(formula = Y ~ X4, data = TS, drop.unused.levels =
TRUE) : Object "TS" not found"

Actions taken :
debug(), traceback(), and looking into model.frame.default.

Situation : 
stepAIC() is used to select variables into a regression model by forward
selection procedure. This stepAIC() is programmed within a function where the
data.frame of the required data are defined and computed.

Problem : When I cut and paste the body of the function Q and run in the
workspace, it will give me the required output and the data.frame from gfit (T)
is passed into stepAIC() for the selection procedure. However when I execute the
same codes, but now through the function (Q(), see below), the above error
message appears.

Code snippets :
rm(list=ls(all=TRUE))
library(base); library(boot); library(MASS); library(methods); library(rpart);
library(nls)

B <- data.frame 
   Y    X1    X2    X3    X4
1  0 -1.14 -0.47 -0.05 -0.98
2  0 -1.14 -0.47 -0.05 -0.98
3  0 -1.14 -0.47 -0.05 -0.98
4  0 -1.14 -0.47 -0.05 -0.98
5  0 -1.14 -0.47 -0.05 -0.98
6  0 -1.14 -0.47 -0.05 -0.98
7  0 -1.14 -0.47 -0.05 -0.98
8  0 -1.14 -0.47 -0.05 -0.98
9  0 -1.14 -0.47 -0.05 -0.98
10 0 -1.14 -0.47 -0.05 -0.98
11 0 -1.14 -0.47 -0.05 -0.98
12 0 -1.14 -0.47 -0.05 -0.98
13 0 -1.14 -0.47 -0.05 -0.98
14 0 -1.14 -0.47 -0.05 -0.98
15 0 -1.14 -0.47 -0.05 -0.98
16 0 -1.14 -0.47 -0.05 -0.98
17 0 -1.14 -0.47 -0.05 -0.98
18 0 -1.14 -0.47 -0.05 -0.98
19 0 -1.14 -0.47 -0.05 -0.98
20 0 -1.14 -0.47 -0.05 -0.98
21 0 -1.14 -0.47 -0.05 -0.98
22 1 -1.14 -0.47 -0.05 -0.98
23 0 -1.14 -0.47 -0.05 -0.98
24 1 -1.14 -0.47 -0.05 -0.98
25 0 -1.14 -0.47 -0.05 -0.98
26 0 -1.14 -0.47 -0.05 -0.98
27 0 -1.14 -0.47 -0.05 -0.98
28 0 -1.14 -0.47 -0.05 -0.98
29 1 -1.14 -0.47 -0.05 -0.98
30 1 -1.14 -0.47 -0.05 -0.98
31 0 -1.14 -0.47 -0.05 -0.98
32 0 -1.14 -0.47 -0.05 -0.98
33 0 -1.14 -0.47 -0.05 -0.98
34 0 -1.14 -0.47 -0.05 -0.98
35 0 -1.14 -0.47 -0.05 -0.98
36 0 -1.14 -0.47 -0.05 -0.98
37 0 -1.14 -0.47 -0.05 -0.98
38 0 -1.14 -0.47 -0.05 -0.98
39 0 -1.14 -0.47 -0.05 -0.98
40 1 -1.14 -0.47 -0.05 -0.98
41 0 -1.14 -0.47 -0.05 -0.98
42 0 -1.14 -0.47 -0.05 -0.98
43 0 -1.14 -0.47 -0.05 -0.98
44 1 -1.14 -0.47 -0.05 -0.98
45 0 -1.14 -0.47 -0.05 -0.98
46 0 -1.14 -0.47 -0.05 -0.98
47 0 -1.14 -0.47 -0.05 -0.98
48 0 -1.14 -0.47 -0.05 -0.98
49 0 -1.14 -0.47 -0.05 -0.98
50 1 -1.14 -0.47 -0.05 -0.98

Z <- data.frame
   Y    X1    X2    X3    X4
1  1  2.30  0.48  1.27  0.29
2  1  2.30  0.48  1.27  0.29
3  1  2.30  0.48  1.27  0.29
4  1  2.30  0.48  1.27  0.29
5  1  2.30  0.48  1.27  0.29
6  0 -1.67  1.13 -1.63  0.22
7  1 -1.67  1.13 -1.63  0.22
8  1 -1.67  1.13 -1.63  0.22
9  1 -1.67  1.13 -1.63  0.22
10 1 -1.67  1.13 -1.63  0.22
11 0  0.61  1.11 -0.37 -1.43
12 1  0.61  1.11 -0.37 -1.43
13 0  0.61  1.11 -0.37 -1.43
14 0  0.61  1.11 -0.37 -1.43
15 0  0.61  1.11 -0.37 -1.43
16 1 -0.49  0.68 -1.15  0.32
17 0 -0.49  0.68 -1.15  0.32
18 1 -0.49  0.68 -1.15  0.32
19 1 -0.49  0.68 -1.15  0.32
20 1 -0.49  0.68 -1.15  0.32
21 1 -1.05  0.19 -0.21 -0.54
22 0 -1.05  0.19 -0.21 -0.54
23 0 -1.05  0.19 -0.21 -0.54
24 1 -1.05  0.19 -0.21 -0.54
25 1 -1.05  0.19 -0.21 -0.54
26 1 -0.48 -1.45  0.15  0.90
27 1 -0.48 -1.45  0.15  0.90
28 1 -0.48 -1.45  0.15  0.90
29 1 -0.48 -1.45  0.15  0.90
30 1 -0.48 -1.45  0.15  0.90
31 1  1.33  1.17  0.70  0.50
32 1  1.33  1.17  0.70  0.50
33 1  1.33  1.17  0.70  0.50
34 1  1.33  1.17  0.70  0.50
35 1  1.33  1.17  0.70  0.50
36 0 -0.62 -0.15  0.44  0.94
37 1 -0.62 -0.15  0.44  0.94
38 1 -0.62 -0.15  0.44  0.94
39 1 -0.62 -0.15  0.44  0.94
40 1 -0.62 -0.15  0.44  0.94
41 1  0.90  0.68  0.50 -0.37
42 0  0.90  0.68  0.50 -0.37
43 1  0.90  0.68  0.50 -0.37
44 0  0.90  0.68  0.50 -0.37
45 0  0.90  0.68  0.50 -0.37
46 1  1.26 -0.22 -1.83 -0.37
47 1  1.26 -0.22 -1.83 -0.37
48 0  1.26 -0.22 -1.83 -0.37
49 0  1.26 -0.22 -1.83 -0.37
50 1  1.26 -0.22 -1.83 -0.37

S <- matrix

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[,15] [,16] [,17] [,18] [,19] [,20] [,21]
 [1,]    0    0    0    1    0    0    0    0    1     1     0     0     0     0
    0     0     0     0     0     0     0
 [2,]    0    0    0    0    0    1    0    0    0     0     0     0     0     0
    1     0     0     0     0     0     1
 [3,]    0    0    0    0    1    0    0    1    0     0     0     0     0     0
    0     0     0     1     0     0     0
 [4,]    0    1    0    0    0    0    0    0    0     0     1     0     0     0
    0     1     0     0     0     0     0
 [5,]    0    0    0    0    0    0    0    0    0     0     0     1     1     0
    0     0     0     0     0     0     0
 [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0
    0     0     0     0     1     0     0
 [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     1
    0     0     0     0     0     0     0
 [8,]    1    0    1    0    0    0    0    0    0     0     0     0     0     0
    0     0     0     0     0     0     0
 [9,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0
    0     0     1     0     0     1     0
[10,]    0    0    0    0    0    0    1    0    0     0     0     0     0     0
    0     0     0     0     0     0     0
      [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[,34] [,35] [,36] [,37] [,38] [,39] [,40]
 [1,]     0     0     0     0     0     0     0     0     0     0     1     0   
 0     0     0     1     0     0     0
 [2,]     0     0     0     0     0     1     0     0     0     0     0     0   
 1     0     0     0     0     0     0
 [3,]     0     0     1     1     0     0     0     0     0     0     0     0   
 0     0     0     0     0     0     0
 [4,]     0     0     0     0     0     0     0     0     0     0     0     1   
 0     0     0     0     0     0     0
 [5,]     0     0     0     0     0     0     0     1     0     0     0     0   
 0     0     0     0     1     0     0
 [6,]     1     0     0     0     0     0     1     0     0     0     0     0   
 0     1     0     0     0     0     0
 [7,]     0     1     0     0     0     0     0     0     1     0     0     0   
 0     0     0     0     0     0     0
 [8,]     0     0     0     0     0     0     0     0     0     1     0     0   
 0     0     0     0     0     0     0
 [9,]     0     0     0     0     0     0     0     0     0     0     0     0   
 0     0     1     0     0     1     0
[10,]     0     0     0     0     1     0     0     0     0     0     0     0   
 0     0     0     0     0     0     1
      [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
 [1,]     0     0     0     0     0     0     0     0     0     0
 [2,]     0     0     0     0     0     0     0     0     0     0
 [3,]     0     0     0     0     0     0     0     0     0     0
 [4,]     0     1     0     0     0     0     0     0     0     0
 [5,]     0     0     0     1     0     0     0     0     0     0
 [6,]     0     0     0     0     0     1     0     0     0     0
 [7,]     1     0     0     0     0     0     0     1     0     0
 [8,]     0     0     1     0     0     0     0     0     1     0
 [9,]     0     0     0     0     1     0     0     0     0     0
[10,]     0     0     0     0     0     0     1     0     0     1

v <- 10


Q <- function(v, Z, B, S) {

	for (m in 1:v) {

		T <- Z[S[m,]==0,]

		options(contrasts=c("contr.treatment", "contr.poly"))
		gfit <- glm(Y~1, family=binomial(link=logit), data = T)
		ffit <- stepAIC(gfit, as.formula(paste(paste(colnames(Z)[1]), paste("~"),
paste(colnames(Z)[2:NCOL(Z)], collapse= "+"))), direction = c("forward"), trace
= FALSE, k=0)
	}
}



More information about the R-devel mailing list