I'm trying to replicate some functions from Stata in R, but I'm really really stuck with the e(sample)
function after doing a multiple correspondence analysis (mca).
In Stata the code is this:
clear
set obs 10
gen var1 = cond(_n <= 2, 0, 1)
gen var2 = cond(_n == 1, 0, 1)
gen var3 = var2
mca var1 var2 var3, method(burt)
predict var4 if e(sample)
The last command generates predicted values only for the observations used by mca
.
In R, I have been doing this to do mca:
if(!require("FactoMineR")) {
install.packages("FactoMineR")
library("FactoMineR")
}
if(!require("factoextra")) {
install.packages("factoextra")
library("factoextra")
}
var1 <- c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1)
var2 <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1)
var3 <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1)
df <- data.frame(var1, var2, var3)
df$var1 <- as.factor(df$var1)
df$var2 <- as.factor(df$var2)
df$var3 <- as.factor(df$var3)
mca4 <- MCA(df, ncp = 2, method = "Burt")
mca4$call$marge.col
And I get the same results from the mca process as in Stata, but I've not been able to replicate the last line from the Stata code predict var4 if e(sample)
, I already tried with predict.mca
but it doesn't work at all: it gives me values from the dimensions specified in ncp = 2
, so I guess it doesn't do the same as the predict
command from Stata.
The results from Stata:
mca var1 var2 var3, method(burt)
Statistics for column categories in standard normalization
| Overall | Dimension_1
Categories | Mass Quality %inert | Coord Sqcorr Contrib
-------------+---------------------------+---------------------------
var1 | |
0 | 0.067 1.101 0.188 | 1.786 1.101 0.213
1 | 0.267 1.101 0.047 | -0.446 1.101 0.053
-------------+---------------------------+---------------------------
var2 | |
0 | 0.033 0.936 0.344 | 3.148 0.936 0.330
1 | 0.300 0.936 0.038 | -0.350 0.936 0.037
-------------+---------------------------+---------------------------
var3 | |
0 | 0.033 0.936 0.344 | 3.148 0.936 0.330
1 | 0.300 0.936 0.038 | -0.350 0.936 0.037
---------------------------------------------------------------------
predict var4 if e(sample)
The results of the predict command:
var4
2.912461
.3913612
-.4129778
-.4129778
-.4129778
-.4129778
-.4129778
-.4129778
-.4129778
-.4129778