# NOT RUN {
## get help for a method
## help("as.factor,db.obj-method")
<!-- %% @test .port Database port number -->
<!-- %% @test .dbname Database name -->
## set up the database connection
## Assume that .port is port number and .dbname is the database name
cid <- db.connect(port = .port, dbname = .dbname, verbose = FALSE)
## create a temporary table from the example data.frame "abalone"
x <- as.db.data.frame(abalone, conn.id = cid, verbose = FALSE)
## set sex to be a categorical variable
x$sex <- as.factor(x$sex)
fit1 <- madlib.lm(rings ~ . - id, data = x) # linear regression
fit2 <- madlib.glm(rings < 10 ~ . - id, data = x, family = "binomial") # logistic regression
## another temporary table
z <- as.db.data.frame(abalone, conn.id = cid, verbose = FALSE)
## specify factor during fitting
fit3 <- madlib.lm(rings ~ as.factor(sex) + length + diameter, data = z)
## as.factor is automatically used onto text column
## so as.factor is not necessary
fit4 <- madlib.glm(rings < 10 ~ sex + length + diameter, data
= z, family = "binomial")
## using relevel to change the reference level
x$sex <- relevel(x$sex, ref = "M")
madlib.lm(rings ~ . - id, data = x) # use "M" as the reference level
db.disconnect(cid, verbose = FALSE)
# }
Run the code above in your browser using DataLab