2016-04-28 15 views
1

因此,這裏是遊戲計劃。我正在嘗試下面這個數據集(將是一個結構對象),通過它運行一個曲線迴歸模型。 然後,我想在每個點處取斜率(即每個x的一階導數值),並將該斜率信息的數據表保存在其自己的列中。如何獲得曲線預測模型的斜率?並將它們保存爲表

input.txt中:

yval xval 
0.198 0.125 
0.18 0.0625 
0.126 0.03125 
0.078 0.015625 
0.066 0.007813 
0.03 0.0039065 
0.00 0.0 

script.r:

dat <- read.table("input.txt", header=T, sep="\t") 
library(drc) 
library(ggplot2) 

mm <- structure(list(x = dat$xval, y = dat$yval), .Names= c("x","y"), class = "data.frame") 
model.drm <- drm (y ~ x, data = mm, fct = MM.2()) 
mml <- data.frame(x = seq(0, max(mm$x), length.out = 100)) #I actually don't know what length does, and am unsure about this line 
mml$y <- predict(model.drm, newdata = mml) 

ggplot(mm, aes(x = x, y = y)) + 
    theme_bw() + 
    xlab("x lab") + 
    ylab("y lab") + 
    ggtitle("dose response curve") + 
    geom_point(alpha = 0.5) + 
    geom_line(data = mml, aes(x = x, y = y), colour = "blue") 
ggsave("mm.pdf", width = 6, height = 4) 

#Need to pass in vector (list) of x, into the derivative of mml's function. 
#Output will be a list of corresponding slope values for each of those x's. 
#append said slope values to the data frame and save it. 

dev.off() 

總結: 以數據,運行迴歸,取斜率的每個值,然後用在斜坡保存相同數據單獨的列。輸出將是相同的表格,但有一個新的第三列:與每個x值相關的斜率。輸出必須是這樣的:

output.txt中:

yval xval slopes 
0.198 0.125 slope1 
0.18 0.0625 slope2 
0.126 0.03125 slope3 
0.078 0.01562 slope4 
0.066 0.00781 slope5 
0.03 0.00396 slope6 
0.00 0.00 slope7 

的問題是,只是如何「在得到」這些信息,以及如何重新保存事情我不知道如何正確地做。我不熟悉R如何對方程進行微積分。我可以從summary()得到方程的常量,但我無法處理它。

我無法找到正確的信息組合(或者它只是我使用的搜索字詞?)。我很抱歉,如果其中一些看起來不正確的僞代碼,R一直......令人沮喪的學習,至少可以說。幫幫我?

[R版本3.2.4 紅帽Linux 4.1.2 數據從https://plot.ly/~gwaligroski/15/michaelis-menten-equation 典借改編自https://rpubs.com/RomanL/6752

回答

2

我會提出這個行頭,你正在創建具有一個變量的數據幀是從0相等間隔的至最大(毫米$ x)的

mml <- data.frame(x = seq(0, max(mm$x), length.out = 100)) #I actually don't know what length does, and am unsure about this line

預測然後的100的長度使用該data.frame來決定指向預測,因此如果使用length.out = 5它無線將是一個非常粗糙的曲線和length.out = 1000將是一條非常平滑的曲線。的

因此,而不是預測1之間的序列:最大(毫米$ X)要預測你XVAL,所以我會用

dat$predicted<-predict(model.drm, newdata=dat["xval"]) 

這將另一列添加到您的DAT數據幀與預測值。

添加

+geom_point(data = dat, aes(x=xval,y=predicted),colour="red") 

您ggplot,你應該在你想要的X點看到你的曲線上的紅點。

注意:我不是很擅長R,所以我無法解釋爲什麼dat [「xval」]返回1變量數據幀(您需要),dat $ xval返回一個不適用於預測。

+0

這是一個好的開始。但那些點上的第一個派生物是什麼?我無法輕易訪問該公式,而且我不確定基於geom_line曲線我將如何能夠首先獲得一階導數。摘要中嵌入了支配geom_line的逆多項式的常量 – Tom

相關問題