Skip to content

Commit 26c6c2f

Browse files
committed
added pareto scale class
1 parent 0501f31 commit 26c6c2f

File tree

6 files changed

+93
-2
lines changed

6 files changed

+93
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Collate:
5858
'mv_feature_filter_class.R'
5959
'mv_sample_filter_class.R'
6060
'pairs_filter_class.R'
61+
'paretoscale_class.R'
6162
'permutation_test2_class.R'
6263
'permutation_test_class.R'
6364
'permute_sample_order_class.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export(mv_histogram)
5858
export(mv_sample_filter)
5959
export(mv_sample_filter.hist)
6060
export(pairs_filter)
61+
export(pareto_scale)
6162
export(pca_biplot_plot)
6263
export(pca_correlation_plot)
6364
export(pca_loadings_plot)

R/paretoscale_class.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#' Pareto scaling
2+
#'
3+
#' Pareto scaling centres the columns of the data in a dataset object and divides by the square root of the standard deviation.
4+
#'
5+
#' @return A STRUCT model object with methods for pareto scaling.
6+
#'
7+
#' @examples
8+
#' D = iris_dataset()
9+
#' M = pareto_scale()
10+
#' M = model.train(M,D)
11+
#' M = model.predict(M,D)
12+
#'
13+
#' @export pareto_scale
14+
pareto_scale<-setClass(
15+
"pareto_scale",
16+
contains='model',
17+
slots=c(
18+
outputs.scaled='dataset',
19+
outputs.mean='numeric',
20+
outputs.sd='numeric'
21+
),
22+
prototype = list(name='Pareto scaling',
23+
type="preprocessing",
24+
predicted='scaled'
25+
)
26+
)
27+
28+
#' @export
29+
#' @template model_train
30+
setMethod(f="model.train",
31+
signature=c("pareto_scale",'dataset'),
32+
definition=function(M,D)
33+
{
34+
# column means
35+
X=dataset.data(D)
36+
m=colMeans(X)
37+
output.value(M,'mean')=m
38+
s=apply(X, 2, sd)
39+
output.value(M,'sd')=s
40+
return(M)
41+
}
42+
)
43+
44+
#' @export
45+
#' @template model_predict
46+
setMethod(f="model.predict",
47+
signature=c("pareto_scale",'dataset'),
48+
definition=function(M,D)
49+
{
50+
X=dataset.data(D)
51+
Xc=pscale(X,output.value(M,'mean'),output.value(M,'sd'))
52+
dataset.data(D)=as.data.frame(Xc)
53+
name(D)=c(name(D),'The data has been autoscaled')
54+
output.value(M,'scaled')=D
55+
return(M)
56+
}
57+
)
58+
59+
pscale<- function(x,m,s) {
60+
# http://www.gastonsanchez.com/visually-enforced/how-to/2014/01/15/Center-data-in-R/
61+
return( (x - rep(m, rep.int(nrow(x), ncol(x)))) / rep(sqrt(s), rep.int(nrow(x), ncol(x))) )
62+
}

man/model.predict.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/model.train.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pareto_scale-class.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)