امروز:جمعه, ۱۰ فروردین , ۱۴۰۳
زمان انتشار : جمعه, مهر 25ام, 1393 | پرینت مطلب |مهران فاطمی| بازديد: 6,479 بار

محاسبه گام به گام رگرسیون ،ضریب همبستگی و آمار توصیفی در متلب

دوست خوبمان جناب آقای دوستکامیان مطالب مناسبی را درخصوص نرم افزار مطلب در وبلاگشان ارائه نموده اند که بنا به اهمیت یادگیری این برنامه تقدیم حضورتان می گردد

Example: Computing R۲ from Polynomial Fits

You can derive R۲ from the coefficients of a polynomial regression to determine how much variance in y a linear model explains, as the following example describes:

  1. Create two variables, x and y, from the first two columns of the count variable in the data file count.dat:
    load count.dat
    x = count(:,1);
    y = count(:,2);
  2. Use polyfit to compute a linear regression that predicts y from x:
    p = polyfit(x,y,1)
    
    p =
        ۱٫۵۲۲۹   -۲٫۱۹۱۱
    

    p(1) is the slope and p(2) is the intercept of the linear predictor. You can also obtain regression coefficients using the Basic Fitting GUI.

  3. Call polyval to use p to predict y, calling the result yfit:
    yfit = polyval(p,x);

    Using polyval saves you from typing the fit equation yourself, which in this case looks like:

    yfit =  p(1) * x + p(2);
  4. Compute the residual values as a vector of signed numbers:
    yresid = y - yfit;
  5. Square the residuals and total them to obtain the residual sum of squares:
    SSresid = sum(yresid.^2);
  6. Compute the total sum of squares of y by multiplying the variance of y by the number of observations minus ۱:
    SStotal = (length(y)-1) * var(y);
    
  7. Compute R۲ using the formula given in the introduction of this topic:
    rsq = 1 - SSresid/SStotal
    
    rsq =
        ۰٫۸۷۰۷

    This demonstrates that the linear equation ۱.۵۲۲۹ * x -2.1911 predicts 87% of the variance in the variable y

 

محاسبه ضریب همبستگی در متلب:

همان طوری که قبلا اشاره شد برای محاسبه ضریب همبستگی در متلب از دستور corr استفاده می­کنیم. به مثال زیر توجه کنید:

>> Tem

Tem =

   ۱۰٫۶۹۶۲

   ۱۰٫۵۱۵۷

   ۱۲٫۸۴۸۳

   ۱۵٫۷۵۰۰

   ۱۲٫۷۵۸۳

   ۱۰٫۲۰۹۱

   ۸٫۹۲۳۸

   ۷٫۵۸۴۵

   ۱۱٫۱۳۵۷

   ۱۳٫۸۴۴۷

>> Rain

Rain =

   ۹٫۷۵۰۷

   ۹٫۷۶۴۸

   ۱۱٫۵۵۵۱

   ۱۳٫۳۱۶۵

   ۱۱٫۶۹۲۷

   ۵٫۹۷۹۶

   ۵٫۹۶۳۷

   ۵٫۸۲۰۸

   ۹٫۴۹۴۶

   ۱۲٫۲۴۱۹

در متلب امکان ضریب همبستگی پارامتری و غیر پارامتری به راحتی انجام می­گیرد. در این مواقع کافیست به طرق زیر عمل کنید:

>> [R,PVAL] = corr(a,b,’type’,’pearson’)

R =

   ۰٫۹۲۵۰

PVAL =

   ۱٫۲۶۳۹e-04

>> R2= R^2

R2 =

   ۰٫۸۵۵۶

>> [R,PVAL] = corr(a,b,’type’,’Kendall’)

R =

   ۰٫۸۲۲۲

PVAL =

   ۳٫۵۷۶۹e-04

>> [R,PVAL] = corr(a,b,’type’,’Spearman’)

R =

   ۰٫۹۳۹۴

PVAL =

   ۰ 

clc
P=input(‘data :);
    me=mean(P(:));
    mediann=median(P(:));
    modee=mode(P(:));
    
varr=var(P(:));
stdd=std(P(:));
 cvvv=(stdd/me)*100;
damenetaghirat=(max(P(:))-min(P(:)));
    skewnes=skewness(P(:));
    kurtosi=kurtosis(P(:));
   
maxx=max(P(:));
minn=min(P(:));
prctilec = prctile(P(:),[25 50 75]);
discriptivet=[me,mediann, modee,varr,stdd,cvvv,damenetaghirat,…
    skewnes,kurtosi,maxx,minn];
discriptivet(1,12:14)=prctilec;
discriptivet=(discriptivet)’;

clear me stdd p me me me mediann  modee varr stdd cvvv damenetaghirat
clear skewnes kurtosi prctilec minn maxx p P

 



مطالب مرتبط








avatar

نویسنده: مهران فاطمی

مهران فاطمی دانشجوی دکترای آب و هواشناسی گرایش مخاطرات آب و هوایی دانشگاه یزد