Creating a fractional factorial design in R without prohibited pairs - r

I'm trying to write R code for a choice-based conjoint study.
I can create a factorial design using AlgDesign or conjoint - however, there are combinations of attribute levels that should not be together
Using an example from the web:
#Creating a full factorial design
library(AlgDesign)
ffd <- gen.factorial(c(2,2,4), varNames=c("Discount","Amount","Price"), factors="all")
ffd
Discount Amount Price
1 1 1 1
2 2 1 1
3 1 2 1
4 2 2 1
5 1 1 2
6 2 1 2
7 1 2 2
8 2 2 2
9 1 1 3
10 2 1 3
11 1 2 3
12 2 2 3
13 1 1 4
14 2 1 4
15 1 2 4
16 2 2 4
But what if "Discount" 2 ("no discount") should never be paired with "Amount" 1 ("20% discount")
Is there a way to tell AlgDesign or conjoint or some other factorial design to remove any prohibited pairs from the design?
Any advice would be appreciated.

You could always generate ffd as you did there, and then remove rows which meet your criteria, e.g. ffd$Discount == 2 & ffd$Amount==1 . The easy-ish way is to keep all the rows which do not meet the condition:
ffd<-ffd[(ffd$Discount != 2 | ffd$Amount != 1),]
Repeat for each condition you want to reject.

Related

How to use an if statement to fill two columns related to number of occurencies of interested values [duplicate]

I have a data set which looks something like
data<-c(0,1,2,3,4,2,3,1,4,3,2,4,0,1,2,0,2,1,2,0,4)
frame<-as.data.frame(data)
I now want to create a new variable within this data frame. If the column "data" reports a number of 2 or more, I want it to have "2" in that row, and if there is a 1 or 0 (e.g. the first two observations), I want the new variable to have a "1" for that observation.
I am trying to do this using the following code:
frame$twohouses<- if (any(frame$data>=2)) {frame$twohouses=2} else {frame$twohouses=1}
However if I run these 3 lines of script, every observation in the column "twohouses" is coded with a 2. However a number of them should be coded with a 1.
So my question: what am I doing wrong with my if else line or script? Or is there an alternative way to do this.
My question is similar to this one:
Using ifelse on factor in R
ut no one has answered that question.
Use ifelse:
frame$twohouses <- ifelse(frame$data>=2, 2, 1)
frame
data twohouses
1 0 1
2 1 1
3 2 2
4 3 2
5 4 2
...
16 0 1
17 2 2
18 1 1
19 2 2
20 0 1
21 4 2
The difference between if and ifelse:
if is a control flow statement, taking a single logical value as an argument
ifelse is a vectorised function, taking vectors as all its arguments.
The help page for if, accessible via ?"if" will also point you to ?ifelse
Try this
frame$twohouses <- ifelse(frame$data>1, 2, 1)
frame
data twohouses
1 0 1
2 1 1
3 2 2
4 3 2
5 4 2
6 2 2
7 3 2
8 1 1
9 4 2
10 3 2
11 2 2
12 4 2
13 0 1
14 1 1
15 2 2
16 0 1
17 2 2
18 1 1
19 2 2
20 0 1
21 4 2

How to generate permutation in data.frame format in R?

I am preparing a questionnaire to ask which transport mode do the respondents use in different conditions in terms of its travel time and cost.
There are three transport modes, two levels of travel time and three levels of travel cost as below:
mode <- c(1:3)
time <- c(1:2)
cost <- c(1:3)
I would generate all combinations of travel time and cost by transport mode but do not know how to generate it easily in R.
In the questionnaire, it shows three modes in one pair of modes with different conditions like the example below. comb indicates combination number of each pair of modes.
comb mode time cost
1 1 1 1
1 2 1 1
1 3 1 1
2 1 1 1
2 2 2 1
2 3 2 1
3 1 2 1
3 2 1 2
3 3 1 1
4 1 1 3
4 2 2 3
4 3 1 1
5 1 1 2
5 2 2 1
5 3 1 3
6 1 1 1
6 2 1 1
6 3 1 1
7 1 1 1
7 2 1 1
7 3 1 1
8 1 1 1
8 2 1 1
8 3 1 1
..... continues till fulfilling all combinations
I used expand.grid() but it returns just 18 combinations of mode, time and cost (3*2*3) without taking permutation by a pair of transport mode into account. I also tried several permutation functions but it may not bring my desired result. I prefer to make it in a data.frame with grouping variable such as comb in the example.
Permute groups in a data.frame R
How to calculate permutations of group labels with R?
It would be highly appreciated to generate all combinations simply..

