<<"LinearRegression`"

Transformacie na linearnu regresiu

Nameranymi datami prelozte krivku y=a.(b^x),nakreslite obrazok

xhodnota={2,3,3,4,5,6,7,8,9,11};

yhodnota={18.41,54.78,54.42,162.84,486.67,1458.47,4374.03,13122.15,39366.11,354294.13};

data1=Table[{xhodnota[[i]],yhodnota[[i]]},{i,10}]

g1=ListPlot[data1,PlotStyle->PointSize[Large]]

Aby sa dala prelozit priamka, musime transformovat na [x, log[y]]

data2=Table[{xhodnota[[i]],Log[yhodnota[[i]]]},{i,10}]

ListPlot[data2,PlotStyle->PointSize[Large]]

regresia1=Regress[data2,{x},x]

param1=Regress[data2,{x},x,RegressionReport->BestFitParameters]

ab=BestFitParameters/.param1

a=ab[[1]]

b=ab[[2]]

spatne transformujeme odhady najdenych parametrov

a=Exp[a]

b=Exp[b]

g2=Plot[a*b^x,{x,Min[xhodnota],Max[xhodnota]}]

Show[g1,g2]

Interval spolahlivosti pre a okolo regresnej priamky

Pocas fyzikalneho pokusu boli namerane tieto hodnoty Y v zavislosti od X, Vypocitajte korelacny koeficient, urcte najlepsiu fitovanu krivku, najdite IS pre a okolo regresnej krivky.

data={{0.055,90},{0.091,97},{0.138,107},{0.167,124},{0.182,142},{0.211,150},{0.232,172},{0.248,189},{0.284,209},{0.351,253}}

g1=ListPlot[data,PlotStyle->PointSize[Large]]

xval=data[[All,1]]

xval[[1]]

yval=data[[All,2]]

Correlation[xval,yval]

Regress[data,x,x]

Regress[data,{x,(x^2)},x]

Regress[data,{x^2},x]

fit=Regress[data,{x,(x^2)},x,RegressionReport->BestFit]

fit2=Regress[data,{(x^2)},x,RegressionReport->BestFit]

g2=Plot[BestFit/.fit2,{x,Min[xval],Max[xval]}]

g3=Plot[BestFit/.fit,{x,Min[xval],Max[xval]},PlotStyle->Hue[0.9]]

Show[g1,g2,g3]

Najprv urobime IS okolo regresnej priamky

regress=Regress[data,{1,x,(x^2)},x,RegressionReport->{FitResiduals,SinglePredictionCITable}]

SinglePredictionCITable/.regress

Transpose[(SinglePredictionCITable/.regress)[[1]]]

{observed,predicted,se,ci}=Transpose[(SinglePredictionCITable/.regress)[[1]]];

predicted=Transpose[{xval,predicted}]

lowerCI=Transpose[{xval,First/@ci}]

upperCI=Transpose[{xval,Last/@ci}]

ListPlot[{data,predicted,lowerCI,upperCI},Joined->{False,True,True,True},PlotStyle->{Automatic,Automatic,{Dashing[{.05,.05}],Gray},{Dashing[{.05,.05}],Gray}}]

Pre regresnu priamku, najdeme prislusny kvantil, disperziu, 
(Subscript[a, 0])+(Subscript[a, 1])x+(Subscript[a, 2])(x^2)+-(Subscript[F, 3,n-3])(1-(α/2))(Sqrt[(s^2)c'(((X'X))^(-1))c]), c=(1, x, (x^2))

F=Quantile[FRatioDistribution[3,Length[data]-3],0.975]

var=Regress[data,{x,(x^2)},x,RegressionReport->EstimatedVariance]

s1=EstimatedVariance/.var

param=Regress[data,{x,(x^2)},x,RegressionReport->BestFitParameters]

parametre=BestFitParameters/.param

a0=parametre[[1]];a1=parametre[[2]];a2=parametre[[3]];

n=Length[data]

Xmatica=Array[aa,{10,3}]

Do[aa[i,1]=1;aa[i,2]=xval[[i]];aa[i,3]=xval[[i]]*xval[[i]],{i,1,10}]

Xmatica//MatrixForm

matica=Inverse[Transpose[Xmatica].Xmatica]

c[t_]:={1,t,t^2}

c[1]

c[2]

Transpose[{c[2]}]

dolnahranica[x_]:=a0+a1*x+a2*(x^2)-F*(Sqrt[s1*c[x].matica.Transpose[{c[x]}]])

hornahranica[x_]:=a0+a1*x+a2*(x^2)+F*(Sqrt[s1*c[x].matica.Transpose[{c[x]}]])

dolnahranica[1]

n=Length[data]

Do[dh[i]=dolnahranica[xval[[i]]];hh[i]=hornahranica[xval[[i]]],{i,n}]

lower=Flatten[Table[dh[i],{i,n}]]

upper=Flatten[Table[hh[i],{i,n}]]

dolna=Transpose[{xval,lower}]

horna=Transpose[{xval,upper}]

ListPlot[{data,predicted,lowerCI,upperCI,dolna,horna},Joined->{False,True,True,True,True,True},PlotStyle->{Automatic,Automatic,{Dashing[Large],Green},{Dashing[Large],Green},{Dashing[Tiny],Red},{Dashing[Tiny],Red}}]

Regresia s dvoma nezavislymi premennymi

d = datum, x = priemerne znecistenie vzduchu, z = obsah kyslicnika siriciteho, y = pocty umrti

d=Range[15]

xhod={0.3,0.49,0.61,0.49,2.64,3.45,4.46,4.46,1.22,1.22,0.32,0.29,0.5,0.32,0.32}

zhod={0.09,0.16,0.22,0.14,0.75,0.86,1.34,1.34,0.47,0.47,0.22,0.23,0.26,0.16,0.16}

yhod={112,140,143,120,196,294,513,518,430,274,255,236,256,222,213}

Vykreslime hodnoty (tri 2D grafy) vzhladom na datum merania, hodnoty x a z su prenasobene 100, hodnotu y vzhladom na (x,z)

xdata=Table[{d[[i]],100*xhod[[i]]},{i,15}];
ydata=Table[{d[[i]],yhod[[i]]},{i,15}];
zdata=Table[{d[[i]],100*zhod[[i]]},{i,15}];

ListPlot[{xdata,zdata,ydata},Joined->{True,True,True},PlotMarkers->Automatic]

regresnedata=Table[{xhod[[i]],zhod[[i]],yhod[[i]]},{i,15}]

g1=ListPointPlot3D[regresnedata,PlotStyle->PointSize[Large],Filling->Bottom,Boxed->False]

reg=Regress[regresnedata,{x,z},{x,z}]

krivka=Regress[regresnedata,{x,z},{x,z},RegressionReport->BestFit]

funkcia=BestFit/.krivka

g2=Plot3D[funkcia,{x,Min[xhod],Max[xhod]},{z,Min[zhod],Max[zhod]}]

Show[g1,g2]