簡體   English   中英

使用 ggplot 將 sigmoidal 曲線擬合到點

[英]Fitting a sigmoidal curve to points with ggplot

我有一個簡單的數據框,用於不同劑量的葯物治療的反應測量:

drug <- c("drug_1", "drug_1", "drug_1", "drug_1", "drug_1", 
  "drug_1", "drug_1", "drug_1", "drug_2", "drug_2", "drug_2", 
        "drug_2", "drug_2", "drug_2", "drug_2", "drug_2")

conc <- c(100.00, 33.33, 11.11, 3.70, 1.23, 0.41, 0.14, 
        0.05, 100.00, 33.33, 11.11, 3.70, 1.23, 0.41, 0.14, 0.05)

mean_response <- c(1156, 1833, 1744, 1256, 1244, 1088, 678, 489, 
        2322, 1867, 1333, 944, 567, 356, 200, 177)

std_dev <- c(117, 317, 440, 200, 134, 38, 183, 153, 719,
      218, 185, 117, 166, 167, 88, 50)

df <- data.frame(drug, conc, mean_response, std_dev)

我可以使用以下代碼繪制這些點,並獲得我想要的可視化的基本基礎:

p <- ggplot(data=df, aes(y=mean_response, x= conc, color = drug)) +
  geom_pointrange(aes(ymax = (mean_response + std_dev), ymin = (mean_response - std_dev))) +
  scale_x_log10()

p

陰謀

我想對這些數據做的下一件事是在圖中添加一條 sigmoidal 曲線,它適合每種葯物的繪制點。 之后,我想計算這條曲線的 EC50。 我意識到我的數據中可能沒有 sigmoidal 曲線的整個范圍,但我希望能得到我所能得到的最好的估計。 此外,drug_1 的最終點不遵循 sigmoidal 曲線的預期趨勢,但這實際上並不意外,因為葯物所在的溶液可以抑制高濃度的反應(每種葯物在不同的溶液中)。 我想從數據中排除這一點。

我陷入了將 sigmoidal 曲線擬合到我的數據的步驟。 我查看了一些其他的解決方案來擬合數據的 sigmoidal 曲線,但似乎沒有一個工作。

一篇與我的問題非常接近的帖子是: (sigmoid)曲線擬合glm in r

基於它,我嘗試了:

p + geom_smooth(method = "glm", family = binomial, se = FALSE)

這會產生以下錯誤,並且似乎默認繪制直線:

`geom_smooth()` using formula 'y ~ x'
Warning message:
Ignoring unknown parameters: family 

我也嘗試過此鏈接中的解決方案: Fitting a Sigmoidal curve to this oxy-Hb data

在這種情況下,我收到以下錯誤:

Computation failed in `stat_smooth()`:
Convergence failure: singular convergence (7) 

並且沒有線被添加到圖中。

我曾嘗試查找這兩個錯誤,但似乎找不到對我的數據有意義的原因。

任何幫助將非常感激!

正如我在評論中所說,我只會使用geom_smooth()來解決一個非常簡單的問題; 一旦我遇到麻煩,我就會改用nls

我的回答與@Duck 的非常相似,但有以下不同:

  • 我展示了未加權和(逆方差)加權擬合。
  • 為了使加權擬合工作,我不得不使用nls2包,它提供了一個稍微更健壯的算法
  • 我使用SSlogis()來獲得自動(自啟動)初始參數選擇
  • 我在ggplot2之外進行所有預測,然后將其輸入geom_line()
p1 <- nls(mean_response~SSlogis(conc,Asym,xmid,scal),data=df,
          subset=(drug=="drug_1" & conc<100)
        ## , weights=1/std_dev^2  ## error in qr.default: NA/NaN/Inf ...
          )

library(nls2)
p1B <- nls2(mean_response~SSlogis(conc,Asym,xmid,scal),data=df,
            subset=(drug=="drug_1" & conc<100),
            weights=1/std_dev^2)

