Naive Bayes classifier in R - r

I'm trying to run a NaiveBayes classifier running this code:
library(e1071)
filename <- "file.csv"
data <- read.csv(filename, sep=",")
trainData <- data[1:1000,,drop = FALSE]
testData <- data[1001:1500,,drop = FALSE]
model <- naiveBayes(trainData[,1:49], trainData[,50])
predicted <- predict(model, testData[,-50])
I get this error:
Error in apply(log(sapply(seq_along(attribs), function(v) { :
dim(X) must have a positive length
I checked and I don't see any missing attributes. What could be the problem?
Some sample data, following Dr Mike's request. Note that the data contains 50 columns (in my original post I used 30 since I tried to simplify things)
structure(list(X = 1:10, X1 = c(12L, 6L, 6L, 11L, 17L, 7L, 5L,
3L, 4L, 7L), X2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "A", class = "factor"), X3 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "us", class = "factor"),
X4 = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE,
TRUE), X5 = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE,
FALSE, FALSE, TRUE), X6 = c(FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE), X7 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "DAY", class = "factor"),
X8 = c(159L, 47L, 163L, 51L, 171L, 145L, 31L, 49L, 49L, 154L
), X9 = structure(c(2L, 5L, 2L, 4L, 3L, 2L, 1L, 5L, 1L, 2L
), .Label = c("100-150", "150-200", "50-100", "650-700",
"unknown"), class = "factor"), X10 = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L), X11 = c(0L, 1L, 0L, 2L, 0L, 0L, 0L,
1L, 0L, 0L), X12 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), X13 = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), X14 = c(0L,
0L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37 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X38 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X39 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), X40 = c(3L, 1L, 4L, 3L, 0L,
2L, 0L, 3L, 1L, 0L), X41 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), X42 = c(3L, 6L, 3L, 2L, 0L, 2L, 0L, 3L, 2L,
3L), X43 = c(1L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L), X44 = c(1L,
2L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), X45 = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), X46 = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L), X47 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), X48 = c(0L, 1L, 0L, 1L, 0L, 0L, 1L, 2L, 1L,
0L), X49 = c(2L, 0L, 1L, 1L, 0L, 0L, 2L, 3L, 0L, 4L), target = c(FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
)), .Names = c("X", "X1", "X2", "X3", "X4", "X5", "X6", "X7",
"X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15", "X16",
"X17", "X18", "X19", "X20", "X21", "X22", "X23", "X24", "X25",
"X26", "X27", "X28", "X29", "X30", "X31", "X32", "X33", "X34",
"X35", "X36", "X37", "X38", "X39", "X40", "X41", "X42", "X43",
"X44", "X45", "X46", "X47", "X48", "X49", "target"), row.names = c(NA,
10L), class = "data.frame")
Following Dr Mike's advice I factorized the data. I still get the exact same error.
So the code is now:
data <- read.csv(filename, sep=",")
data[, -which(sapply(data, is.numeric))]<-
as.data.frame(lapply((data[,-which(sapply(data, is.numeric))]), as.factor))
trainData <- data[1:1000,,drop = FALSE]
testData <- data[1001:1500,,drop = FALSE]
model <- naiveBayes(trainData[,1:49], trainData[,50])
predicted <- predict(model, testData[,-50])

You should make sure to work with factors for the categorical variables. The following small example using your data.
data$target[1:5]<-TRUE
data$target[6:10]<-FALSE
data<-data[sample(1:10),]
# Make sure the target is a factor
data$target<-as.factor(data$target)
# Work with a subset since we only have 10 data points.
data2<-data[,c(2,49:50,51)]
model<-naiveBayes(target~., data=data2[1:5,])
predict(model, data2[6:10,])
table(predict(model, data2[6:10,]), data[6:10, 5])
# FALSE TRUE
# FALSE 0 0
# TRUE 0 5
So make sure to convert categorical variables and response to factors and use a subset of the variables.

Found the problem and recording the solution in case someone googles this error like I did.
In this case, the problem was that the "target" attribute contained only examples labeled "false". Since there were no examples labelled "true" the classifier didn't work.
I still don't understand why I got this specific error and not something readable like: "cannot classify, no "true" labelled examples". But at least I solved it.
Many thanks to Dr.Mike for sending me in the right direction (which in this case was - look at the data, not at the code)

I had a similar issue. After many hours or research I changed TRUE/FALSE to
character values y/n.
It seems R gets confused about whether TRUE/FALSE is categorical or numeric.

Related

Perform a PCA to check if there's separation between biological replicates of different sexes. How can i do it in this case with the data I have?

