简体   繁体   中英

[factorial design]invalid 'times' argument in rep()

#creat chart
thickness<-matrix(c(14.037,14.165,13.972,13.907,14.821,14.757,14.843,14.878,13.880,13.860,14.032,13.914,14.888,14.921,14.415,14.932),byrow = T,ncol = 4)
dimnames(thickness)<- list(c("(1)","a","b","ab"),c("Rep1","Rep2","Rep3","Rep4"))
A<- rep(c(-1,1),2)
B<- c(rep(-1,2),rep(1,2))
AB <- A*B
Total<- apply(thickness,1,sum)
Average<- apply(thickness,1,mean)
Variance<- apply(thickness,1,var)
table<-cbind(A,B,AB,thickness,Total,Average,Variance)
table



#Effect estimate(1)
n<-4
Aeff <-(Total %*% A)/(2*n)
Aeff
Beff <-(Total %*% B)/(2*n)
Beff
ABeff <-(Total %*% AB)/(2*n)
ABeff

#Effect estimate(2)
n<-4
Aeff<-{sum(Total[A==+1])/(2*n)}-{sum(Total[A==-1])/(2*n)}
Aeff
Beff<-{sum(Total[B==+1])/(2*n)}-{sum(Total[B==-1])/(2*n)}
Beff
ABeff<-{sum(Total[AB==+1])/(2*n)}-{sum(Total[AB==-1])/(2*n)}
ABeff

#summary
thickness.vec<- c(t(thickness))
XA <- rep(as.factor(A),rep(2,4))
XB <- rep(as.factor(B),rep(2,4))
XAB <- rep(as.factor(AB),rep(2,4))
options(contrasts=c("contr.sum","contr.poly"))
thickness.lm<- lm(thickness.vec ~ XA+XB+XAB)

Error in model.frame.default(formula = thickness.vec ~ XA + XB + XAB, : variable lengths differ (found for 'XA')

It is factorial design in r.
I cannot get lm to work because of different lengths of the regressors and response.

length(thickness.vec)
#[1] 16
length(XA)
#[1] 8
length(XB)
#[1] 8
length(XAB)
#[1] 8

I don't know how to fix it.
What should I do to solve this?

Maybe what you want is to transform wide format into long format. I think it isn't good idea to do it manually.

Here is my example:

# install.packages("tidyverse")
library(tidyverse)

d1 <- table %>% 
  as.tibble() %>%                                                 # transform into tbl_df to manipulate easily
  select(A, B, AB, Rep1, Rep2, Rep3, Rep4) %>%                    # select interested cols
  gather(key = "Rep", value = "thickness", -A, -B, -AB) %>%       # change into longformat
  mutate(A = as.factor(A), B = as.factor(B), AB = as.factor(AB))  # change classes

options(contrasts=c("contr.sum","contr.poly"))
thickness.lm <- lm(thickness ~ A + B + AB, data = d1)
summary(thickness.lm)


[EDITED (update)]
lm() wants one dependent variable and a sets of independent variables in one row, using formula with data.frame. But your table, each rows have four dependent variables. So I transform your table into long format.
-term isn't gathered but repeated (see help(gather, tidyr) ).
In R, alphabetical order is a default order of factor levels, and it effects appearances of the output.

d2 <- table %>% 
  as.tibble() %>% 
  select(A, B, AB, Rep1, Rep2, Rep3, Rep4) %>% 
  gather(key = "Rep", value = "thickness", -A, -B, -AB) %>%  
  mutate(A = factor(A, levels = c(1, -1)),   # change class with definition of levels' order.
         B =factor(B, levels = c(1, -1)), 
         AB = factor(AB, levels = c(1, -1)))

> d1$A
 [1] -1 1  -1 1  -1 1  -1 1  -1 1  -1 1  -1 1  -1 1 
Levels: -1 1
> d2$A
 [1] -1 1  -1 1  -1 1  -1 1  -1 1  -1 1  -1 1  -1 1 
Levels: 1 -1

options(contrasts=c("contr.sum","contr.poly"))
thickness.lm2 <- lm(thickness ~ A + B + AB, data = d2)
summary(thickness.lm2)

# AB is an interaction term, isn't it? if so, you needn't prepare the value.
thickness.lm3 <- lm(thickness ~ A * B, data = d2)
summary(thickness.lm3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM