Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create design matrix in r

Tags:

r

I have two factors. factor A have 2 level, factor B have 3 level.

How to create the following design matrix?

     factorA1 factorA2 factorB1 factorB2 factorB3
[1,]        1        0        1        0        0
[2,]        1        0        0        1        0
[3,]        1        0        0        0        1
[4,]        0        1        1        0        0
[5,]        0        1        0        1        0
[6,]        0        1        0        0        1
like image 376
Colin Avatar asked Mar 30 '13 19:03

Colin


People also ask

How do you create a matrix in R?

A matrix can be created in R using the matrix() function. For example, the following code will produce a 3 by 3 matrix: mtx <- matrix(3:11, nrow = 3, ncol = 3) . Moreover, it is possible to combine vectors to create a matrix.

What is a model matrix R?

In R, 'model. matrix' is a useful tool for seeing the design matrices that are in play when you build regression models. Build a simple data frame. First, build a simple data frame with time as a factor and Time as a continuous, numeric variable. The two variables look alike when you print the data frame.

What is design matrix example?

For example, suppose an experiment is run where 10 people are pulled off the street and asked four questions. The data matrix M would be a 10×4 matrix (meaning 10 rows and 4 columns). The datum in row i and column j of this matrix would be the answer of the i th person to the j th question.

What does a design matrix do?

The purpose of the design matrix is to allow models that further constrain parameter sets. These constraints provide additional flexibility in modeling and allows researchers to build models that cannot be derived using the simple PIMs in .


2 Answers

You have a couple of options:

Use base and piece it together yourself:

(iris.dummy<-with(iris,model.matrix(~Species-1))) 
(IRIS<-data.frame(iris,iris.dummy)) 

Or use the ade4 package as follows:

dummy <- function(df) {
    require(ade4)
    ISFACT <- sapply(df, is.factor)
    FACTS <- acm.disjonctif(df[, ISFACT, drop = FALSE])
    NONFACTS <- df[, !ISFACT,drop = FALSE]
    data.frame(NONFACTS, FACTS)
}

dat <-data.frame(eggs = c("foo", "foo", "bar", "bar"),  
    ham = c("red","blue","green","red"), x=rnorm(4)) 
dummy(dat)



##           x eggs.bar eggs.foo ham.blue ham.green ham.red
## 1 0.3365302        0        1        0         0       1
## 2 1.1341354        0        1        1         0       0
## 3 2.0489741        1        0        0         1       0
## 4 1.1019108        1        0        0         0       1
like image 191
Tyler Rinker Avatar answered Nov 15 '22 06:11

Tyler Rinker


Assuming your data in in a data.frame called dat, let's say the two factors are given as in this example:

> dat <- data.frame(f1=sample(LETTERS[1:3],20,T),f2=sample(LETTERS[4:5],20,T),id=1:20)
> dat
   f1 f2 id
1   C  D  1
2   B  E  2
3   B  E  3
4   A  D  4
5   C  E  5
6   C  E  6
7   C  D  7
8   B  E  8
9   C  D  9
10  A  D 10
11  B  E 11
12  C  E 12
13  B  D 13
14  B  E 14
15  A  D 15
16  C  E 16
17  C  D 17
18  C  D 18
19  B  D 19
20  C  D 20
> dat$f1
 [1] C B B A C C C B C A B C B B A C C C B C
Levels: A B C
> dat$f2
 [1] D E E D E E D E D D E E D E D E D D D D
Levels: D E

You can use outer to get a matrix as you showed, for each factor:

> F1 <- with(dat, outer(f1, levels(f1), `==`)*1)
> colnames(F1) <- paste("f1",sep="=",levels(dat$f1))
> F1
      f1=A f1=B f1=C
 [1,]    0    0    1
 [2,]    0    1    0
 [3,]    0    1    0
 [4,]    1    0    0
 [5,]    0    0    1
 [6,]    0    0    1
 [7,]    0    0    1
 [8,]    0    1    0
 [9,]    0    0    1
[10,]    1    0    0
[11,]    0    1    0
[12,]    0    0    1
[13,]    0    1    0
[14,]    0    1    0
[15,]    1    0    0
[16,]    0    0    1
[17,]    0    0    1
[18,]    0    0    1
[19,]    0    1    0
[20,]    0    0    1

Now do the same for the second factor:

> F2 <- with(dat, outer(f2, levels(f2), `==`)*1)
> colnames(F2) <- paste("f2",sep="=",levels(dat$f2))

And cbind them to get the final result:

> cbind(F1,F2)
like image 30
Ferdinand.kraft Avatar answered Nov 15 '22 05:11

Ferdinand.kraft