|
1 | 1 | # Takes a HOC name and replaces that constructs relationships with the dimensions of the HOC |
2 | 2 | substitute_dimensions_for_HOC <- function(construct, sm, mm) { |
3 | 3 | # Identify dimensions of HOCs |
4 | | - dimensions <- mm[mm[, "type"] == "HOCA" | mm[, "type"] == "HOCB", ][mm[mm[, "type"] == "HOCB" | mm[, "type"] == "HOCA", ][, "construct"] == construct, "measurement"] |
| 4 | + dimensions <- matrix(construct, ncol = 3, byrow = TRUE)[,2] |
| 5 | + #dimensions <- mm[mm[, "type"] == "HOCA" | mm[, "type"] == "HOCB", ][mm[mm[, "type"] == "HOCB" | mm[, "type"] == "HOCA", ][, "construct"] == construct, "measurement"] |
5 | 6 | # identify antecedent relationships to HOC |
6 | | - antecedents <- sm[which(sm[, "target"] == construct), "source"] |
| 7 | + antecedents <- sm[which(sm[, "target"] == construct[1]), "source"] |
7 | 8 | # change antecedent relationship to first order constructs in structural model |
8 | 9 | if (!length(antecedents) == 0) { |
9 | 10 | sm <- rbind(sm, |
10 | 11 | relationships(paths(from = antecedents, |
11 | 12 | to = dimensions))) |
12 | | - sm <- sm[-which(sm[, "target"] == construct), ] |
| 13 | + sm <- sm[-which(sm[, "target"] == construct[1]), ] |
13 | 14 | } |
14 | 15 |
|
15 | 16 | # identify outcomes |
16 | | - outcomes <- sm[which(sm[, "source"] == construct), "target"] |
| 17 | + outcomes <- sm[which(sm[, "source"] == construct[1]), "target"] |
17 | 18 | if (!length(outcomes) == 0) { |
18 | 19 | sm <- rbind(sm, |
19 | 20 | relationships(paths(from = dimensions, |
20 | 21 | to = outcomes))) |
21 | | - sm <- sm[-which(sm[, "source"] == construct), ] |
| 22 | + sm <- sm[-which(sm[, "source"] == construct[1]), ] |
22 | 23 | } |
23 | | - return(sm) |
| 24 | + return(list(sm = sm, |
| 25 | + dimensions = dimensions)) |
24 | 26 | } |
25 | 27 |
|
26 | 28 | remove_HOC_in_measurement_model <- function(construct, mm) { |
27 | 29 | mm[!mm[, "construct"] == construct, ] |
28 | 30 | } |
29 | 31 |
|
30 | 32 | # Function to parse measurement and structural model and create the higher order model with complete information |
31 | | -prepare_higher_order_model <- function(data, sm , mm, ints, inners) { |
| 33 | +prepare_higher_order_model <- function(data, sm , mm, inners, HOCs) { |
32 | 34 | #retain the mm and sm |
33 | 35 | orig_mm <- mm |
| 36 | + new_mm <- matrix(unlist(mm[!(substr(names(mm), nchar(names(mm))-10, nchar(names(mm))) == "interaction") & !(names(mm) == "higher_order_composite")]), ncol = 3, byrow = TRUE, |
| 37 | + dimnames = list(NULL, c("construct", "measurement", "type"))) |
34 | 38 | orig_sm <- sm |
35 | | - # Identify HOCs |
36 | | - HOCs <- unique(mm[which(mm[, "type"] == "HOCA" | mm[, "type"] == "HOCB"), "construct"]) |
37 | | - |
38 | 39 | # Rebuild model for first stage |
39 | 40 | # Add new HOC paths to SM |
| 41 | + dimensions <- c() |
40 | 42 | for (construct in HOCs) { |
41 | | - sm <- substitute_dimensions_for_HOC(construct, sm, mm) |
42 | | - #mm <- remove_HOC_in_measurement_model(construct,mm) |
| 43 | + obj <- substitute_dimensions_for_HOC(construct, sm, new_mm) |
| 44 | + sm <- obj$sm |
| 45 | + dimensions <- c(dimensions, obj$dimensions) |
43 | 46 | } |
| 47 | + # Remove interactions from the sm |
| 48 | + sm <- sm[sm[, "source"] %in% unique(new_mm[, "construct"]),] |
| 49 | + |
| 50 | + |
44 | 51 | # Identify all the dimensions |
45 | | - dimensions <- orig_mm[which(orig_mm[, "construct"] == HOCs), "measurement"] |
46 | | - # remove HOCs from mm |
47 | | - new_mm <- mm[-which(mm[, "construct"] == HOCs), ] |
| 52 | + # dimensions <- orig_mm[which(orig_mm[, "construct"] == HOCs), "measurement"] |
48 | 53 |
|
49 | 54 | # Run first stage |
50 | 55 | new_model <- estimate_pls(data = data, |
51 | | - measurement_model = new_mm, |
52 | | - interactions = ints, |
| 56 | + measurement_model = mm[!(substr(names(mm), nchar(names(mm))-10, nchar(names(mm))) == "interaction") & !(names(mm) == "higher_order_composite")], |
53 | 57 | structural_model = sm, |
54 | 58 | inner_weights = inners) |
55 | 59 |
|
56 | 60 | # Add the construct scores to data |
57 | 61 | data <- cbind(data, new_model$construct_scores[, dimensions]) |
58 | 62 |
|
59 | | - # Update the mm to include the type of the new data and item |
60 | | - mm[mm[,"type"] == "HOCA", "type"] <- "A" |
61 | | - mm[mm[,"type"] == "HOCB", "type"] <- "B" |
| 63 | + # # Update the mm to include the type of the new data and item |
| 64 | + # mm[mm[,"type"] == "HOCA", "type"] <- "A" |
| 65 | + # mm[mm[,"type"] == "HOCB", "type"] <- "B" |
62 | 66 |
|
63 | 67 |
|
64 | 68 | # pass the updated mm, sm and data back to estimate_model() |
|
0 commit comments