Matlab:回归分析(2)
方法一:直接用多元二项式回归
1 %数据输入 2 x1 = [1000 600 1200 500 300 400 1300 1100 1300 300]; 3 x2 = [5 7 6 6 8 7 5 4 3 9]; 4 %多元二项式回归函数 5 y = [100 75 80 70 50 65 90 100 110 60]; 6 X = [x1' x2']; 7 Y = y'; 8 rstool(X, Y, 'purequadratic', 0.05);%使用纯二次模型
结果:
方法二:化为多元线性回归
1 %数据输入 2 x1 = [1000 600 1200 500 300 400 1300 1100 1300 300]; 3 x2 = [5 7 6 6 8 7 5 4 3 9]; 4 y = [100 75 80 70 50 65 90 100 110 60]; 5 %多元线性回归 6 X = [ones(10, 1), x1', x2', (x1.^2)' (x2.^2)']; 7 Y = y'; 8 [b, bint, r, rint, states] = regress(Y, X, 0.05); 9 b, states
1 %数据输入 2 x = [20 25 30 35 40 45 50 55 60 65]; 3 y = [13.2 15.1 16.4 17.1 17.9 18.7 19.6 21.2 22.5 24.3]; 4 %X = [ones(10,1), x']; 5 %Y = y'; 6 %[b, bint, r, rint, states] = regress(Y, X, 0.05); 7 %rcoplot(r, rint) 8 [P, S] = polyfit(x, y, 3); 9 [Y, DELTA] = polyconf(P, 42, S, 0.05); 10 Y 11 [Y-DELTA, Y+DELTA]
结果:
Y =
18.3287
ans =
17.7891 18.8682
1 x = [0 2 4 6 8 10 12 14 16 18 20]; 2 y = [0.6 2.0 4.4 7.5 11.8 17.1 23.3 31.2 39.6 49.7 61.7]; 3 %方法一:二次多项式回归 4 [P, S] = polyfit(x, y, 2); 5 P 6 %方法二:转化为多元线性回归 7 X = [ones(11, 1), x', (x.^2)']; 8 Y = y'; 9 b = regress(Y, X, 0.05); 10 b
结果:
P =
0.1403 0.1971 1.0105
b =
1.0105 0.1971 0.1403
1 function f = Q3fun(beta, x) 2 x1 = x(:,1); 3 x2 = x(:,2); 4 x3 = x(:,3); 5 f = (beta(1).*x2 - x3./beta(5)) ./ (1 + beta(2).*x1 + beta(3).*x2 + beta(4).*x3);
1 %数据输入 2 x1 = [470 285 470 470 470 100 100 470 100 100 100 285 285]; 3 x2 = [300 80 300 80 80 190 80 190 300 300 80 300 190]; 4 x3 = [10 10 120 120 10 10 65 65 54 120 120 10 120]; 5 y = [8.55 3.79 4.82 0.02 2.75 14.39 2.54 4.35 13.00 8.50 0.05 11.32 3.13]; 6 7 X = [x1', x2', x3']; 8 Y = y'; 9 %参考值 10 beta0 = [1 0.05 0.02 0.1 2]; 11 %非线性回归函数 12 [beta, r, J] = nlinfit(X, Y, 'Q3fun', beta0); 13 beta 14 %预测及估计 15 nlintool(X, Y, 'Q3fun', beta0, 0.05)
结果:
beta =
1.2526 0.0628 0.0400 0.1124 1.1914
置信区间
-0.746742266632702 3.25193260901551
-0.0376762702432533 0.163228825666061
-0.0311725427348701 0.111268700470180
-0.0608975612723978 0.285728748651990
-0.738086110268459 3.12082296226925
——现在的努力是为了小时候吹过的牛B!!