You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Afex::mixed()读取错误列且未执行指定公式的技术求助

问题排查与解决:Afex mixed()函数列偏移异常

问题现象

使用afex包的mixed()函数做分析时,函数未执行设定的公式,读取的数据列比Formula中指定的列偏移了3列。因原始数据列名含特殊字符,改用编号列(如col477、col478等)进行分析。

分析代码

library(purrr)
library(afex)
library(tidyverse) 

res.aov<- ANOVAprep %>% 
  mutate(
    Formula = map(names, ~as.formula(paste(.x, "~ col2*col3 + (1| col1)"))),
    linregmod = map(Formula, ~mixed(.x, data = df4)),
    filtered_ANOVA = map(linregmod, pluck, "anova_table"),

数据处理代码

####Make Timepoint Factor####
df$col2<- as.factor(df$col2)

####Make new df of names####
populations <- t(df[1,]) 
nnames<- names(df)
nnames<- as.data.frame(nnames)
ANOVAprep<- bind_cols(nnames,populations)
ANOVAprep<- ANOVAprep %>% rename(populations = ...2)
ANOVAprep<- ANOVAprep %>% rename(names = nnames)
ANOVAprep<- ANOVAprep[4:9,]

####Prep for ANOVA####
df1<- df[-1,]
df2<- df1[,1:3]
df3<-lapply(df1[,4:9], as.numeric)
df4<- bind_cols(df2,df3)
df4$col3<- as.factor(df4$col3)

问题数据

> dput(df)
structure(list(col1 = c("Subject", "sub001", "sub001", "sub001", "sub001", "sub001", "sub001", "sub006", "sub006", "sub006", "sub006", "sub006", "sub006", "sub010", "sub010", "sub010", "sub010", "sub010", "sub010", "sub011", "sub011", "sub011", "sub011", "sub011", "sub011", "sub013", "sub013", "sub013", "sub013", "sub013", "sub013", "sub016", "sub016", "sub016", "sub016", "sub016", "sub016", "sub018", "sub018", "sub018", "sub018", "sub018", "sub018", "sub021", "sub021", "sub021", "sub021", "sub021", "sub021", "sub022", "sub022", "sub022", "sub022", "sub022", "sub022", "sub023", "sub023", "sub023", "sub023", "sub023", "sub023", "sub024", "sub024", "sub024", "sub024", "sub024", "sub024"), col2 = structure(c(7L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), levels = c("0", "0.25", "0.5", "1", "2", "30", "Timepoint"), class = "factor"), col3 = c("Genotype", "C", "C", "C", "C", "C", "C", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "B", "B", "B", "B", "B", "B", "A", "A", "A", "A", "A", "A", "C", "C", "C", "C", "C", "C", "B", "B", "B", "B", "B", "B"), col477 = c("pop1", "2397", "434", "547.4", "469", "330.2", NA, "515", "1507", "1269", "1612", "2292", NA, "463", "4072", "1198", "1425", "1167", NA, "111", "32", "133", "87", "16", "138", "0", "156.69999999999999", "0", "0", "18", NA, "176", "136", "157", "113", "188", "72", "641", "350", "337", "444", "1473", NA, "110", "23", "2133", "0", "0", "127", "857", "986.9", "1421", NA, "840", NA, "490.3", "593", "353", "536", NA, "1495", "574.70000000000005", "0", "58", "2962", "0", NA), col478 = c("pop2", "1446", "980", "1128", "936", "968", NA, "1824", "2080", "2071", "2006", "1799", NA, "982", "976", "882", "951", "1079", NA, "119", "126", "140", "136", "51", "187", "93.3", "167", "114", "76", "138", NA, "428", "485", "692", "570", "566", "385", "833", "1085", "1187", "1264", "1141", NA, "404", "477", "229", "282", "242", "226", "574", "1318", "1141", NA, "984", NA, "612", "692", "694", "612", NA, "432", "2087", "295", "139", "236", "291", NA), col479 = c("pop3", "2922", "5025", "5454", "5032", "4225.8", NA, "11872", "16101", "13443", "13145", "13060", NA, "7100", "8789", "9309", "7758", "8577", NA, "371", "379", "514", "459", "282", "460", "0", "639", "530", "476", "684", NA, "3093", "4075", "4595", "4526", "4000", "4196", "2866", "5062", "5643.1", "5105.8", "5745", NA, "1828", "1707", "1779", "1414", "1689", "1266", "5321.6", "8827", "8940", NA, "7004.3", NA, "4590", "4876", "5957", "5110", NA, "3245", "747", "1194", "733", "1301", "1258", NA), col480 = c("pop4", "1914", "1190", "4930", "12463", "7749", NA, "4118.3", "23390", "9634", "12350", "15303", NA, "10427", "2120", "6286", "8579", "5314", NA, "184", "1047", "1427", "1086", "975", "879", "0", "200", "421", "683", "301", NA, "1970", "6210", "4934", "6546", "9516", "68", "2763", "1925", "7537", "7231", "6034", NA, "1339", "8708", "5538", "861", "812", "1305", "7153", "11860", "9174", NA, "6449", NA, "4986", "5624.4", "7774", "4655", NA, "2565", "2075", "1031", "1970", "1510", "3251", NA), col481 = c("pop5", "2928", "5088", "5457", "4982", "4177.7", NA, "12950", "16045", "13802", "13315", "12898", NA, "7049", "9067", "9345", "7741", "8751", NA, "360", "369", "496", "449", "276", "447", "13.4", "648", "535", "477", "691", NA, "3100", "4065", "4568", "4498", "3931", "4230", "2867", "5132", "5619.1", "5074.1000000000004", "5736", NA, "1891", "1589", "1712", "1448", "1712", "1260", "5202.6000000000004", "8679", "8932", NA, "7017.4", NA, "4514", "4816", "5817", "5062", NA, "3311", "687", "1115", "462", "1265", "1167", NA), col482 = c("pop6", "7642", "9398", "9084", "8019", "9315", NA, "12976", "17003", "14201", "13580", "13513", NA, "7424", "8968", "9048", "7804", "8729", NA, "596", "636", "822", "708", "391", "766", "528", "982", "913", "669", "914", NA, "3711", "4418", "4817", "4849", "4382", "4329", "4556.2", "5580", "6302.2", "6255", "6702", NA, "1918", "1850", "1771", "1438", "1663", "1292", "5290.7", "8903", "9023", NA, "6913.2", NA, "4671", "4983", "6029", "5201", NA, "3300", "747", "1500", "883", "1360", "1295", NA)), row.names = c(NA, -67L), class = c("tbl_df", "tbl", "data.frame"))

问题根源与解决方案

问题根源

核心问题是变量名冲突:你在ANOVAprep中把列命名为names,但在mutate里调用map(names, ...)时,names实际是R内置函数names(),而非ANOVAprep的列,导致生成的公式完全错误,进而引发列偏移。

解决方案

  1. 重命名ANOVAprep中的names列,避免与内置函数冲突:
ANOVAprep<- ANOVAprep %>% rename(col_names = names)
  1. 修改分析代码,使用重命名后的列生成公式:
res.aov<- ANOVAprep %>% 
  mutate(
    # 用reformulate替代paste,构造公式更安全
    Formula = map(col_names, ~reformulate(response = .x, termlabels = c("col2*col3", "(1|col1)"))),
    linregmod = map(Formula, ~mixed(.x, data = df4, check.contrasts = FALSE)),
    filtered_ANOVA = map(linregmod, pluck, "anova_table")
  )
  1. 验证数据类型:确认df4的数值列已正确转换为数值型,避免类型错误导致的解析异常:
# 查看df4的结构
str(df4)

验证方法

运行修改后的代码后,查看res.aov$Formula,确认每个公式的响应变量为目标列名(如col477 ~ col2*col3 + (1|col1)),再检查模型摘要,确认使用的数据列正确。


内容的提问来源于stack exchange,提问作者swats15

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:37:01