p2 <- update(p1,subset=(drug=="drug_2"))
p2B <- update(p1B,subset=(drug=="drug_2"))

pframe0 <- data.frame(conc=10^seq(log10(min(df$conc)),log10(max(df$conc)), length.out=100))
pp <- rbind(
    data.frame(pframe0,mean_response=predict(p1,pframe0),
               drug="drug_1",wts=FALSE),
    data.frame(pframe0,mean_response=predict(p2,pframe0),
               drug="drug_2",wts=FALSE),
    data.frame(pframe0,mean_response=predict(p1B,pframe0),
               drug="drug_1",wts=TRUE),
    data.frame(pframe0,mean_response=predict(p2B,pframe0),
               drug="drug_2",wts=TRUE)
)

library(ggplot2); theme_set(theme_bw())
(ggplot(df,aes(conc,mean_response,colour=drug)) +
 geom_pointrange(aes(ymin=mean_response-std_dev,
                     ymax=mean_response+std_dev)) +
 scale_x_log10() +
 geom_line(data=pp,aes(linetype=wts),size=2)
)

在此處輸入圖片說明

我相信 EC50 相當於xmid參數……注意加權和未加權估計之間的巨大差異……

我會建議下一個接近你想要的方法。 我還嘗試使用binomial族對您的數據進行設置,但在 0 和 1 之間的值存在一些問題。在這種情況下,您需要一個額外的變量來確定相應的比例。 以下行中的代碼使用非線性近似來繪制輸出。

最初,數據:

library(ggplot2)
#Data
df <- structure(list(drug = c("drug_1", "drug_1", "drug_1", "drug_1", 
"drug_1", "drug_1", "drug_1", "drug_1", "drug_2", "drug_2", "drug_2", 
"drug_2", "drug_2", "drug_2", "drug_2", "drug_2"), conc = c(100, 
33.33, 11.11, 3.7, 1.23, 0.41, 0.14, 0.05, 100, 33.33, 11.11, 
3.7, 1.23, 0.41, 0.14, 0.05), mean_response = c(1156, 1833, 1744, 
1256, 1244, 1088, 678, 489, 2322, 1867, 1333, 944, 567, 356, 
200, 177), std_dev = c(117, 317, 440, 200, 134, 38, 183, 153, 
719, 218, 185, 117, 166, 167, 88, 50)), class = "data.frame", row.names = c(NA, 
-16L))

在非線性最小二乘法中,您需要定義初始值以搜索理想參數。 我們使用帶有基函數nls()下一個代碼來獲取這些初始值:

#Drug 1
fm1 <- nls(log(mean_response) ~ log(a/(1+exp(-b*(conc-c)))), df[df$drug=='drug_1',], start = c(a = 1, b = 1, c = 1))
#Drug 2
fm2 <- nls(log(mean_response) ~ log(a/(1+exp(-b*(conc-c)))), df[df$drug=='drug_2',], start = c(a = 1, b = 1, c = 1))

使用這種初始參數方法,我們使用geom_smooth()繪制繪圖。 我們再次使用nls()找到正確的參數:

#Plot
ggplot(data=df, aes(y=mean_response, x= conc, color = drug)) +
  geom_pointrange(aes(ymax = (mean_response + std_dev), ymin = (mean_response - std_dev))) +
  geom_smooth(data = df[df$drug=='drug_1',],method = "nls", se = FALSE,
              formula = y ~ a/(1+exp(-b*(x-c))),
              method.args = list(start = coef(fm1),
                                 algorithm='port'),
              color = "tomato")+
  geom_smooth(data = df[df$drug=='drug_2',],method = "nls", se = FALSE,
              formula = y ~ a/(1+exp(-b*(x-c))),
              method.args = list(start = coef(fm0),
                                 algorithm='port'),
              color = "cyan3")

輸出:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM