簡體   English   中英

R:使用二維非線性最小二乘(nls)的流行擴散模型

[英]R: epidemic diffusion model using two-dimensional non-linear least squares (nls)

我正在嘗試在R中實施流行病擴散模型。擴散的公式為delta_y =(a + b * y)*(Ny)。 y描述了時段t中的當前用戶,N描述了潛在用戶的數量,delta_y描述了t中的新用戶,並且a和b是要估計的參數。 注意,y是所有先前delta_y的累加和。 對於單個觀察(以delta_y和y為向量),該模型僅適用於:

model1 <- nls(delta_y ~ (a+b * y) * (N-y))

現在的問題是,我有一組這種類型的觀測值,並且我想為所有這些值估計相同的參數a和b。 我試圖從上面使用相同的公式,但是現在delta_y和y是二維數組,而不是矢量。 我在“ qr.qty(QR,resid)中收到錯誤:'qr'和'y'必須具有相同的行數”

有關數據的詳細信息:y和delta_y是具有16列和20103行的二維數組。 數組創建如下:

y=matrix(c(data$nearby_1998,data$nearby_1999, data$nearby_2000, ..., data$nearby_2013),nrow=20103)
invCum <- function (data) {result=matrix(nrow=nrow(data), ncol=ncol(data)); result[,1]=data[,1]; for (i in 2:ncol(data)) {result[,i] <- data[,i]-data[,i-1]}; return(result)}
delta_y <- invCum(y)

invCum是在t中累積用戶的情況下返回t中新用戶的函數(實際上是反求和函數)。

str(y)傳遞“ int [1:20103,1:16] 0 0 0 0 0 0 0 0 0 0 ...”。 str(delta_y)還提供“ int [1:20103,1:16] 0 0 0 0 0 0 0 0 0 0 ...”。 請注意,並非所有條目都是0,只有許多前一個條目。

數據列各具有20103條目。 上面的模型只處理了一行數據。

搜索Rhelp檔案文件中的錯誤並發現Duncan Murdoch解決類似情況,方法是使用as.vector()將矩陣轉換為“ long”形式,並查看Pinheiro和Bates中nls和nlsList上的資料, m發布一些可能與您的數據情況一致的實驗結果。 如果我正確理解,您對delta_yy的觀測值有16種不同的“運行”,並且您希望用相同的非線性模型對它們進行建模。 還不清楚您是否期望它們全部:(A)具有相同的參數,或者(B)期望系數僅以相同的形式變化。 讓我們首先考慮(A)情況,這是九年前Duncan Murdoch提供的as.vector()解決方案所提供的。

newdf <- data.frame( d_y <- as.vector(delta_y), 
                     y = as.vector(y), 
                     grp=rep(letters[1:16], each=20103) )
N=   _____  # you need to add this; not sure if it's a constant or vector
            # if it varies across groups need to use the rep()-strategy to add to newdf
model1 <- nls( d_y ~ (a+b * y) * (N-y)  , data=newdf, start=list(a=0, b=1))

另一方面,如果您想要單獨的系數:

 library(nlme)
 model1 <- nlsList(delta_y ~ (a+b * y) * (N-y) | grp, data=newdf, start=c(a=0, b=1) )

這是一些測試:首先在單個組上(以?nls為例):

DNase1 <- subset(DNase, Run == 1) 
> fm2DNase1 <- nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
+                  data = DNase1,
+                  start = list(xmid = 0, scal = 1) )
> summary(fm2DNase1)
==================
Formula: density ~ 1/(1 + exp((xmid - log(conc))/scal))

Parameters:
     Estimate Std. Error t value Pr(>|t|)
xmid -0.02883    0.30785  -0.094    0.927
scal  0.45640    0.27143   1.681    0.115

Residual standard error: 0.3158 on 14 degrees of freedom

Number of iterations to convergence: 14 
Achieved convergence tolerance: 1.631e-06

現在,在不考慮組ID的情況下對該數據集的所有組進行了處理:

> fm2DNase <- nls(density ~ 1/(1 + exp((xmid - log(conc))/scal)),
+                  data = DNase,
+                  start = list(xmid = 0, scal = 1) )
> summary(fm2DNase)
==========
Formula: density ~ 1/(1 + exp((xmid - log(conc))/scal))

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
xmid -0.14816    0.09780  -1.515    0.132    
scal  0.46736    0.08691   5.377 2.41e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3291 on 174 degrees of freedom

Number of iterations to convergence: 13 
Achieved convergence tolerance: 7.341e-06

最后,在每個組上,方程式保持唯一的常數:

> fm2DNase <- nlsList(density ~ 1/(1 + exp((xmid - log(conc))/scal))|Run,
+                  data = DNase,
+                  start = list(xmid = 0, scal = 1) )
> summary(fm2DNase)
Call:
  Model: density ~ 1/(1 + exp((xmid - log(conc))/scal)) | Run 
   Data: DNase 

Coefficients:
   xmid 
      Estimate Std. Error     t value  Pr(>|t|)
10 -0.23467586  0.3527077 -0.66535499 0.4749505
11 -0.18717815  0.3522418 -0.53139112 0.5746396
9  -0.14742434  0.3459987 -0.42608348 0.6521089
1  -0.02882911  0.3403312 -0.08470898 0.9267180
4  -0.01243939  0.3351487 -0.03711604 0.9691708
8  -0.09549007  0.3408348 -0.28016525 0.7741478
5  -0.09216741  0.3367420 -0.27370331 0.7800695
7  -0.25657193  0.3613815 -0.70997535 0.4750054
6  -0.25052019  0.3564816 -0.70275765 0.5051072
2  -0.11218699  0.3245483 -0.34567120 0.7763199
3  -0.23007674  0.3433663 -0.67006203 0.5933597
   scal 
    Estimate Std. Error  t value  Pr(>|t|)
10 0.4904888  0.3148254 1.557971 0.1076081
11 0.4892928  0.3138277 1.559113 0.1139307
9  0.4723505  0.3075025 1.536087 0.1189793
1  0.4564003  0.3000630 1.521015 0.1148339
4  0.4423467  0.2946883 1.501066 0.1338825
8  0.4582587  0.3018498 1.518168 0.1352101
5  0.4473772  0.2980249 1.501140 0.1407799
7  0.5142468  0.3234251 1.590003 0.1224310
6  0.5007426  0.3185856 1.571768 0.1483103
2  0.4161636  0.2878193 1.445920 0.2457047
3  0.4654567  0.3062277 1.519969 0.2355130

Residual standard error: 0.3491304 on 154 degrees of freedom

暫無
暫無

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

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