4

RI では、nls を使用して非線形最小二乗法を実行します。では、適合によって得られた係数の値を使用してモデル関数をプロットするにはどうすればよいでしょうか?

(はい、これはR の初心者からの非常に素朴な質問です。)

4

4 に答える 4

14

最初の例を使用して、?nls私が指摘した例に従って行ごとに実行すると、次のことが達成されます。

#This is just our data frame
DNase1 <- subset(DNase, Run == 1)
DNase1$lconc <- log(DNase1$conc)
#Fit the model
fm1DNase1 <- nls(density ~ SSlogis(lconc, Asym, xmid, scal), DNase1)

#Plot the original points
# first argument is the x values, second is the y values
plot(DNase1$lconc,DNase1$density)

#This adds to the already created plot a line
# once again, first argument is x values, second is y values
lines(DNase1$lconc,predict(fm1DNase1))

引数のpredictメソッドはnls、適合y値を自動的に返します。または、ステップを追加して実行します

yFitted <- predict(fm1DNase1)

yFitted代わりに に 2 番目の引数を渡しlinesます。結果は次のようになります。

ここに画像の説明を入力

または、「滑らかな」曲線が必要な場合は、単純にこれを繰り返しますが、関数をより多くの点で評価します。

r <- range(DNase1$lconc)
xNew <- seq(r[1],r[2],length.out = 200)
yNew <- predict(fm1DNase1,list(lconc = xNew))

plot(DNase1$lconc,DNase1$density)
lines(xNew,yNew)
于 2012-03-29T14:33:12.557 に答える
3

coef(x) は、回帰結果 x の係数を返します。

model<-nls(y~a+b*x^k,my.data,list(a=0.,b=1.,k=1))
plot(y~x,my.data)
a<-coef(model)[1]
b<-coef(model)[2]
k<-coef(model)[3]
lines(x<-c(1:10),a+b*x^k,col='red')

例えば。

于 2013-05-21T17:34:34.950 に答える