Browse code

add tests for PCA

Using testthat package

grlloyd authored on 01/04/2019 10:08:03
Showing 4 changed files

... ...
@@ -69,3 +69,5 @@ Imports: ggplot2,
69 69
     emmeans,
70 70
     lme4
71 71
 RoxygenNote: 6.1.1
72
+Suggests: 
73
+    testthat
... ...
@@ -237,8 +237,8 @@ pca_biplot_plot<-setClass(
237 237
     params.points_to_label='entity',
238 238
     params.factor_name='entity',
239 239
     params.groups='entity',
240
-    params.sf='entity'
241
-
240
+    params.scale_factor='entity',
241
+    params.style='entity'
242 242
   ),
243 243
   prototype = list(name='Feature boxplot',
244 244
     description='plots a boxplot of a chosen feature for each group of a dataset.',
... ...
@@ -268,6 +268,11 @@ pca_biplot_plot<-setClass(
268 268
       value=0.95,
269 269
       type='numeric',
270 270
       description='Scaling factor to apply to loadings. Default = 0.95.'
271
+    ),
272
+    params.style=entity(name='Plot style',
273
+      value='points',
274
+      type='character',
275
+      description='Named plot styles for the biplot. [points]'
271 276
     )
272 277
   )
273 278
 
... ...
@@ -307,7 +312,7 @@ setMethod(f="chart.plot",
307 312
 
308 313
     # plot
309 314
     A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
310
-    SP=pca_scores_plot()
315
+    out=pca_scores_plot()
311 316
 
312 317
 
313 318
     if (opt$style=='points')
314 319
new file mode 100644
... ...
@@ -0,0 +1,4 @@
1
+library(testthat)
2
+library(structtoolbox)
3
+
4
+test_check("structtoolbox")
0 5
new file mode 100644
... ...
@@ -0,0 +1,40 @@
1
+# test PCA
2
+test_that('PCA gives expected sum of sqaures for Iris data',{
3
+  # dataset
4
+  D=iris_dataset()
5
+  # PCA model
6
+  M=PCA()
7
+  # train the model
8
+  M=model.train(M,D)
9
+  # apply the model
10
+  M=model.predict(M,D)
11
+  # check the sum of squares
12
+  expect_equal(M$ssx,9539.29)
13
+
14
+  # using 1 component
15
+  M$number_components=1
16
+  # train the model
17
+  M=model.train(M,D)
18
+  # apply the model
19
+  M=model.predict(M,D)
20
+  # check the sum of squares
21
+  expect_equal(M$ssx,9539.29)
22
+})
23
+
24
+test_that('PCA scores chart returns ggplot object',{
25
+  # dataset
26
+  D=iris_dataset()
27
+  # PCA model
28
+  M=PCA()
29
+  # train the model
30
+  M=model.train(M,D)
31
+  # apply the model
32
+  M=model.predict(M,D)
33
+  # chart
34
+  C=pca_scores_plot(groups=D$sample_meta$Species)
35
+  # plot
36
+  gg=chart.plot(C,M)
37
+  plot(gg)
38
+  expect_true(is(gg,'ggplot'))
39
+})
40
+