Hi!
When running the following code I get the message: Error in eval(expr,
envir, enclos) : object "A" not found
fm <- function(p,ydata, env = parent.frame()) {
#fit model y ~ (b0+b1x1+...+bpxp)*exp(g2plus*z2plus) where bi and g2plus
are parameters
#create design matrix for linear part
fo00 <-
paste("~",paste(paste("x",1:p,sep=""),collapse="+"),sep="")
fo0 <- as.formula(fo00)
A <- model.matrix(fo0,data=ydata)
# create z variable
z2plus <- ifelse(totx>1,1,0)
ydata <- data.frame(ydata,z2plus)
#run model
t2 <- nls(y ~
A*exp(g2plus*z2plus),data=ydata,start=c(g2plus=0),alg="plinear")
}
model <- fm(4,ydata)
The code inside the function works if not enclosed in a function. This
makes me think that it is a scope problem and I need to specify the
environment. Even though I've read about environment and lexical scope I
still don't fully understand how it works. Sometimes I can do it others
I can't!
Can someone help me?
I've included below code to create a dataframe that can be used to test
my code.
Thanks, Maria
######################
ydata <-
structure(list(x1 = as.integer(c(1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), x2 = as.integer(c(0,
0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1,
0, 1, 0, 1, 0, 0, 1, 0, 1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,
1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1,
0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1,
1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1,
1)), y = c(6.395261598, 6.075346031, 6.396929655, 5.220355825,
6.703188113, 7.290974778, 6.779921908, 6.047372179, 5.837730447,
6.761572769, 6.597145702, 6.371611847, 7.19142933, 6.278521424,
6.646390515, 6.64898455, 6.200509174, 7.297768283, 6.64898455,
6.933423026, 6.666956792, 6.248042875, 6.673297968, 5.697093487,
7.216709487, 6.23636959, 6.643789733, 7.210818454, 5.988961417,
7.305188215, 6.22059017, 7.259116128, 6.613384218, 6.776506992,
6.901737207, 6.311734809, 6.073044534, 6.805722553, 6.921658184,
6.171700597, 6.478509642, 6.877296072, 6.309918278, 6.599870499,
6.173786104, 6.580639137, 6.293419279, 6.980075941, 6.937314081,
6.033086222, 6.64509097, 6.620073207, 6.33150185, 6.97354302,
6.620073207, 6.668228248, 7.153833802, 6.629363253, 6.573680167,
6.54534966, 6.768493212, 7.086737935, 6.144185634, 6.903747258,
7.500529485, 7.134093721, 6.363028104, 7.145984468, 7.355001921,
6.875232087, 6.849066283, 6.630683386, 6.899723107, 6.760414691,
6.154858094, 6.74051936, 5.609471795, 6.82437367, 6.825460036,
6.684611728, 6.610696045, 6.845879875, 6.64898455, 6.878326468,
6.931471806, 7.185387016, 6.858565035, 6.008813185, 7.111512117,
6.639875834, 6.694562059, 6.81014245, 7.617759577, 6.953684211,
6.368187186, 7.007600614, 6.302618976, 6.003887067, 6.56526497,
7.592366129, 7.512617545, 7.438971592, 6.49677499, 6.635946556,
6.771935556, 6.74051936, 6.708084084, 6.401917197, 7.412160335,
6.369900983, 6.822197391, 7.199678346, 6.415096959, 6.914730893,
7.204149292, 7.180831199, 7.17472431, 6.769641977, 6.726233402,
6.385194399, 6.901737207, 7.121252453, 6.672032946, 6.912742821,
7.037027615, 7.496097345, 7.015712421, 6.668228248, 7.074963198,
6.251903883, 6.391917113, 6.456769656, 6.426488458, 6.244166901,
6.56526497, 7.042286172, 6.677083461, 6.054439346, 6.282266747,
6.605297921, 6.326149473, 6.492239835, 6.368187186, 7.218909708,
7.003974137, 6.184148891, 6.622736324, 6.862757913, 7.069874129,
7.509883061), totx = as.integer(c(3, 2, 4, 3, 2, 2, 1, 2, 2,
1, 2, 2, 3, 2, 2, 2, 2, 2, 3, 3, 2, 2, 1, 2, 2, 2, 3, 2, 2, 2,
2, 3, 2, 3, 3, 1, 2, 3, 2, 2, 2, 3, 2, 2, 2, 1, 2, 4, 4, 3, 2,
3, 3, 3, 4, 3, 3, 2, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 4, 3, 3, 2,
3, 2, 2, 2, 3, 3, 3, 3, 1, 3, 3, 4, 3, 3, 2, 3, 3, 3, 3, 2, 3,
3, 3, 3, 3, 1, 1, 3, 3, 2, 3, 2, 2, 1, 2, 2, 3, 3, 2, 3, 3, 3,
4, 4, 4, 2, 3, 3, 4, 3, 3, 3, 3, 3, 3, 4, 3, 2, 2, 2, 1, 3, 2,
1, 1, 1, 0, 1, 0, 1, 1, 2, 1, 1, 1, 1, 1, 2)), z2plus = c(1,
1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1)), .Names = c("x1", "x2", "x3",
"x4", "y", "totx", "z2plus"
), row.names = c("1", "2", "3", "4",
"5", "6", "7", "8", "9",
"10", "11", "12", "13", "14",
"15", "16", "17", "18", "19",
"20",
"21", "22", "23", "24", "25",
"26", "27", "28", "29", "30",
"31",
"32", "33", "34", "35", "36",
"37", "38", "39", "40", "41",
"42",
"43", "44", "45", "46", "47",
"48", "49", "50", "51", "52",
"53",
"54", "55", "56", "57", "58",
"59", "60", "61", "62", "63",
"64",
"65", "66", "67", "68", "69",
"70", "71", "72", "73", "74",
"75",
"76", "77", "78", "79", "80",
"81", "82", "83", "84", "85",
"86",
"87", "88", "89", "90", "91",
"92", "93", "94", "95", "96",
"97",
"98", "99", "100", "101",
"102", "103", "104", "105",
"106",
"107", "108", "109", "110",
"111", "112", "113", "114",
"115",
"116", "117", "118", "119",
"120", "121", "122", "123",
"124",
"125", "126", "127", "128",
"129", "130", "131", "132",
"133",
"134", "135", "136", "137",
"138", "139", "140", "141",
"142",
"143", "144", "145", "146",
"147", "148", "149", "150"), class =
"data.frame")