883f7f27 |
#'linear model class
#'
#' wrapper for R lm.
#'
#' @import struct
#' @export linear_model
|
1a7cd70b |
#' @examples
#' M = linear_model()
|
883f7f27 |
linear_model<-setClass(
|
c34a3c53 |
"linear_model",
contains='model',
slots=c(
# INPUTS
params.formula='entity',
params.na_action='enum',
params.contrasts='entity',
|
883f7f27 |
|
c34a3c53 |
# OUTPUTS
outputs.lm='entity',
outputs.coefficients='entity',
outputs.residuals='entity',
outputs.fitted_values='entity',
|
a1752138 |
outputs.predicted_values='entity',
outputs.r_squared='entity',
outputs.adj_r_squared='entity'
|
883f7f27 |
|
c34a3c53 |
),
prototype = list(name='Linear Model',
description='Used to fit linear models. It can be used to carry out regression, single stratum analysis of variance and analysis of covariance.',
type="regression",
predicted='predicted_values',
|
883f7f27 |
|
c34a3c53 |
params.formula=entity(name='Model Formula',
description='Compact symbolic form of the equation to be fitted using a linear model.',
value=y~x,
|
384d31b3 |
type='formula',
max_length=Inf
|
c34a3c53 |
),
params.na_action=enum(name='NA Action',
description='The action to be taken when encoutering NA',
value='na.omit',
type='character',
list=c('na.omit','na.fail','na.exclude','na.pass')
),
params.contrasts=entity(name='Contrasts',
|
c17a810f |
description='The contrasts associated with a factor. If zero length then the default contrasts are used.',
|
c34a3c53 |
type='list'
),
|
883f7f27 |
|
c34a3c53 |
outputs.lm=entity(name='Linear model object',
description='The lm object for this model.',
type='lm'
),
outputs.coefficients=entity(name='Model coefficients',
description='The coefficients for the fitted model.',
type='numeric'
),
outputs.residuals=entity(name='Residuals',
description='The residuals for the fitted model.',
type='numeric'
),
outputs.fitted_values=entity(name='Fitted values',
description='The fitted values for the data used to train the model.',
type='numeric'
),
outputs.predicted_values=entity(name='Predicted values',
description='The predicted values for new data using the fitted model.',
type='numeric'
|
a1752138 |
),
outputs.r_squared=entity(name='R Squared',
description='The value of R Squared for the fitted model.',
type='numeric'
),
outputs.adj_r_squared=entity(name='Adjusted R Squared',
description='The value ofAdjusted R Squared for the fitted model.',
type='numeric'
|
c34a3c53 |
)
)
|
883f7f27 |
)
#' @export
setMethod(f="model.train",
|
c34a3c53 |
signature=c("linear_model",'dataset'),
definition=function(M,D)
{
X=cbind(D$data,D$sample_meta)
|
c17a810f |
if (length(M$contrasts)==0) {
|
c34a3c53 |
M$lm=lm(formula = M$formula, na.action = M$na_action,data=X) # default contrasts
} else {
M$lm=lm(formula = M$formula, na.action = M$na_action, contrasts = M$contrasts,data=X)
}
M$coefficients=coefficients(M$lm)
M$residuals=residuals(M$lm)
M$fitted_values=fitted(M$lm)
|
a1752138 |
M$r_squared=summary(M$lm)$r.squared
M$adj_r_squared=summary(M$lm)$adj.r.squared
|
c34a3c53 |
return(M)
}
|
883f7f27 |
)
#' @export
setMethod(f="model.predict",
|
c34a3c53 |
signature=c("linear_model",'dataset'),
definition=function(M,D)
{
X=cbind(D$data,D$sample_meta)
M$predicted_values(predict(M$lm,newdata = X))
return(M)
}
|
883f7f27 |
)
|