This article describes replicating the calculation of Components/Dimensions values derived from the Principal Component Analysis analysis in Displayr. The final output matches the values created by Displayr when following the steps outlined here: How to Save Components/Dimensions from a Dimension Reduction Output.
Requirements
Please note these steps require a Displayr license.
- A dimension reduction output created in Displayr such as Principal Component Analysis, Correspondence Analysis, Multidimensional Scaling or t-SNE.
See: How to Do Principal Component Analysis in Displayr.
Method
The below steps assume that the Principal Component Analysis is weighted. If you don't require a weight, you can create a weight of 1 for all respondents. This will return the same outputs as an unweighted analysis. To create a weight of 1 for all respondents:
-
- Hover over the Data Sources tree and select + > Custom Code > JavaScript - Numeric.
- Input a value of 1 into the JavaScript Code window.
- Click Calculate.
- Select Usable as a weight.
- Select the PCA output, go to Data > Filters & Weight, and select the weight from the drop-down.
The first step of the process requires us to obtain the respondent data and weights from the PCA analysis and compute the weighted mean and standard deviation.
-
- Select Calculation > Custom Code from the toolbar and draw an output on the page.
- Go to General > R Code and input the below code:
#Define your input mode name
Note: the first of the code assumes the PCA output is called
input.model = dim.reduce
#Obtain the respondent level data and weights
respondent.data = input.model$data.used$subset.data
weights = input.model$data.used$subset.weights
#Function to calculate the weighted response level data and standard devition
weightedMeanAndSD <- function(x, weights)
{
complete.cases <- !is.na(x) & weights > 0
wx <- x * weights
weighted.mean <- Sum(wx[complete.cases], remove.missing = FALSE) / Sum(weights[complete.cases], remove.missing = FALSE)
sx <- x - weighted.mean
wsx2 <- weights * sx * sx
weighted.variance <- Sum(wsx2[complete.cases], remove.missing = FALSE) / (Sum(weights[complete.cases], remove.missing = FALSE) - 1)
return(list(weighted.mean = weighted.mean, weighted.sd = sqrt(weighted.variance)))
}
weighted.mean.and.sd = lapply(respondent.data, FUN = function(x, weights) { unlist(weightedMeanAndSD(x, weights)) }, weights = weights)
#Return the table wihh the outputs
weighted.mean.and.sd = do.call(rbind, weighted.mean.and.sd)dim.reduce
. - The code will return an output like the one below. Select the output and select Copy > Copy Data from the Tools menu in the upper left corner.
- Paste the data into Excel.
Next, we will need to obtain the score weights from the PCA.
-
- Select Calculation > Custom Code from the toolbar and draw an output on the page.
- Go to Properties > R Code and input the below code:
#Define your input mode name
input.model = dim.reduce
#Obtain the weights
score.weights = input.model$score.weights - The code will return an output like the one below. Select the output and select Copy > Copy Data from the toolbar.
- Paste the data into Excel.
In the next step, we will obtain the respondent-level data used in the PCA.
-
- Select Calculation > Custom Code from the toolbar and draw an output on the page.
- Go to Properties > R Code and input the below code:
dim.reduce$data.used
. - Select the output and select Copy > Copy Data from the toolbar.
- Paste the data into Excel.
For each component, do the sum-product of the scaled respondent data and the component scores for that component (see Excel file with Calculations).
- To check the values match those obtained using Save Variable(s) > Components/ Dimensions, select
Scores from dim.reduce
variable set from the Data Sources tree, right-click > View in Data Editor.
And compare it to the outputs calculated in Excel:
Next
How to Do Principal Component Analysis in Displayr
How to Create a Principal Component Analysis Biplot
How to Create a Dimension Reduction Scatterplot
How to Create a Component Plot from a Principal Component Analysis
How to Create a Goodness of Fit Plot from a Dimension Reduction Output
How to Create a Scree Plot from a Principal Component Analysis
How to Do Multidimensional Scaling