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
- 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 steps below 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 editor.
- Click Calculate in the object inspector
.
- Tick Usable as a weight in the object inspector
.
- Select the PCA output, and from the object inspector
, 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.
- From the toolbar, select Calculation
> Custom Code and click on the page to place the output.
-
Input the code below in the R code editor:
#Define your input mode name 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)Note: the first part of the code assumes the PCA output is called
dim.reduce. - The code will return an output like the one below. Select the output, right-click, and select Copy > Copy Data.
- Paste the data into Excel.
Next, we will need to obtain the score weights from the PCA.
- From the toolbar, select Calculation
> Custom Code and click on the page to place the output.
-
Input the code below in the R code editor:
#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, right-click, and select Copy > Copy Data.
- Paste the data into Excel.
In the next step, we will obtain the respondent-level data used in the PCA.
- From the toolbar, select Calculation
> Custom Code and click on the page to place the output.
- In the R code editor, paste:
dim.reduce$data.used. - Select the output, right-click, and select Copy > Copy Data.
- 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 in the object inspector:
- Select the
Scores from dim.reducevariable set from the Data Sources tree. - Right-click and select View in Data Editor.
- Compare the results in the Data Editor
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
UPCOMING WEBINAR: The Roadmap for Market Researchers in the Age of AI