I have to identify genes showing sex specific expression in 2 tissues: "pancreas" and "lung".
To do it first of all i need to do a PCA to ascertain whether there is separation between tissues of different sexes (in particular there are 3 individuals of sex 1 and 3 of sex2 for each tissue)
I suppose that i should classify the genes in counts for sex by using the sex column in the Design list and after I should perform a PCA where different colors are assigned to sex 1 and sex 2 genes.
The problem is that even if I know what I should do to perform the PCA (if what i tought is right) I don't know how to write the codes required to do it: how can i create a new dataframe made by only the genes in count that correspond to lung and pancreas rows in Design?
I thought to do in this way in order to color the genes with different colors depending by sex (information shown in Design), if there's a simplier way is well accepted any suggestion.
dput(Design[1:10,]):
Design = structure(list(Individual = c("GTEX-Y5V6", "GTEX-1KXAM", "GTEX-18A67",
"GTEX-14BMU", "GTEX-13PVR", "GTEX-1211K", "GTEX-1KXAM", "GTEX-18A67",
"GTEX-14BMU", "GTEX-1211K"), sex = c(1L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 2L, 2L), age = c("60-69", "60-69", "50-59", "20-29",
"60-69", "60-69", "60-69", "50-59", "20-29", "60-69"), RNA.quality..max10. = c(7.1,
8.1, 7.2, 7.2, 7.3, 7, 7.2, 7.3, 7.4, 8.2), organ = c("Thyroid",
"Thyroid", "Thyroid", "Thyroid", "Thyroid", "Thyroid", "Stomach",
"Stomach", "Stomach", "Stomach"), tissue = c("Thyroid", "Thyroid",
"Thyroid", "Thyroid", "Thyroid", "Thyroid", "Stomach", "Stomach",
"Stomach", "Stomach")), row.names = c("GTEX-Y5V6-0526-SM-4VBRV",
"GTEX-1KXAM-1726-SM-D3LAE", "GTEX-18A67-0826-SM-7KFTI", "GTEX-14BMU-0226-SM-5S2QA",
"GTEX-13PVR-0626-SM-5S2RC", "GTEX-1211K-0726-SM-5FQUW", "GTEX-1KXAM-0926-SM-CXZKA",
"GTEX-18A67-2626-SM-718AD", "GTEX-14BMU-1126-SM-5RQJ8", "GTEX-1211K-1426-SM-5FQTF"
), class = "data.frame")
dput(counts[1:10,]):
structure(list(`GTEX-Y5V6-0526-SM-4VBRV` = c(0L, 1L, 2L, 1L,
0L, 0L, 0L, 0L, 0L, 214L), `GTEX-1KXAM-1726-SM-D3LAE` = c(0L,
0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 205L), `GTEX-18A67-0826-SM-7KFTI` = c(0L,
0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 164L), `GTEX-14BMU-0226-SM-5S2QA` = c(0L,
0L, 0L, 12L, 0L, 0L, 0L, 0L, 0L, 108L), `GTEX-13PVR-0626-SM-5S2RC` = c(0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 100L), `GTEX-1211K-0726-SM-5FQUW` = c(0L,
0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 174L), `GTEX-1KXAM-0926-SM-CXZKA` = c(2L,
1L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 99L), `GTEX-18A67-2626-SM-718AD` = c(7L,
3L, 7L, 2L, 0L, 1L, 5L, 0L, 0L, 116L), `GTEX-14BMU-1126-SM-5RQJ8` = c(0L,
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 44L), `GTEX-1211K-1426-SM-5FQTF` = c(4L,
0L, 5L, 2L, 0L, 0L, 0L, 0L, 0L, 143L), `GTEX-11TT1-0726-SM-5GU5A` = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 57L), `GTEX-1HCUA-1626-SM-A9SMG` = c(0L,
0L, 0L, 22L, 0L, 0L, 0L, 0L, 0L, 53L), `GTEX-1KXAM-0226-SM-EV7AP` = c(0L,
0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 75L), `GTEX-18A67-1726-SM-7KFT9` = c(0L,
0L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 73L), `GTEX-14BMU-0726-SM-73KXS` = c(0L,
0L, 0L, 40L, 0L, 0L, 0L, 0L, 0L, 74L), `GTEX-13PVR-0726-SM-5S2PX` = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 54L), `GTEX-1211K-1126-SM-5EGGB` = c(0L,
1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 25L), `GTEX-11TT1-0326-SM-5LUAY` = c(0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 54L), `GTEX-1KXAM-2426-SM-DIPFC` = c(1L,
0L, 2L, 1L, 0L, 0L, 2L, 0L, 0L, 29L), `GTEX-18A67-0326-SM-7LG5X` = c(0L,
0L, 5L, 4L, 0L, 0L, 2L, 0L, 1L, 91L), `GTEX-14BMU-2026-SM-5S2W6` = c(0L,
0L, 2L, 5L, 0L, 0L, 0L, 0L, 0L, 30L), `GTEX-13PVR-2526-SM-5RQIT` = c(0L,
0L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 14L), `GTEX-1211K-2126-SM-59HJZ` = c(1L,
0L, 2L, 0L, 0L, 0L, 1L, 0L, 0L, 51L), `GTEX-Y3I4-2326-SM-4TT81` = c(0L,
0L, 3L, 0L, 0L, 0L, 1L, 0L, 0L, 38L), `GTEX-1KXAM-0426-SM-DHXKG` = c(0L,
0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 105L), `GTEX-18A67-1126-SM-7KFSB` = c(1L,
0L, 0L, 4L, 0L, 0L, 1L, 0L, 0L, 76L), `GTEX-14BMU-0526-SM-73KW4` = c(0L,
0L, 0L, 11L, 0L, 0L, 0L, 0L, 0L, 53L), `GTEX-1211K-0826-SM-5FQUP` = c(1L,
0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 104L), `GTEX-11TT1-1626-SM-5EQL7` = c(0L,
0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 113L), `GTEX-ZYFG-0226-SM-5GIDT` = c(1L,
0L, 2L, 2L, 0L, 0L, 2L, 0L, 0L, 54L), `GTEX-1KXAM-0826-SM-CXZK9` = c(0L,
0L, 0L, 5L, 0L, 0L, 2L, 0L, 0L, 97L), `GTEX-18A67-2426-SM-7LT95` = c(1L,
0L, 2L, 0L, 0L, 1L, 3L, 0L, 0L, 69L), `GTEX-14BMU-0926-SM-5S2QB` = c(0L,
0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 29L), `GTEX-13PVR-1826-SM-5Q5CC` = c(1L,
0L, 0L, 3L, 0L, 1L, 2L, 0L, 0L, 32L), `GTEX-1211K-0926-SM-5FQTL` = c(0L,
0L, 0L, 3L, 0L, 0L, 1L, 0L, 0L, 99L), `GTEX-11TT1-0526-SM-5P9JO` = c(0L,
1L, 2L, 4L, 0L, 0L, 2L, 0L, 0L, 52L), `GTEX-1KXAM-0726-SM-E9U5I` = c(0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 45L), `GTEX-18A67-2526-SM-7LG5Z` = c(1L,
0L, 2L, 0L, 0L, 0L, 1L, 0L, 0L, 91L), `GTEX-14BMU-1026-SM-5RQJ5` = c(1L,
0L, 1L, 8L, 0L, 0L, 0L, 0L, 0L, 47L), `GTEX-13PVR-2026-SM-73KXT` = c(0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 27L), `GTEX-1211K-1326-SM-5FQV2` = c(0L,
0L, 3L, 0L, 0L, 0L, 1L, 1L, 0L, 57L), `GTEX-11TT1-0626-SM-5GU4X` = c(1L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 90L), `GTEX-ZYFG-1826-SM-5GZWX` = c(0L,
0L, 3L, 2L, 0L, 0L, 2L, 0L, 0L, 91L), `GTEX-1KXAM-1926-SM-D3LAG` = c(0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 103L), `GTEX-18A67-2226-SM-7LT9Z` = c(0L,
0L, 2L, 2L, 0L, 0L, 1L, 0L, 1L, 157L), `GTEX-13PVR-1726-SM-5Q5EC` = c(1L,
0L, 2L, 0L, 0L, 0L, 1L, 0L, 0L, 34L), `GTEX-1211K-1826-SM-5EGJ2` = c(0L,
0L, 1L, 3L, 0L, 0L, 0L, 0L, 0L, 49L), `GTEX-11TT1-0926-SM-5GU5M` = c(0L,
2L, 0L, 3L, 1L, 0L, 0L, 0L, 1L, 49L), `GTEX-1KXAM-1026-SM-CY8IA` = c(0L,
0L, 1L, 3L, 0L, 0L, 0L, 0L, 0L, 93L), `GTEX-14BMU-1626-SM-5TDE7` = c(0L,
1L, 3L, 13L, 0L, 0L, 1L, 0L, 0L, 84L), `GTEX-13PVR-2226-SM-7DHKP` = c(0L,
0L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 75L), `GTEX-1211K-1926-SM-5EQLB` = c(0L,
1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 114L), `GTEX-11TT1-2126-SM-5GU5Y` = c(2L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 49L), `GTEX-ZT9W-2026-SM-51MRA` = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 70L), `GTEX-1KXAM-2326-SM-CYPTD` = c(0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 20L), `GTEX-18A67-0226-SM-7LG67` = c(0L,
0L, 5L, 2L, 0L, 0L, 1L, 0L, 0L, 94L), `GTEX-14BMU-2126-SM-5S2TS` = c(0L,
0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 50L), `GTEX-13PVR-2426-SM-5RQHN` = c(0L,
0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 59L), `GTEX-1211K-2226-SM-5FQU6` = c(0L,
0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 81L), `GTEX-11TT1-2426-SM-5EQMK` = c(0L,
1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 60L)), row.names = c("ENSG00000243485",
"ENSG00000237613", "ENSG00000186092", "ENSG00000238009", "ENSG00000222623",
"ENSG00000241599", "ENSG00000236601", "ENSG00000235146", "ENSG00000223181",
"ENSG00000237491"), class = "data.frame")