R - Count duplicates values for each row

I'm working on a data frame that requires to calculate Fleiss's Kappa for inter-rater agreements. I'm using the 'irr' package for that.
Besides that, I need to count, for each observation, how many of raters are in agreement.
My data looks like these:
a b c
1 1 1 1
2 1 2 2
3 2 3 2
4 3 3 1
5 4 2 1
I'm expecting something like this, , where count stands for number of raters on agreement
a b c count
1 1 1 1 3
2 1 2 2 2
3 2 3 2 2
4 3 3 1 2
5 4 2 1 0
Thanks a lot.
Alternative solution if your data is in a data frame called abc:
as.numeric(apply(abc,1,function(x) {
ux<-unique(x);
tab <- tabulate(match(x, ux));
mode <- ux[tab == max(tab)];
ifelse(length(mode)==1,length(which(x==mode)),NA_character_);
} ))
When you run it gives:
[1] 3 2 2 2 NA

R merge matrices with function

I would like to merge two matrices with different length on their incommon row.names with a function:
My first matrix (T) looks similar to this:
1 2 3 4
1 -4 3 2 2
1 2 1 1 5
2 3 -2 4 6
2 -2 1 -1 -9
Now I want to join this function into my new matrix (M), however in this matrix there should be only the colsum of the matching rows which are >=0 plus 1:
1 2 3 4
1 2 3 3 3
2 2 2 2 2
I tried following formula, which I found here in the forum, however it does not work:
merge.default(as.data.frame(M), as.data.frame(T), by = "row.names", function(x){colSums(T[,]>0)+1})
Do you have an idea, where my mistake is?
Thank you very much
EDIT: my desired output would be my Matrix T, which is at the moment empty:
T now:
1 2 3 4
1
2
T after merge which is now filled with the function:
colsums(T[,] >=0)+1
1 2 3 4
1 2 3 3 3
2 2 2 2 2
T[1,1]= 2 as there is 1 value in Matrix M which is >=0 and then I add 1 to it
T[2,1]= 3 : two values >=0 and plus 1

Using If/Else on a data frame

I have a data set which looks something like
data<-c(0,1,2,3,4,2,3,1,4,3,2,4,0,1,2,0,2,1,2,0,4)
frame<-as.data.frame(data)
I now want to create a new variable within this data frame. If the column "data" reports a number of 2 or more, I want it to have "2" in that row, and if there is a 1 or 0 (e.g. the first two observations), I want the new variable to have a "1" for that observation.
I am trying to do this using the following code:
frame$twohouses<- if (any(frame$data>=2)) {frame$twohouses=2} else {frame$twohouses=1}
However if I run these 3 lines of script, every observation in the column "twohouses" is coded with a 2. However a number of them should be coded with a 1.
So my question: what am I doing wrong with my if else line or script? Or is there an alternative way to do this.
My question is similar to this one:
Using ifelse on factor in R
ut no one has answered that question.
Use ifelse:
frame$twohouses <- ifelse(frame$data>=2, 2, 1)
frame
data twohouses
1 0 1
2 1 1
3 2 2
4 3 2
5 4 2
...
16 0 1
17 2 2
18 1 1
19 2 2
20 0 1
21 4 2
The difference between if and ifelse:
if is a control flow statement, taking a single logical value as an argument
ifelse is a vectorised function, taking vectors as all its arguments.
The help page for if, accessible via ?"if" will also point you to ?ifelse
Try this
frame$twohouses <- ifelse(frame$data>1, 2, 1)
frame
data twohouses
1 0 1
2 1 1
3 2 2
4 3 2
5 4 2
6 2 2
7 3 2
8 1 1
9 4 2
10 3 2
11 2 2
12 4 2
13 0 1
14 1 1
15 2 2
16 0 1
17 2 2
18 1 1
19 2 2
20 0 1
21 4 2

Resources