RI では、nls を使用して非線形最小二乗法を実行します。では、適合によって得られた係数の値を使用してモデル関数をプロットするにはどうすればよいでしょうか?
(はい、これはR の初心者からの非常に素朴な質問です。)
最初の例を使用して、?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)
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')
例えば。