system is computationally singular : mlogit

When I am trying to add all variables in formula, I am getting this error. If I omit A then, the model runs fine.
dput(df1)
structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Choice = c(1L, 0L, 0L,
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L,
1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L,
1L), A = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, -1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, -1L, 0L, 0L, 1L, 0L, 0L, -1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), B = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, -1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, -1L, 0L, 1L, 0L, 0L, -1L, 0L, 0L),
C = c(1L, 0L, 0L, -1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, -1L, 0L, 0L, 0L, 1L,
0L, 0L, -1L, 0L, 0L, 0L, 0L, 0L, 0L), D = c(0L, 1L, 0L, 0L,
-1L, 0L, 0L, 1L, 0L, 0L, -1L, 0L, 0L, 1L, 0L, 0L, -1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), E = c(0L, 0L, 1L, 0L, 0L, -1L, 0L, 0L, 1L, 0L,
0L, -1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, -1L, 0L), F = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, -1L, 0L, 0L, 1L, 0L, 0L, -1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, -1L), Alternative = c(1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L)), row.names = c(NA, -36L), class = "data.frame")
> model = mlogit( Choice ~ A + B + C + D + E + F | 0, data = df1,
+
+ alt.var = 'Alternative',
+
+ shape = "long")
Error in solve.default(H, g[!fixed]) :
system is computationally singular: reciprocal condition number = 4.85723e-17
I have seen these two questions and the documentation however still not able to figure it out completely. Any help will be highly appreciated.
R mlogit model, computationally singular
Error in mlogit: Error in solve.default(H, g[!fixed]) : system is computationally singular: reciprocal condition number = 3.4767e-18

dist() function in R returning "Error in dist(x, method = "binary") : unused argument (method = "binary")?

