【问题标题】:[factorial design]invalid 'times' argument in rep()[因子设计] rep() 中的“次”参数无效
【发布时间】:2017-11-24 04:26:08
【问题描述】:
#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 中的错误(公式 = 厚度.vec ~ XA + XB + XAB, : 可变长度不同(为 'XA' 找到)

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

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

我不知道如何解决它。
我该怎么做才能解决这个问题?

【问题讨论】:

  • OP 对错误的原因和抛出它的函数有误。甚至没有包含错误消息。当我运行代码时,错误消息就是我所包含的。加上一些关于错误中涉及的变量的额外信息。

标签: r


【解决方案1】:

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

这是我的例子:

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

【讨论】:

  • 谢谢!它真的有效!但我想知道你为什么改成长格式
  • 我也想知道你把 (-) 放在 A,B,AB 聚集(key = "Rep", value = "thickness", -A, -B, -AB) %>%
  • 如果你做thickness.lm,A、B的系数值分别是-0.41800和0.03363。但系数的真实值分别为 0.41800 和 -0.03363。这是代码问题还是程序问题?
  • 但系数仍然不同:( A,B 必须分别为 0.41800 和 -0.03363,但仍为 -0.41800 和 0.03363。“基线是字母顺序的最大值,例如 A -1"?
  • 感谢您的长篇解释。但是有没有办法改变回归系数的符号?
猜你喜欢
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 2020-05-31
相关资源
最近更新 更多