R nls回归和存储输出系数和图
我试图按照以下建议制作可复制的数据: 在示例数据中,ID只有两个级别,而不是指示的150 我的nls看起来是这样的:R nls回归和存储输出系数和图,r,dataframe,ggplot2,plyr,nls,R,Dataframe,Ggplot2,Plyr,Nls,我试图按照以下建议制作可复制的数据: 在示例数据中,ID只有两个级别,而不是指示的150 我的nls看起来是这样的: res.sample <- ddply ( d, .(ID), function(x){ mod <- nls(carbon~phi1/(1+exp(-(phi2 + phi3 * DAY))), start=list( phi1 = x$max.carb, phi2 = int[1],
res.sample <- ddply (
d, .(ID),
function(x){
mod <- nls(carbon~phi1/(1+exp(-(phi2 + phi3 * DAY))),
start=list(
phi1 = x$max.carb,
phi2 = int[1],
phi3 = mean(x$g.rate.perc)),
data=x,trace=TRUE)
return(coef(mod))
}
)
顶部的樱桃将是实际数据和顶部拟合曲线的曲线图。手动与子集我也可以做到这一点,但它只是太耗时。
我的代码是
ggplot(data=n, aes(x = DAY, y = carbon))+
geom_point(stat="identity", size=2) +
geom_line( aes(DAY,predict(logMod) ))+
ggtitle("ID")
如果包含三元组信息的ID不太有用,下面是如何将其返回到另一个版本
sep_sample <- sample %>% separate(ID, c("algae", "id", "nutrient"))
我希望这是你可以处理的,这是一个合理的问题。如果有一些页面已经提供了类似的解决方案,而我还没有找到,我会很乐意看一看
非常感谢 所以我提出了这个解决方案,这并不是我想要的全部,但我认为更进一步,因为它正在运行
coef_list <- list()
curve_list <- list()
for(i in levels(d$ALGAE)) {
for(j in levels(d$NUTRIENT)) {
dat = d[d$ALGAE == i & d$NUTRIENT == j,]
#int <- coef(lm(DAY~carbon,data=dat))
mod <- nls(carbonlog~phi1/(1+exp(-(phi2+phi3*DAY))),
start=list(
phi1=9.364,
phi2=0,
phi3= 0.135113),
data=dat,trace=TRUE)
coef_list[[paste(i, j, sep = "_")]] = coef(mod)
plt <- ggplot(data = dat, aes(x = DAY, y = carbonlog)) + geom_point()+
geom_line( aes(DAY,predict(mod) ))+
ggtitle(paste(i,"RATIO",j,sep=" ")) +
theme.plot
curve_list[[paste(i, j, sep = "_")]] = plt
}
}
但我收到一条错误消息:
Error: Aesthetics must be either length 1 or the same as the data (17): x, y
我只在使用对数转换的碳值时才得到消息。当我使用原始格式的碳元素时,它会绘制所有内容真的,将
dput(yourdataframe)
的输出粘贴到问题中太困难了?“它开始占用太多时间”不是一个合理的借口。你可能会发现library(nlme);帮助(“nlsList”)
有用。我现在明白了我的错误,我使用了dput(head(df))而不仅仅是dput,所以我很抱歉,但是对于那些不经常使用代码的人来说,有时候事情并不那么明显,特别是当你在短时间内不断接收大量信息时。我现在将调整我的问题,我为这明显的死罪感到抱歉
sep_sample <- sample %>% separate(ID, c("algae", "id", "nutrient"))
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
coef_list <- list()
curve_list <- list()
for(i in levels(d$ALGAE)) {
for(j in levels(d$NUTRIENT)) {
dat = d[d$ALGAE == i & d$NUTRIENT == j,]
#int <- coef(lm(DAY~carbon,data=dat))
mod <- nls(carbonlog~phi1/(1+exp(-(phi2+phi3*DAY))),
start=list(
phi1=9.364,
phi2=0,
phi3= 0.135113),
data=dat,trace=TRUE)
coef_list[[paste(i, j, sep = "_")]] = coef(mod)
plt <- ggplot(data = dat, aes(x = DAY, y = carbonlog)) + geom_point()+
geom_line( aes(DAY,predict(mod) ))+
ggtitle(paste(i,"RATIO",j,sep=" ")) +
theme.plot
curve_list[[paste(i, j, sep = "_")]] = plt
}
}
curve_list[["ANK_1"]]
Error: Aesthetics must be either length 1 or the same as the data (17): x, y