R中的回归(对数正态):找到特定y值(结果)的x值(预测值)

时间:2017-08-28 22:57:16

标签: r

这与Regression (logistic) in R: Finding x value (predictor) for a particular y value (outcome)中发布的问题类似。我试图找到已知y值(在这种情况下为0.000001)的x值,该值是通过使用遗传算法拟合适合于与亲本树的距离处的幼树密度的对数正态曲线而获得的。该算法给出了最佳拟合对数正态曲线的a和b参数。

对于其他曲线,我已经获得了x的y = 0.00001的值,例如负指数,通过使用此代码使用uniroot(适用于这些曲线):

##calculate x value at y=0.000001 (predicted near-maximum recruitment distance)
aparam=a
bparam=b
testfn <- function (y, aparam, bparam) {
## find value of x that satisfies y = a + bx
    fn <- function(x) (a * exp(-b * x)) - y
    uniroot(fn, lower=0, upper= 100000000)$root
}
testfn(0.000001)

不幸的是,使用对数常规公式的相同代码不起作用。我试图通过将下边界设置为零以上来使用uniroot。但是得到一个错误代码:

Error in uniroot(fn, lower = 1e-16, upper = 1e+18) : 
  f() values at end points not of opposite sign

我的代码和数据(在代码下面给出)是:

file="TR maire 1mbin.txt"
xydata <- read.table(file,header=TRUE,col.names=c('x','y'))



####assign best parameter values
a = 1.35577
b = 0.8941521



#####Plot model against data
par(mar=c(5,5,2,2))

xvals=seq(1,max(xydata$x),1)
plot(jitter(xydata$x), jitter(xydata$y),pch=1,xlab="distance from NCA (m)",
ylab=quote(recruit ~ density ~ (individuals ~ m^{2~~~ -1})))
col2="light grey"


plotmodel <-  a* exp(-(b) * xvals)
lines(xvals,plotmodel,col=col2)




####ATTEMPT 1
##calculate x value at y=0.000001 (predicted near-maximum recruitment distance)
aparam=a
bparam=b
testfn <- function (y, aparam, bparam) {
    fn <- function(x) ((exp(-(((log(x/b)) * (log(x/b)))/(2*a*a))))/(a * x * sqrt(2*pi))) - y
    uniroot(fn, lower=0.0000000000000001, upper= 1000000000000000000)$root
}
testfn(0.000001)

数据是:

 xydata

1    1 0.318309886
2    2 0.106103295
3    2 0.106103295
4    2 0.106103295
5    3 0.063661977
6    4 0.045472841
7    5 0.035367765
8    5 0.035367765
9    7 0.048970752
10   8 0.021220659
11   8 0.021220659
12   8 0.042441318
13   9 0.018724111
14  10 0.016753152
15  10 0.016753152
16  12 0.013839560
17  13 0.025464791
18  16 0.010268061
19  17 0.009645754
20  24 0.013545102
21  25 0.032480601
22  26 0.043689592
23  27 0.006005847
24  28 0.011574905
25  31 0.062618338
26  32 0.005052538
27  42 0.003835059
28  42 0.003835059
29  44 0.003658734
30  46 0.003497911
31  48 0.006701261
32  50 0.003215251
33  50 0.006430503
34  51 0.006303166
35  58 0.002767912
36  79 0.002027452
37 129 0.003715680
38 131 0.001219578
39 132 0.001210304
40 133 0.001201169
41 144 0.001109094
42 181 0.000881745
43 279 0.001142944
44 326 0.000488955

还是有另一种方法来接近这个吗?  我是一名生态学家,有时R只是没有意义!

1 个答案:

答案 0 :(得分:0)

似乎我的r代码中存在一些错误,但主要问题是我的下限太低而Log Normal曲线没有扩展到该值(我的解释)。适合我的解决方案是:

    ### define the formula parameter values

        a = 1.35577
        b = 0.8941521


        ### define your formula (in this instance a log normal) in the {}
        fn <- function(x,a,b,y) { ((exp(-(((log(x/b)) * (log(x/b)))/(2*a*a))))/(a * x * sqrt(2*pi))) - y}


###then use uniroot()$root calling the known parameter values and defining the value of y that is of interest (in this case 0.000001)

        uniroot(fn,c(1,200000),a=a,b=b,y=0.000001)$root