我正在使用R type provider将一个小代码段转换为F#。一切正常并且评估,但是我似乎无法使预测函数使用我给它预测的所有点。
R代码段:
Nit = c(0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,6,6,6)
AOB = c(4.26,4.15,4.68,6.08,5.87,6.92,6.87,6.25,6.84,6.34,6.56,6.52,7.39,7.38,7.74,7.76,8.14,7.22)
AOBm=tapply(AOB,Nit,mean) #means of AOB
Nitm=tapply(Nit,Nit,mean) #means of Nit
fitAOB=lm(AOBm∼ns(Nitm,df=2)) #natural spline
predict(fitAOB,data.frame(Nitm=seq(xmin,xmax,.5))
和相应的F#代码:
let Nit = [0;0;0;1;1;1;2;2;2;3;3;3;4;4;4;6;6;6]
let AOB = [4.26;4.15;4.68;6.08;5.87;6.92;6.87;6.25;6.84;6.34;6.56;6.52;7.39;7.38;7.74;7.76;8.14;7.22]
let AOBm = R.tapply(AOB,Nit, "mean")
let Nitm = R.tapply(Nit, Nit, "mean")
let fitAOB =
namedParams [
"AOBm", box AOBm
"Nitm", box Nitm
]
|> R.data_frame
|> fun d -> R.lm(formula="AOBm~splines::ns(Nitm,df=2)", data=d)
let xmin, xmax = float(List.min Nit), float(List.max Nit)
let prediction1 =
namedParams [ "Nitm",[xmin .. 0.5 .. xmax]]
|> R.data_frame
|> fun data -> R.predict(fitAOB, data)
prediction1.Print()
R代码片段为我提供了预测:
1 2 3 4 5 6 7 8
4.753486 5.177103 5.590795 5.984636 6.348702 6.673067 6.947806 7.166302
9 10 11 12 13
7.335171 7.464340 7.563733 7.643276 7.712893
F#代码片段让我关注预测:
val it : string =
" 0 1 2 3 4 6
4.753486 5.668817 6.470509 7.048984 7.388006 7.660199
"
我缺少什么?,例如。为什么预测功能在预测时不考虑所有[0.0 .. 0.5 .. 6.0]点?
答案 0 :(得分:0)
找到一种方法使R.predict产生与R中使用的预测函数相同的结果。
let prediction1 =
namedParams [
"type",box "prediction"
"Nitm",box [0.0 .. 0.5 .. 6.0]
]
|> R.data_frame
|> fun d -> R.predict(fitAOB, [0.0 .. 0.5 .. 6.0], d)
prediction1.Print()
给出了
val it : string =
" 1 2 3 4 5 6 7 8
4.753486 5.177103 5.590795 5.984636 6.348702 6.673067 6.947806 7.166302
9 10 11 12 13
7.335171 7.464340 7.563733 7.643276 7.712893
"
但是,我不太明白为什么我必须重复[0.0 .. 0.5 .. 6.0]序列..