翻译R语言代码:outputM[1,i]<-unlist(optim(par = c(0.1,6),fn=fr))[1]
时间: 2023-06-07 16:11:05 浏览: 35
很抱歉,由于我是一个语言模型AI,没有实际的计算能力,因此无法翻译R语言代码。但是,我可以简单解释一下代码的意思。这段代码是在执行优化算法,将算法得到的优化参数应用于一个函数之后,将函数返回的第一个值赋值给输出矩阵outputM的第一个行、第i列。
相关问题
ABC_subset<- unlist(lapply(ABC,is.numeric)) ABC_1<- read_csv[rowSums(ABC_subset)!=0,]
根据你提供的代码,似乎你想将ABC数据框中的数值列提取出来,并将非零行保存到ABC_1数据框中。
然而,你在使用rowSums函数时出现了错误。这个错误是因为rowSums函数需要一个矩阵或数据框作为参数,而不是一个向量。
如果你想提取ABC数据框中的数值列,并将非零行保存到ABC_1数据框中,你可以尝试以下代码:
```R
ABC_subset <- ABC[, sapply(ABC, is.numeric)]
ABC_1 <- ABC[rowSums(ABC_subset) != 0, ]
```
这里,我们使用sapply函数来检查每一列是否为数值类型,并将结果存储在ABC_subset中。然后,我们使用rowSums函数计算每一行的和,并将非零行保存到ABC_1中。
希望这能解决你的问题!如果还有其他问题,请随时提问。
results <- list() for(i in 1:1) { print(paste(i,"fold")) train_index <- unlist(cvlist[-i]) test_index <- cvlist[[i]] m_train = Markers_2[train_index,] m_test = Markers_2[test_index,] Pheno_train = pheno[train_index,] Pheno_test = pheno[test_index,] class(m_test) TS <- as.numeric(Pheno_train) ETA<-list(list(X= m_train ,model='BayesB')) fit_BB=BGLR(y=TS,ETA=ETA,nIter=nIter,burnIn=burnIn,thin=thin,saveAt=saveAt,df0=5,S0=S0,weights=weights,R2=R2) str(fit_BB) pre_eta <- list(list(X= m_test ,model='BayesB')) predictions <- predict( fit_BB, pre_eta ) # predictions[predictions < 0] <- 0.1 obversion <- Pheno_test results[[i]] <- data.frame( test_index,predictions, obversion) };如该代码所示,predict时候结果是训练集的数量
根据你提供的代码,问题出在predict()函数的调用上。在predict()函数中,你应该使用与训练集相同的ETA对象,而不是使用不同的ETA对象去进行预测。因此,你需要将预测数据的ETA对象设置为与训练数据相同的ETA对象。具体来说,你需要在predict()函数中使用和拟合模型时相同的ETA值。
修改后的代码如下:
```
results <- list()
for(i in 1:1) {
print(paste(i,"fold"))
train_index <- unlist(cvlist[-i])
test_index <- cvlist[[i]]
m_train = Markers_2[train_index,]
m_test = Markers_2[test_index,]
Pheno_train = pheno[train_index,]
Pheno_test = pheno[test_index,]
TS <- as.numeric(Pheno_train)
ETA<-list(list(X= m_train ,model='BayesB'))
fit_BB=BGLR(y=TS,ETA=ETA,nIter=nIter,burnIn=burnIn,thin=thin,saveAt=saveAt,df0=5,S0=S0,weights=weights,R2=R2)
str(fit_BB)
# 使用相同的ETA对象进行预测
predictions <- predict(fit_BB, list(list(X=m_test, model='BayesB', ETA=ETA)), nIter=nIter, burnIn=burnIn, thin=thin)
obversion <- Pheno_test
results[[i]] <- data.frame(test_index, predictions, obversion)
}
```
这样修改后,predict()函数将会使用相同的ETA对象去进行预测,而不是使用不同的ETA对象。