=begin
使用方法:按F11插入脚本编辑器Main前面,用脚本self.hgfc(y)即可···(PS:self和y为数组)
即插即用,麻麻再也不用担心我的学习啦!
=end
def hgfc(y) # 回归方程(必须用print,否则会p num => nil)
xn=self.size
yn=y.size
if xn!=yn
raise ArgumentError.new("NaN")
end
xx=0
xy=0
xl=0
yl=0
for i in 0...xn
xx+=self[i]**2
xy+=self[i]*y[i]
xl+=self[i]
yl+=y[i]
end
xl/=xn
yl/=yn
b=(xy-xn*xl*yl)/(xx-xn*xl**2).to_f
a=yl-b*xl
if a>=0
a="+"+a.to_s
end
if b==1
b=""
end
num="回归方程为y="+b.to_s+"x"+a.to_s
end
=begin
使用方法:按F11插入脚本编辑器Main前面,用脚本self.hgfc(y)即可···(PS:self和y为数组)
即插即用,麻麻再也不用担心我的学习啦!
=end
def hgfc(y) # 回归方程(必须用print,否则会p num => nil)
xn=self.size
yn=y.size
if xn!=yn
raise ArgumentError.new("NaN")
end
xx=0
xy=0
xl=0
yl=0
for i in 0...xn
xx+=self[i]**2
xy+=self[i]*y[i]
xl+=self[i]
yl+=y[i]
end
xl/=xn
yl/=yn
b=(xy-xn*xl*yl)/(xx-xn*xl**2).to_f
a=yl-b*xl
if a>=0
a="+"+a.to_s
end
if b==1
b=""
end
num="回归方程为y="+b.to_s+"x"+a.to_s
end