0

我正在运行几个模型和模型的功能。我需要将列表中的值应用到默认的 R 函数,例如 modelFit() 我的想法是对多个模型运行相同的函数,然后编译结果。

我正在尝试在 R 中使用循环和“应用”函数系列,但没有成功。

#package drc is necessary
library(drc)
#my data
rates <- c(.1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000,.1,.1,.1,1,1,1,10,10,10,100,100,100,1000,1000,1000)
prod <- c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B")
resp <- c(.295,.32,.301,.155,.1501,.148,.05,.03,.044,.002,.001,.0015,.001,.0005,.0003,.312,.337,.299,.265,.2501,.248,.102,.103,.114,.02,.01,.015,.003,.0002,.0007)
data.test <- data.frame(rates,prod,resp) #my data frame

#my models
m1 <- drm(resp~rates, fct=LL.4(), data=data.test[data.test$prod=="A",])
m2 <- drm(resp~rates, fct=LL.4(), data=data.test[data.test$prod=="B",])

#lack of fit test
modelFit(m1)
modelFit(m2)

#I can get the modelFit p-values this way:
modelFit(m1)$"p value"[2]
modelFit(m2)$"p value"[2]


#I have several models. I want to create a loop to give me only the p value for each model fit. I want to use that for other information given by the summaries and function

#list of models
modelsList <- c("m1","m2")

#I can print the strings with the loop
for(i in modelsList){
  print(i)
  }

我的想法是使用字符串为默认的“drc”函数 modelFit() 添加信息。结果将是一个列表,其中包含循环中所有结果中的所有 p 值,但我收到错误消息:

错误:$ 运算符对原子向量无效

#Not working
for(i in modelsList){
  modelFit(i)$"p value"[2]
  }

#Error
# Error: $ operator is invalid for atomic vectors

#Trying to use lapply
#Following this logic
lapply(modelsList, function(x) print(x))
#I could not get the results
lapply(modelsList, function(x) modelFit(x)$"p value"[2])

有了结果,我将继续创建一个包含所有模型和相应 p 值的 data.frame。

4

2 回答 2

0

很高兴见到你,达尼洛

我认为你错过了drm功能。


modelsList <- c("m1","m2")
prod = c("A","B")
type = data.frame(modelsList,
                  prod)
#I can print the strings with the loop
for(i in modelsList){
  print(i)
}


for(i in modelsList){
  model_Prod = type[modelsList == i,]$prod
  drm_result<-drm(resp~rates, fct=LL.4(), data=data.test[data.test$prod==model_Prod,])
  print(modelFit(drm_result)$"p value"[2])
}

# lapply
lapply(modelsList, function(x) {
  model_Prod = type[modelsList == x,]$prod
  x<-drm(resp~rates, fct=LL.4(), data=data.test[data.test$prod==model_Prod,])
  modelFit(x)$"p value"[2]
}

)
于 2019-08-02T00:31:21.913 回答
0

继续,在 Steve Lee 输入之后,我可以生成一个 data.frame,其中包含我从循环中获得的所有结果:

#source of information:
#https://stackoverflow.com/questions/25285570/invalid-factor-level-with-rbind-to-data-frame
#df.results will be a data.frane with headers = "Model" and "ModelFit_pvalue""
df.results <- NULL
#Loop to print only specif information requested from function and add it to a data frame
for(i in modelsList){
  model_Prod = type[modelsList == i,]$prod
  drm_result<-drm(resp~rates, fct=LL.4(), data=data.test[data.test$prod==model_Prod,])
  #print(i)
  #print(modelFit(drm_result)$"p value"[2])
  modelpvalue <- modelFit(drm_result)$"p value"[2] #store p values in a variable
  de <- data.frame("Model"=i,"ModelFit_pvalue"= modelpvalue)
  df.results = rbind(df.results,de)
}
df.results

#df.results output:
  Model ModelFit_pvalue
1    m1       0.2346460
2    m2       0.5757368
于 2019-08-03T13:55:03.363 回答