I am trying to create a distance matrix using dist on categorical variables. I performed dummy.data.frame successfully on my categorical variables, but when I run dist() R returns this message:
Error in dist(dummyClusterdf, method = "binary") : unused argument (method = "binary")"
Has anyone else had this problem? Any help would be appreciated.
#dummify categorical variables
dummyClusterdf <- dummy.data.frame(data = clusterdf, sep = ".", dummy.classes = "ALL", all = FALSE)
glimpse(dummyClusterdf)
#distance matrix
surveydist <- dist(dummyClusterdf, method = "binary")
Error in dist(dummyClusterdf, method = "binary") : unused argument (method = "binary")
here is a sample of the data:
> dput(head(dummyClusterdf))
structure(list(`Age_Range.23 - 25` = c(0L, 0L, 0L, 1L, 0L, 0L
), `Age_Range.26 - 30` = c(1L, 0L, 0L, 0L, 0L, 0L), `Age_Range.30 +` = c(0L,
1L, 1L, 0L, 1L, 1L), User_Role.Faculty = c(0L, 1L, 1L, 0L, 0L,
1L), User_Role.Staff = c(1L, 0L, 0L, 1L, 1L, 0L), User_Role.Administration = c(0L,
0L, 0L, 0L, 0L, 0L), `Sustainability_Importance.Extremely Unimportant` = c(0L,
0L, 0L, 0L, 0L, 0L), Sustainability_Importance.Neutral = c(0L,
0L, 0L, 0L, 0L, 0L), `Sustainability_Importance.Very important` = c(0L,
1L, 1L, 0L, 0L, 1L), `Sustainability_Importance.Extremely important` = c(1L,
0L, 0L, 1L, 1L, 0L), `Self_rate_Sustainable_Lifestyle.Somewhat disagree` = c(0L,
0L, 0L, 0L, 0L, 0L), `Self_rate_Sustainable_Lifestyle.Neither agree nor disagree` = c(0L,
1L, 0L, 0L, 0L, 0L), `Self_rate_Sustainable_Lifestyle.Somewhat agree` = c(1L,
0L, 1L, 1L, 1L, 1L), `Self_rate_Sustainable_Lifestyle.Strongly agree` = c(0L,
0L, 0L, 0L, 0L, 0L), `School_Prioritizes_Sustainability.A little` = c(0L,
0L, 0L, 0L, 0L, 0L), `School_Prioritizes_Sustainability.A moderate amount` = c(1L,
0L, 1L, 0L, 0L, 0L), `School_Prioritizes_Sustainability.A lot` = c(0L,
0L, 0L, 0L, 0L, 0L), `School_Prioritizes_Sustainability.A great deal` = c(0L,
1L, 0L, 1L, 1L, 1L), Lights_on_Empty_Room.Never = c(0L, 0L, 0L,
0L, 0L, 0L), Lights_on_Empty_Room.Sometimes = c(0L, 0L, 1L, 0L,
0L, 0L), `Lights_on_Empty_Room.About half the time` = c(0L, 0L,
0L, 0L, 0L, 0L), `Lights_on_Empty_Room.Most of the time` = c(1L,
0L, 0L, 0L, 0L, 1L), Lights_on_Empty_Room.Always = c(0L, 1L,
0L, 1L, 1L, 0L), Look_kWh_on_Bill.Yes = c(1L, 1L, 1L, 1L, 0L,
1L), Look_kWh_on_Bill.No = c(0L, 0L, 0L, 0L, 1L, 0L), `Real_time_Updates_Reduce_Use.Strongly disagree` = c(0L,
0L, 0L, 0L, 0L, 0L), `Real_time_Updates_Reduce_Use.Somewhat disagree` = c(0L,
0L, 0L, 0L, 0L, 0L), `Real_time_Updates_Reduce_Use.Neither agree nor disagree` = c(0L,
0L, 0L, 0L, 0L, 0L), `Real_time_Updates_Reduce_Use.Somewhat agree` = c(1L,
0L, 1L, 0L, 0L, 0L), `Real_time_Updates_Reduce_Use.Strongly agree` = c(0L,
1L, 0L, 1L, 1L, 1L), `Electric_Cost_Importance.Slightly important` = c(0L,
0L, 0L, 0L, 0L, 0L), `Electric_Cost_Importance.Moderately important` = c(0L,
0L, 1L, 0L, 0L, 0L), `Electric_Cost_Importance.Very important` = c(1L,
0L, 0L, 1L, 1L, 1L), `Electric_Cost_Importance.Extremely important` = c(0L,
1L, 0L, 0L, 0L, 0L), `Electric_Environment_Importance.Slightly important` = c(0L,
0L, 0L, 0L, 0L, 0L), `Electric_Environment_Importance.Moderately important` = c(1L,
0L, 1L, 0L, 0L, 0L), `Electric_Environment_Importance.Very important` = c(0L,
0L, 0L, 1L, 0L, 1L), `Electric_Environment_Importance.Extremely important` = c(0L,
1L, 0L, 0L, 1L, 0L), Work_Can_Change_Thermostat.Yes = c(0L, 0L,
0L, 0L, 0L, 0L), Work_Can_Change_Thermostat.No = c(1L, 1L, 1L,
1L, 1L, 1L), `Peacock_VS_Campus_Efficiency.Not efficient at all` = c(0L,
0L, 0L, 0L, 0L, 0L), `Peacock_VS_Campus_Efficiency.Slightly efficient` = c(0L,
0L, 1L, 0L, 0L, 0L), `Peacock_VS_Campus_Efficiency.Moderately efficient` = c(1L,
1L, 0L, 1L, 1L, 0L), `Peacock_VS_Campus_Efficiency.Very efficient` = c(0L,
0L, 0L, 0L, 0L, 1L), `Peacock_VS_Campus_Efficiency.Extremely efficient` = c(0L,
0L, 0L, 0L, 0L, 0L), `Display_Useful.Not at all useful` = c(0L,
0L, 1L, 0L, 0L, 0L), `Display_Useful.Slightly useful` = c(1L,
0L, 0L, 0L, 0L, 0L), `Display_Useful.Moderately useful` = c(0L,
0L, 0L, 1L, 0L, 1L), `Display_Useful.Very useful` = c(0L, 1L,
0L, 0L, 1L, 0L), `Display_Useful.Extremely useful` = c(0L, 0L,
0L, 0L, 0L, 0L), `Display_Impact.None at all` = c(0L, 0L, 1L,
0L, 0L, 0L), `Display_Impact.A little` = c(1L, 0L, 0L, 0L, 0L,
1L), `Display_Impact.A moderate amount` = c(0L, 1L, 0L, 1L, 1L,
0L), `Display_Impact.A lot` = c(0L, 0L, 0L, 0L, 0L, 0L), Gender.Male = c(0L,
1L, 0L, 0L, 0L, 1L), Gender.Female = c(1L, 0L, 1L, 1L, 1L, 0L
), `Gender.Prefer not to answer` = c(0L, 0L, 0L, 0L, 0L, 0L),
`Race/Ethnicity.I would prefer not to answer` = c(0L, 0L,
0L, 0L, 0L, 0L), `Race/Ethnicity.Other` = c(1L, 0L, 0L, 0L,
0L, 0L), `Race/Ethnicity.Asian/Asian American` = c(0L, 1L,
0L, 0L, 0L, 0L), `Race/Ethnicity.Hispanic or Latino` = c(0L,
0L, 0L, 0L, 0L, 0L), `Race/Ethnicity.White or Caucasian` = c(0L,
0L, 1L, 1L, 1L, 1L), `Prize_Participation.Yes please!` = c(1L,
1L, 1L, 1L, 1L, 1L), `Prize_Participation.No thanks, I'd like to be finished` = c(0L,
0L, 0L, 0L, 0L, 0L)), dummies = list(Age_Range = 1:3, User_Role = 4:6,
Sustainability_Importance = 7:10, Self_rate_Sustainable_Lifestyle = 11:14,
School_Prioritizes_Sustainability = 15:18, Lights_on_Empty_Room = 19:23,
Look_kWh_on_Bill = 24:25, Real_time_Updates_Reduce_Use = 26:30,
Electric_Cost_Importance = 31:34, Electric_Environment_Importance = 35:38,
Work_Can_Change_Thermostat = 39:40, Peacock_VS_Campus_Efficiency = 41:45,
Display_Useful = 46:50, Display_Impact = 51:54, Gender = 55:57,
`Race/Ethnicity` = 58:62, Prize_Participation = 63:64), row.names = c("1",
"2", "3", "4", "5", "6"), class = "data.frame")
Mankind_008 answered the question! (Solved by using stats::dist)

