-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateResponseVariable.R
executable file
·72 lines (63 loc) · 2.22 KB
/
createResponseVariable.R
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
"createResponseVariable" <- function(
data, #@ data frame to which to add the response variable
equation, #@ function for creating the response variable or text character
preTest = TRUE, #@ run the pre test ?
subsetSize = 5 #@ size used for the subset
) {
################################################################################
# Mango Solutions, Chippenham SN15 1BN 2009
# createResponseVariable.R Tue Jun 19 15:20:10 BST 2007 @639 /Internet Time/
#
# Author: Romain
################################################################################
# DESCRIPTION: create the response
# KEYWORDS: component:response
################################################################################
.requiredArgs(data)
print("data")
print(data)
print("equation")
print(equation)
if(!is.data.frame(data)) ectdStop("`data` must be a data frame")
nr <- nrow(data)
eqFun <- try( match.fun( equation ), silent = TRUE )
# See 'utils.R'
eqFun <- if( class(eqFun) == "try-error") {
function( data ){
print("eval equation")
eval( parse( text = equation) ,data )
}
} else {
print("Else")
.checkFun( eqFun, "data")
}
# Run a pre test to find out if the equation will run a
# small subset of the data
if( preTest && nr > subsetSize ){
subsetData <- data[ 1:subsetSize , , drop=FALSE ]
out <- try( eqFun( subsetData), silent = TRUE )
if( class(out) == "try-error" ) {
ectdStop("Error when evaluating equation on subset of the data")
}
if( length(out) != subsetSize ){ #: testLength
ectdStop(
"The equation given for the response does not generate" %.nt%
"a vector of length equal to the dimension of the data"
)
}
}
# Call the equation on the data.
out <- try( eqFun(data), silent = TRUE )
if( class(out) == "try-error") {
ectdStop( "Error when executing the equation statement on the data")
}
if( length(out) != nr ){
ectdStop(
"The equation given for the response does not generate" %.n%
"a vector of length equal to the dimension of the data ($nr)"
)
}
print("out")
print(out)
out
}