Skip to content

Commit

Permalink
add max_pca param
Browse files Browse the repository at this point in the history
allows reducing size of scree plot when PCA model has lots of components
  • Loading branch information
grlloyd committed Jul 19, 2023
1 parent 584c488 commit 106ae6f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 18 additions & 3 deletions R/PCA_plotfcns.R
Original file line number Diff line number Diff line change
Expand Up @@ -472,20 +472,31 @@ setMethod(f="chart_plot",
#' @include PCA_class.R
#' @examples
#' C = pca_scree_plot()
pca_scree_plot = function(...) {
out=struct::new_struct('pca_scree_plot',...)
pca_scree_plot = function(max_pc=15,...) {
out=struct::new_struct('pca_scree_plot',
max_pc=max_pc,
...)
return(out)
}


.pca_scree_plot<-setClass(
"pca_scree_plot",
contains=c('chart'),
slots=c(max_pc='entity'),
prototype = list(name='Scree plot',
description=paste0('A plot of the percent variance and cumulative ',
'percent variance for the components of a PCA model. '),
type="line",
ontology="STATO:0000386"
ontology="STATO:0000386",
.params=c('max_pc'),
max_pc=entity(
name='Maximum number of components',
description='The maximum number of components to include in the plot.',
value=15,
type=c('numeric','integer'),
max_length=1
)
)
)

Expand All @@ -498,6 +509,10 @@ setMethod(f="chart_plot",
## percent variance
scores=output_value(dobj,'scores')$data
pvar=(colSums(scores*scores)/output_value(dobj,'ssx'))*100

# only include up to max number of components
pvar=pvar[1:min(c(obj$max_pc,length(pvar)))]

A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
labels=round(A$y,digits = 1)
labels=format(labels,1)
Expand Down
4 changes: 3 additions & 1 deletion man/pca_scree_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 106ae6f

Please sign in to comment.