Factor analysis not assigning to object

I am running an exploratory factor analysis and am specifying that the factor analysis results be loaded onto f2.
My code is below. The first line runs but when I go to run my next line of code, where I reference f2, I get a message "object 'f2' not found".
So somehow the assignment isn't working. Am I missing something really obvious? The factor analysis uses the psych package. I have checked the documentation and the examples they give assign factor analysis results to objects similarly, so I'm a bit stumped. Any pointers would be greatly appreciated.
f2 <- fa(MDI, nfactors = 30, n.obs = 286, rotation = "promax", min.err = 0.001,
fm = "wls", cor = "poly")
load = loadings(f2)
print(load, sort = TRUE, digits = 2, cutoff = 0.001)
plot(load)
identify(load, labels = names(MDI))
plot(f2, labels = names(MDI))
Below is dput of the correlation matrix so it can be reproducible.
> dput(MDI)
structure(list(X = structure(c(1L, 12L, 23L, 25L, 26L, 27L, 28L,
29L, 30L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L), .Label = c("mdi1",
"mdi10", "mdi11", "mdi12", "mdi13", "mdi14", "mdi15", "mdi16",
"mdi17", "mdi18", "mdi19", "mdi2", "mdi20", "mdi21", "mdi22",
"mdi23", "mdi24", "mdi25", "mdi26", "mdi27", "mdi28", "mdi29",
"mdi3", "mdi30", "mdi4", "mdi5", "mdi6", "mdi7", "mdi8", "mdi9"
), class = "factor"), mdi1 = c(1, 0.3936173, 0.4689895, 0.3723157,
0.5000249, 0.4007723, 0.4752565, 0.3299713, 0.4467554, 0.3092035,
0.4587867, 0.3502716, 0.4765846, 0.4296331, 0.4279444, 0.3978138,
0.3130349, 0.4217768, 0.4217768, 0.3941672, 0.442678, 0.4143811,
0.4883476, 0.3818616, 0.4253592, 0.4220679, 0.3341886, 0.2904774,
0.4125257, 0.471747), mdi2 = c(0.3936173, 1, 0.5687221, 0.5503958,
0.3579519, 0.5524491, 0.4113922, 0.5692459, 0.5244077, 0.4519596,
0.4691271, 0.3328134, 0.3909096, 0.5207473, 0.517872, 0.4460308,
0.5357907, 0.5271745, 0.5656711, 0.6416844, 0.5933588, 0.5301733,
0.4632707, 0.3692169, 0.3899222, 0.6722428, 0.571866, 0.4770556,
0.4447538, 0.4801506), mi3 = c(0.4689895, 0.5687221, 1, 0.5337568,
0.4605771, 0.5801225, 0.4748034, 0.5027398, 0.6493962, 0.4371995,
0.4814276, 0.5174168, 0.4575024, 0.5161417, 0.7302354, 0.443922,
0.4503891, 0.6403537, 0.4638545, 0.5755256, 0.6559644, 0.5669309,
0.4882669, 0.540937, 0.4305827, 0.6208696, 0.5774123, 0.4337106,
0.5007241, 0.5812161), mdi4 = c(0.3723157, 0.5503958, 0.5337568,
1, 0.4647914, 0.5432398, 0.5222107, 0.480119, 0.5095777, 0.6127156,
0.5411, 0.3438971, 0.5132041, 0.4708869, 0.5773365, 0.640864,
0.5419371, 0.4278067, 0.4294387, 0.5762886, 0.6173602, 0.696368,
0.4768973, 0.4312955, 0.4800561, 0.4966285, 0.4940535, 0.6449681,
0.4448795, 0.4511662), mdi5 = c(0.5000249, 0.3579519, 0.4605771,
0.4647914, 1, 0.529076, 0.4627546, 0.4322669, 0.5266816, 0.4193437,
0.5274925, 0.355744, 0.4748984, 0.3943626, 0.5452649, 0.4282942,
0.3884295, 0.4405988, 0.5013351, 0.4372788, 0.5033538, 0.449599,
0.4719872, 0.5221968, 0.4572666, 0.478281, 0.4474777, 0.436465,
0.6423572, 0.5367758), mdi6 = c(0.4007723, 0.5524491, 0.5801225,
0.5432398, 0.529076, 1, 0.5481986, 0.5292259, 0.554901, 0.5197566,
0.6618723, 0.7776074, 0.5538152, 0.5513789, 0.6005339, 0.5479274,
0.5154317, 0.7863542, 0.5508043, 0.6893594, 0.5754793, 0.6092226,
0.5590816, 0.7415316, 0.5702535, 0.7306869, 0.6161738, 0.5381451,
0.5487092, 0.7739138), mdi7 = c(0.4752565, 0.4113922, 0.4748034,
0.5222107, 0.4627546, 0.5481986, 1, 0.4208138, 0.5818144, 0.5123467,
0.5154297, 0.5182619, 0.6342495, 0.5054629, 0.5154232, 0.550466,
0.4921495, 0.4687262, 0.4268752, 0.56955, 0.5046245, 0.5563658,
0.544639, 0.4961694, 0.6133689, 0.5084869, 0.4299369, 0.5220904,
0.4294706, 0.4739928), mdi8 = c(0.3299713, 0.5692459, 0.5027398,
0.480119, 0.4322669, 0.5292259, 0.4208138, 1, 0.5723092, 0.3119612,
0.4675261, 0.3380419, 0.353584, 0.4541021, 0.5421594, 0.474245,
0.6079999, 0.5235899, 0.5335896, 0.4957257, 0.5442768, 0.4974862,
0.5023641, 0.4025193, 0.4116167, 0.5727691, 0.6270551, 0.4208924,
0.5064872, 0.4869765), mdi9 = c(0.4467554, 0.5244077, 0.6493962,
0.5095777, 0.5266816, 0.554901, 0.5818144, 0.5723092, 1, 0.490689,
0.5181651, 0.4850319, 0.5356553, 0.6158338, 0.6559043, 0.5307519,
0.5871398, 0.5846594, 0.5470594, 0.6284931, 0.6879265, 0.6163574,
0.512804, 0.5047779, 0.5466345, 0.613476, 0.5883362, 0.506302,
0.4979364, 0.5808664), mdi10 = c(0.3092035, 0.4519596, 0.4371995,
0.6127156, 0.4193437, 0.5197566, 0.5123467, 0.3119612, 0.490689,
1, 0.5118569, 0.4462334, 0.4532171, 0.4315306, 0.4522421, 0.7588315,
0.4401081, 0.4350122, 0.4499751, 0.5206525, 0.5352926, 0.6998865,
0.4755847, 0.4100334, 0.4787971, 0.453012, 0.4734884, 0.7813307,
0.535401, 0.4395691), mdi11 = c(0.4587867, 0.4691271, 0.4814276,
0.5411, 0.5274925, 0.6618723, 0.5154297, 0.4675261, 0.5181651,
0.5118569, 1, 0.5527827, 0.501612, 0.5149324, 0.540138, 0.5069535,
0.528538, 0.579202, 0.5901564, 0.5299329, 0.5250493, 0.5154014,
0.5127527, 0.5788864, 0.5161334, 0.507289, 0.5158013, 0.5344951,
0.5978617, 0.647102), mdi12 = c(0.3502716, 0.3328134, 0.5174168,
0.3438971, 0.355744, 0.7776074, 0.5182619, 0.3380419, 0.4850319,
0.4462334, 0.5527827, 1, 0.5154903, 0.4620118, 0.472202, 0.4547294,
0.4840607, 0.8285936, 0.5358637, 0.5490359, 0.4616615, 0.4249117,
0.4808445, 0.7267043, 0.552395, 0.5925844, 0.4751664, 0.3929154,
0.4023344, 0.6972608), mdi13 = c(0.4765846, 0.3909096, 0.4575024,
0.5132041, 0.4748984, 0.5538152, 0.6342495, 0.353584, 0.5356553,
0.4532171, 0.501612, 0.5154903, 1, 0.517566, 0.5669901, 0.4811508,
0.4159442, 0.5916418, 0.5317423, 0.6019275, 0.5150589, 0.6103914,
0.7036702, 0.5396378, 0.7106897, 0.5663952, 0.4743636, 0.5357141,
0.4997759, 0.5310849), mdi14 = c(0.4296331, 0.5207473, 0.5161417,
0.4708869, 0.3943626, 0.5513789, 0.5054629, 0.4541021, 0.6158338,
0.4315306, 0.5149324, 0.4620118, 0.517566, 1, 0.5294991, 0.4641652,
0.5595265, 0.5568292, 0.5329007, 0.6300983, 0.5476386, 0.6433976,
0.5124435, 0.5178623, 0.4634521, 0.6025023, 0.4958374, 0.4741311,
0.4363587, 0.5922855), mdi15 = c(0.4279444, 0.517872, 0.7302354,
0.5773365, 0.5452649, 0.6005339, 0.5154232, 0.5421594, 0.6559043,
0.4522421, 0.540138, 0.472202, 0.5669901, 0.5294991, 1, 0.4969139,
0.5407898, 0.711619, 0.5776979, 0.6465131, 0.8102714, 0.6210396,
0.5843541, 0.5708951, 0.5353925, 0.6430862, 0.6773275, 0.5256203,
0.5525755, 0.5968413), mdi16 = c(0.3978138, 0.4460308, 0.443922,
0.640864, 0.4282942, 0.5479274, 0.550466, 0.474245, 0.5307519,
0.7588315, 0.5069535, 0.4547294, 0.4811508, 0.4641652, 0.4969139,
1, 0.5327385, 0.4919386, 0.4640105, 0.6189401, 0.6221125, 0.7849677,
0.5263167, 0.4961512, 0.546515, 0.4792591, 0.5387354, 0.7593365,
0.5443296, 0.4965095), mdi17 = c(0.3130349, 0.5357907, 0.4503891,
0.5419371, 0.3884295, 0.5154317, 0.4921495, 0.6079999, 0.5871398,
0.4401081, 0.528538, 0.4840607, 0.4159442, 0.5595265, 0.5407898,
0.5327385, 1, 0.5972664, 0.6531257, 0.6124992, 0.6593416, 0.5779083,
0.54295, 0.5091556, 0.4999436, 0.5534753, 0.6906962, 0.5515724,
0.5763667, 0.5257787), mdi18 = c(0.4217768, 0.5271745, 0.6403537,
0.4278067, 0.4405988, 0.7863542, 0.4687262, 0.5235899, 0.5846594,
0.4350122, 0.579202, 0.8285936, 0.5916418, 0.5568292, 0.711619,
0.4919386, 0.5972664, 1, 0.616892, 0.6776108, 0.653145, 0.6462477,
0.6133169, 0.76014, 0.5693621, 0.7255007, 0.6772965, 0.4731968,
0.6136029, 0.8215128), mdi19 = c(0.4217768, 0.5656711, 0.4638545,
0.4294387, 0.5013351, 0.5508043, 0.4268752, 0.5335896, 0.5470594,
0.4499751, 0.5901564, 0.5358637, 0.5317423, 0.5329007, 0.5776979,
0.4640105, 0.6531257, 0.616892, 1, 0.6277485, 0.5888859, 0.5781749,
0.5962161, 0.4966702, 0.525626, 0.5893053, 0.5937545, 0.4536458,
0.6023978, 0.595442), mdi20 = c(0.3941672, 0.6416844, 0.5755256,
0.5762886, 0.4372788, 0.6893594, 0.56955, 0.4957257, 0.6284931,
0.5206525, 0.5299329, 0.5490359, 0.6019275, 0.6300983, 0.6465131,
0.6189401, 0.6124992, 0.6776108, 0.6277485, 1, 0.7456018, 0.7237394,
0.5914482, 0.6174089, 0.6084753, 0.7583413, 0.6222094, 0.6093335,
0.5607048, 0.6734581), mdi21 = c(0.442678, 0.5933588, 0.6559644,
0.6173602, 0.5033538, 0.5754793, 0.5046245, 0.5442768, 0.6879265,
0.5352926, 0.5250493, 0.4616615, 0.5150589, 0.5476386, 0.8102714,
0.6221125, 0.6593416, 0.653145, 0.5888859, 0.7456018, 1, 0.6836611,
0.5767896, 0.5020915, 0.5302819, 0.6788499, 0.7302629, 0.5532899,
0.514876, 0.5369505), mdi22 = c(0.4143811, 0.5301733, 0.5669309,
0.696368, 0.449599, 0.6092226, 0.5563658, 0.4974862, 0.6163574,
0.6998865, 0.5154014, 0.4249117, 0.6103914, 0.6433976, 0.6210396,
0.7849677, 0.5779083, 0.6462477, 0.5781749, 0.7237394, 0.6836611,
1, 0.6970878, 0.5551017, 0.5309829, 0.669629, 0.618117, 0.7725701,
0.5368407, 0.6161932), mdi23 = c(0.4883476, 0.4632707, 0.4882669,
0.4768973, 0.4719872, 0.5590816, 0.544639, 0.5023641, 0.512804,
0.4755847, 0.5127527, 0.4808445, 0.7036702, 0.5124435, 0.5843541,
0.5263167, 0.54295, 0.6133169, 0.5962161, 0.5914482, 0.5767896,
0.6970878, 1, 0.5780347, 0.5722519, 0.6007872, 0.6153544, 0.579486,
0.5826146, 0.5400445), mdi24 = c(0.3818616, 0.3692169, 0.540937,
0.4312955, 0.5221968, 0.7415316, 0.4961694, 0.4025193, 0.5047779,
0.4100334, 0.5788864, 0.7267043, 0.5396378, 0.5178623, 0.5708951,
0.4961512, 0.5091556, 0.76014, 0.4966702, 0.6174089, 0.5020915,
0.5551017, 0.5780347, 1, 0.5823804, 0.6163705, 0.5893418, 0.4599374,
0.4879448, 0.7233099), mdi25 = c(0.4253592, 0.3899222, 0.4305827,
0.4800561, 0.4572666, 0.5702535, 0.6133689, 0.4116167, 0.5466345,
0.4787971, 0.5161334, 0.552395, 0.7106897, 0.4634521, 0.5353925,
0.546515, 0.4999436, 0.5693621, 0.525626, 0.6084753, 0.5302819,
0.5309829, 0.5722519, 0.5823804, 1, 0.6287262, 0.4351833, 0.541565,
0.488426, 0.5750416), mdi26 = c(0.4220679, 0.6722428, 0.6208696,
0.4966285, 0.478281, 0.7306869, 0.5084869, 0.5727691, 0.613476,
0.453012, 0.507289, 0.5925844, 0.5663952, 0.6025023, 0.6430862,
0.4792591, 0.5534753, 0.7255007, 0.5893053, 0.7583413, 0.6788499,
0.669629, 0.6007872, 0.6163705, 0.6287262, 1, 0.6485229, 0.6028357,
0.5814636, 0.6474577), mdi27 = c(0.3341886, 0.571866, 0.5774123,
0.4940535, 0.4474777, 0.6161738, 0.4299369, 0.6270551, 0.5883362,
0.4734884, 0.5158013, 0.4751664, 0.4743636, 0.4958374, 0.6773275,
0.5387354, 0.6906962, 0.6772965, 0.5937545, 0.6222094, 0.7302629,
0.618117, 0.6153544, 0.5893418, 0.4351833, 0.6485229, 1, 0.5630586,
0.5338222, 0.5340758), mdi28 = c(0.2904774, 0.4770556, 0.4337106,
0.6449681, 0.436465, 0.5381451, 0.5220904, 0.4208924, 0.506302,
0.7813307, 0.5344951, 0.3929154, 0.5357141, 0.4741311, 0.5256203,
0.7593365, 0.5515724, 0.4731968, 0.4536458, 0.6093335, 0.5532899,
0.7725701, 0.579486, 0.4599374, 0.541565, 0.6028357, 0.5630586,
1, 0.5835477, 0.5081215), mdi29 = c(0.4125257, 0.4447538, 0.5007241,
0.4448795, 0.6423572, 0.5487092, 0.4294706, 0.5064872, 0.4979364,
0.535401, 0.5978617, 0.4023344, 0.4997759, 0.4363587, 0.5525755,
0.5443296, 0.5763667, 0.6136029, 0.6023978, 0.5607048, 0.514876,
0.5368407, 0.5826146, 0.4879448, 0.488426, 0.5814636, 0.5338222,
0.5835477, 1, 0.611525), mdi30 = c(0.471747, 0.4801506, 0.5812161,
0.4511662, 0.5367758, 0.7739138, 0.4739928, 0.4869765, 0.5808664,
0.4395691, 0.647102, 0.6972608, 0.5310849, 0.5922855, 0.5968413,
0.4965095, 0.5257787, 0.8215128, 0.595442, 0.6734581, 0.5369505,
0.6161932, 0.5400445, 0.7233099, 0.5750416, 0.6474577, 0.5340758,
0.5081215, NA, 1)), class = "data.frame", row.names = c(NA, -30L
))
I was able to use the raw data as input for MDI and the factor analysis ran with error messages (in comment below so that this post isn't super long). The actual dataset is really big but I created a smaller dataset from it that generates the same error message when I run the factor analysis. THe dput for that smaller data set is below.
>dput(MDI)
structure(list(MDIdisengagement = c(2L, 1L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 3L,
1L, 4L, 2L, 0L, 1L, 0L, 2L, 4L, 2L, 0L, 0L, 1L, 1L, 0L, 2L, 1L,
1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 2L, 0L, 0L, 0L,
1L, 4L, 1L, 0L, 3L, 2L, 1L, 0L, 0L, 2L, 0L, 1L, 1L, 2L, 4L, 7L,
0L, 0L, 0L, 1L, 3L, 0L, 2L, 0L, 7L, 0L, 4L, 0L, 1L, 0L, 0L, 0L,
1L, 1L, 1L, 2L, 2L, 0L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
0L, 0L, 0L, 3L, 2L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 2L,
0L, 4L, 2L, 4L, 2L, 3L, 1L, 1L, 2L, 4L, 3L, 0L, 2L, 4L, 1L, 1L,
1L, 0L, 1L, 0L, 1L, 3L, 1L, 4L,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2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 7L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 7L, 0L, 0L, 1L, 1L, 0L, 0L,
0L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
2L, 0L, 0L, 3L, 1L, 0L, 0L, 4L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 2L,
0L, 0L)), class = "data.frame", row.names = c(NA, -291L))
You have a few problems going on in your call to fa. First you need to remove the X column to provide a proper correlation matrix. This may not be an issue for you but of course is for your dput example. However even after fixing this the main issue is with your call to polychoric correlation. If you want to specify cor = "poly" you need to use the discrete data.
What I would suggest is to run your call to fa using the discrete data with only the measurement variables and no extra variables like X. If you are still getting errors then please update your example to include the discrete data and I can try to help you.
EDIT: So now your big problem is that you only have 6 items and in factor analysis you usually want a minimum of three items per factor which in your case would leave you with at most 2 factors but even that is pushing it. With nfactors=1 it will work but not with nfactors>1 To see why you are having issues you can look at the eigenvalues of your correlation matrix like so:
eigen(polychoric(MDI)$rho)
This produces the following output:
$values
[1] 4.76730952 0.68445132 0.28440838 0.18855401 0.05141578 0.02386099
As you can see, nearly all of the variance is being explained by the first factor and there is little left after. Therefore, it is not surprising that you are getting errors when trying to extract more than one factor. A scree plot will visually show this.
So this will work but the results are probably not very interesting:
f2 <- fa(MDI,nfactors=1,rotation="promax", min.err= 0.001, fm="wls", cor="poly")
Does that make sense?

failed without any error message while replacing part of names of columns with other names in dataframe

I have a dataframe which has column names as shown in the dput data below.
structure(list(mosales = c(1L, 1L, 1L, 12L, 1L), sale123 = c(14.86,
8.97, 6.44, 463.61, 15.94), totsales = c(1L, 1L, 1L,
30L, 1L), totqty = c(1L, 1L, 1L, 34L, 2L), unqsales = c(1L,
1L, 1L, 6L, 2L), x1_rank_1 = c(1L, 1L, 1L, 0L, 1L), x1_rank_4 = c(0L,
0L, 0L, 1L, 0L), x1_rank_3 = c(0L, 0L, 0L, 0L, 0L), x1_rank_2 = c(0L,
0L, 0L, 0L, 0L), x2_rank_2 = c(1L, 1L, 0L, 0L, 1L), x2_rank_1 = c(0L,
0L, 1L, 0L, 0L), x2_rank_5 = c(0L, 0L, 0L, 1L, 0L), x2_rank_4 = c(0L,
0L, 0L, 0L, 0L), x2_rank_3 = c(0L, 0L, 0L, 0L, 0L), x3_rank_1 = c(1L,
1L, 1L, 0L, 1L), x3_rank_4 = c(0L, 0L, 0L, 1L, 0L), x3_rank_3 = c(0L,
0L, 0L, 0L, 0L), x3_rank_2 = c(0L, 0L, 0L, 0L, 0L), x4_rank_1 = c(1L,
1L, 1L, 0L, 0L), x4_rank_5 = c(0L, 0L, 0L, 1L, 0L), x4_rank_2 = c(0L,
0L, 0L, 0L, 1L), x4_rank_4 = c(0L, 0L, 0L, 0L, 0L), x4_rank_3 = c(0L,
0L, 0L, 0L, 0L), x5_rank_1 = c(1L, 1L, 1L, 0L, 0L), x5_rank_4 = c(0L,
0L, 0L, 1L, 0L), x5_rank_2 = c(0L, 0L, 0L, 0L, 1L), x5_rank_3 = c(0L,
0L, 0L, 0L, 0L)), row.names = c(36L, 41L, 72L, 79L, 137L), class = "data.frame")
What I am trying to do is to replace columns names, which start with x1_rank, x2_rank .. and so forth, by the names such that "x1_rank" should be replaced with mosales, "x2_rank" should be replaced with sales123, "x3_rank" should be replaced with "totsales", "x4_rank" should be replaced with "totqty" and "x5_rank" should be replaced with "unqsales".
So the final column names would look like:
mosales, sale123, totsales, totqty, unqsales, mosales_1, mosales_2,... sale123_1, sale123_2,... totsales_1, totsales_2,... totqty_1, totqty_2,... unqsales_1, unqsales_2,...
I tried using a for loop and gsub, as in below. This code ran without any errors but I didn't get what I was looking for. Not sure where is the error.
df1 <- df
z <- names(df)
for (i in 1:length(z)){
gsub(paste0("x",i,"_rank"), z[i], names(df1))
}
df is dataframe which can be created using above dput data.
String manipulation functions in the stringi package are vectorized over pattern, replacement, and (optionally) string. This is very convenient for your case:
library(stringi)
orig <- c(stri_c("x", 1:5, "_rank"))
repl <- c("mosales", "sales123", "totsales", "totqty", "unqsales")
names(df) <- stri_replace_all_fixed(names(df), orig, repl,
vectorize_all = FALSE)
You can use this reg \\bx1_rank[a-zA-Z]*.
This will match x1_rank at the start of the string.
gsub lets you replace the found pattern with what you want. Repeat for all the cases you need.
In a loop:
repl <- c("mosales", "sales123", "totsales", "totqty", "unqsales")
for (i in 1:5) {
p = paste0("\\b^x", i, "_rank[a-zA-Z]*")
colnames(d) = gsub(pattern=p, repl[i], colnames(d))
}
colnames(d)
#
# [1] "mosales" "sale123" "totsales" "totqty" "unqsales" "mosales_1" "mosales_4"
# [8] "mosales_3" "mosales_2" "sales123_2" "sales123_1" "sales123_5" "sales123_4" "sales123_3"
# [15] "totsales_1" "totsales_4" "totsales_3" "totsales_2" "totqty_1" "totqty_5" "totqty_2"
# [22] "totqty_4" "totqty_3" "unqsales_1" "unqsales_4" "unqsales_2" "unqsales_3"

Resources