我希望能做到这样的事情:
data = read.table("mydata.txt", header=T)[,-1]
test<-c("Piggies","Epstein","Majesty","Millionmiles","Mozambique","Deadpool","Blackbird","SamStonev2","Daytripper","Starr","Yellowsubmarine","Harrison","Taxman","Carey","Baez","Finn","Glassonionv2","Weight","Jericho","Monkey","Maggiemae","Cinnamongirl","Walrus","Anna","Handydandy","Pam","Wigwam","Psylocke","Coolwater","Wallflower","Cactustree","Hoist","Baronharkonnen","Coyote","BlueJay","NeilYoung","Harvest","KittyPryde","Beatles","Jam_session","Black_Crow","Carnival","Tombstone_blues","Sun_King","EmmaFrost","Sway","GhanimaAtreides","Heartland","Siona","Percy","Julia","Trouble","Sol_49","C21","Sol_18","i_126","i_114","Sol_6","Sinv_25","Sol_M3","Sol_20","Sol_42f","C316","C204","i_120_PigTail","C234","C368_PigTail","i_135","C1","C121_PigTail","C264","C367","Sinv12","C278_PT","i_125_PT","C485","Jerry_Garcia","C259","i_113","i_127","cassidy_PigTail","Sol_11","Jackstraw_PigTail","st_stephen","red_ant","sunrise","i_109","C216_pigtail","i129")
fit1 <- lm(data$QnWeight_initial_mg ~ data$test)
而不是必须这样做:
fit1 <- lm(data$QnWeight_initial_mg ~ data$Piggies+data$Epstein+data$Majesty+data$Millionmiles+data$Mozambique+data$Deadpool+data$Blackbird+data$SamStonev2+data$Daytripper+data$Starr+data$Yellowsubmarine+data$Harrison+data$Taxman+data$Carey+data$Baez+data$Finn+data$Glassonionv2+data$Weight+data$Jericho+data$Monkey+data$Maggiemae+data$Cinnamongirl+data$Walrus+data$Anna+data$Handydandy+data$Pam+data$Wigwam+data$Psylocke+data$Coolwater+data$Wallflower+data$Cactustree+data$Hoist+data$Baronharkonnen+data$Coyote+data$BlueJay+data$NeilYoung+data$Harvest+data$KittyPryde+data$Beatles+data$Jam_session+data$Black_Crow+data$Carnival+data$Tombstone_blues+data$Sun_King+data$EmmaFrost+data$Sway+data$GhanimaAtreides+data$Heartland+data$Siona+data$Percy+data$Julia+data$Trouble+data$Sol_49+data$C21+data$Sol_18+data$i_126+data$i_114+data$Sol_6+data$Sinv_25+data$Sol_M3+data$Sol_20+data$Sol_42f+data$C316+data$C204+data$i_120_PigTail+data$C234+data$C368_PigTail+data$i_135+data$C1+data$C121_PigTail+data$C264+data$C367+data$Sinv12+data$C278_PT+data$i_125_PT+data$C485+data$Jerry_Garcia+data$C259+data$i_113+data$i_127+data$cassidy_PigTail+data$Sol_11+data$Jackstraw_PigTail+data$st_stephen+data$red_ant+data$sunrise+data$i_109+data$C216_pigtail+data$i129)
我希望我的问题有道理!非常感谢,伙计们!
答案 0 :(得分:6)
嗯,似乎你需要使用model <- lm(data=data,formula=QnWeight~.)
。公式中的.
(点)表示考虑所有因素。
如果您不想使用所有因素,可以使用as.formula
动态创建公式:
> test <- c("Piggies","Epstein","Majesty","Millionmiles","Mozambique","Deadpool","Blackbird","SamStonev2","Daytripper","Starr","Yellowsubmarine","Harrison","Taxman","Carey","Baez","Finn","Glassonionv2","Weight","Jericho","Monkey","Maggiemae","Cinnamongirl","Walrus","Anna","Handydandy","Pam","Wigwam","Psylocke","Coolwater","Wallflower","Cactustree","Hoist","Baronharkonnen","Coyote","BlueJay","NeilYoung","Harvest","KittyPryde","Beatles","Jam_session","Black_Crow","Carnival","Tombstone_blues","Sun_King","EmmaFrost","Sway","GhanimaAtreides","Heartland","Siona","Percy","Julia","Trouble","Sol_49","C21","Sol_18","i_126","i_114","Sol_6","Sinv_25","Sol_M3","Sol_20","Sol_42f","C316","C204","i_120_PigTail","C234","C368_PigTail","i_135","C1","C121_PigTail","C264","C367","Sinv12","C278_PT","i_125_PT","C485","Jerry_Garcia","C259","i_113","i_127","cassidy_PigTail","Sol_11","Jackstraw_PigTail","st_stephen","red_ant","sunrise","i_109","C216_pigtail","i129")
> as.formula(paste("QnWeight~",paste(listoffactors,collapse="+")))
QnWeight ~ Piggies + Epstein + ... + i129
> model <- lm(data=data, formula = as.formula(paste("QnWeight~",paste(listoffactors,collapse="+")))