<!--head
meta:
  title: Principal Component Analysis
  author: Daniel Nagy
  description: In this template Rapporter will present you Principal Component Analysis.
  email: ~
  packages:
  - psych
  - GPArotation
  - rapportools
  - pander
  example:
  - rapport('PCA', data=mtcars, vars=c('carb', 'mpg', 'cyl', 'drat'), components=3)
  - rapport('PCA', data=mtcars, vars=c('carb', 'mpg', 'cyl', 'drat'), components=3, rot.method="oblimin")
inputs:
- name: vars
  label: Used Variables
  description: Which variables would you use?
  class: numeric
  length:
    min: 2.0
    max: 1000.0
  required: yes
  standalone: no
- name: components
  label: Number of Components
  description: How many Principal Components you want to use?
  class: integer
  limit:
    min: 1.0
    max: 999.0
  value: 2.0
  required: yes
  standalone: yes
- name: rot.matrix
  label : Rotation Matrix
  description: Would you check the Rotation Matrix?
  class: logical
  length:
    min: 1.0
    max: 1.0
  value: yes
  required: no
  standalone: yes
- name: rot.method
  label: Method of Rotation
  description: Which rotation method would you use?
  class: character
  length:
    min: 1.0
    max: 1.0
  options:
  - none
  - varimax
  - quartimax
  - promax
  - oblimin
  - simplimax
  - cluster
  value: varimax
  matchable: yes
  allow_multiple: no
  required: no
  standalone: yes
head-->

<% if (components > ncol(vars)) { %>

Your request cannot be implemented, because there are more components (<%= components %>) than the number of the used variables (<%= ncol(vars) %>). Please set the number of the components to maximum <%= ncol(vars) - 1 %> with the same number of the variables or extend the number of those variables to <%= components + 1 %>

<% } else { %>


# Introduction

[Principal Component Analysis](https://en.wikipedia.org/wiki/Principal_component_analysis) is a dimension reduction method. It produces linearly independent principal components using the variances of the observations in a set of variables.


# Results

<%=
vars <- na.omit(vars)
summ <- as.matrix(summary(prcomp(vars))$importance[, 1:components])
if (components == 1) colnames(summ)[1] <- "PC1"
summ
%>
From the table above one can see that the <%= ifelse(components == 1, "", "first") %> <%= components %> Principal Component<%= ifelse(components == 1, '', 's') %> contains the <%= paste(summary(prcomp(vars))$importance[2, 1:components] * 100, "%") %> of the variance<%= ifelse(components == 1, '', 's') %><% if (components != 1) { %>, so together the <%= paste(sum(summary(prcomp(vars))$importance[2, 1:components] * 100), "%") %> of that.<% } else { %>.<% } %>

### Visual representation

It could be informative to see visually how the observations lies on these components. On that two dimensional plot below, where the axes are the components which contains the two most variances, you can see (the red vectors) the effect of the variables as well. <% if(ncol(vars) > 5) { %> In the case we have several variables, the plot could be a little bit confusing but it still helps us to explore the number of the Principal Components. <% } %>

<%=
biplot(prcomp(vars))
%>

<% if (rot.matrix) { %>

## Rotation


<%=
ifelse(rot.matrix,"As you wanted to check the Rotation matrix let us present that for you:","")
if (rot.matrix) {
  rot <- as.matrix(prcomp(vars)$rotation[,1:components])
  if (components == 1) {colnames(rot)[1] <- "PC1"}
  emphasize.strong.cells(which(abs(rot) > 0.3, arr.ind = TRUE))
  rot
} else { }
%>

The cells written in bold shows which components explain the most variances of the variables, with the help of them we can draw the following conclusion:
<%= paste(pandoc.list.return(paste(colnames(rot)[which(abs(rot) > 0.3, arr.ind = TRUE)[, 2]],name(vars)[which(abs(rot) > 0.3, arr.ind = TRUE)[, 1]], sep = " is a principal component of ")), collapse = '\n') %>

<% if (length(which(rot > 0.3)) != length(which(abs(rot) > 0.3))) { %>

<%= neg.comp <- colnames(rot)[which(rot < -0.3, arr.ind = TRUE)[, 2]] %>

From them in the case<%= ifelse(neg.comp < 1, "s", "") %> of the <%= paste(colnames(rot)[which(rot < -0.3, arr.ind = TRUE)[, 2]],name(vars)[which(rot < -0.3, arr.ind = TRUE)[, 1]], sep = "'s impact on ") %>, we can say <%= ifelse(neg.comp < 1, "they are", "that is") %> negative.

<% } else { %>
We can say that <%= ifelse(length(which(abs(rot) > 0.3)), "none of these impacts are negative", "this impact is positive") %>.

<% }}} %>
