[阶乘设计]rep() 中的参数无效'times'

  • 本文关键字:参数 无效 times 阶乘 rep r
  • 更新时间 :
  • 英文 :

#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)

model.frame.default中的错误(公式=厚度。 :可变长度有所不同(针对'xa'找到)

它是r。
中的阶乘设计由于回归器和响应的不同长度,我无法让lm工作。

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

我不知道如何修复它。
我该怎么办?

也许您想要的是将宽格式转换为长格式。我认为手动做不是好主意。

这是我的示例:

# 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)


[编辑(更新)]
lm()使用一个带有data.frame的公式。但是您的桌子,每个行都有四个因变量。所以我将您的桌子转换为长格式。
-term未收集但重复(请参阅help(gather, tidyr))。
在R中,字母顺序是因子级别的默认顺序,并且它会影响输出的外观。

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)

相关内容

最新更新