.olsCheckFunction <- function(state) {
if(length(state$dep) < 1)
return("Please select a dependent variable")
if(length(state$indep) < 1)
return("Please select at least one independent variable")
return("")
}
.olsRunFunction <- function(state) {
form <-paste( " ~ " , state$indep[1])
for(var in state$indep[-1])
form <- paste(form,"+",var)
cmd <- paste("summary(ols <-lm(", state$dep, form, ", data =", state$data, "))")
execute(cmd)
}
makeOLSDialog <-function() {
# make the dialog
dialog <- new(SimpleRDialog)
dialog$setSize(500L, 400L)
dialog$setTitle("Regression")
# add a variable selector
variableSelector <-new(VariableSelectorWidget)
variableSelector$setTitle("data")
variableList <- new(VariableListWidget, variableSelector)
addComponent(dialog, variableSelector,1,450,850,10)
# add the dependent variable
dep <- new(SingleVariableWidget, "Dependent Variable", variableSelector)
dep$setTitle("dep")
addComponent(dialog, dep, 50 ,1000, 250, 500)
# add the independent variables
indep <- new(VariableListWidget, "Independent Variables", variableSelector)
indep$setTitle("indep")
addComponent(dialog, indep, 300,1000, 850, 500)
# call the check and run functions
dialog$setCheckFunction(toJava(.olsCheckFunction))
dialog$setRunFunction(toJava(.olsRunFunction))
# For debugging, comment out the following line. Use dialog$run() instead.
### return(dialog)
dialog$run()
}
# simulate data
x1 <- rnorm(100)
x2 <- rnorm(100)
y <- 4 + 2*x1 - .4*x2 + rnorm(100,sd = .2)
df <- data.frame(cbind(y,x1,x2))
# debug the run-function or the dialog
### runfunc <- TRUE
runfunc <- FALSE
if(runfunc == TRUE) {
.olsRunFunction(list(
'data'="df",
'dep'=c("y"),
'indep'=c("x1","x2")
))
} else {
makeOLSDialog()
}