How to display a linear regression line along with the scatter plot

247 次查看(过去 30 天)
I want to diplay a linear regression line (y=b+mx) along with the scattered plot, but I got stuck.
I need only to display the scatter points and the regression line (y=b+mx) without confidence bounds.
Moreover, the scatter points (dots) should remain the same, instead of x.
I wonder if I could get help.
Thank you in advance.
%Two data x and y
x= [0.0630983945707523 0.0569310761963337 0.0902849174433386 0.0586557564536381 0.0334144697267278 0.0712631880471929 0.0759936968192505 0.123738339182888 0.0297032637109049 0.0795892282583819 0.0282261596294058 0.0537183249987105 0.150572257432409 0.0977963699033021 0.0885269208326175 0.0866396385249492 0.0669298504865110 0.0344312413925116 0.0348667960896803 0.0493656188493081 0.0270650538299466 0.0380968337434081 0.0888445642954378 0.0895945298885049 0.0541359853955182 0.0744321995464817 0.0913504586635817 0.0656554300621712 0.117259195293069 0.0713277353350769 0.0337360552372940 0.0806738045893977 0.0187938769214277 0.112203928934848 0.0344890616611609 0.112070100462341 0.0354340210241964 0.0485753458099790 0.100718883223941 0.0875411740696460 0.0356133031858813 0.0582817986356205 0.0482790634228989 0.158997322992062 0.0729992290097077 0.0471244743021920 0.0446084809659350 0.0511316023897959 0.0506496253843976 0.149380752893838 0.0689045480827398 0.0518533061920089 0.0209788552111653 0.0853321217563026 0.0933452759672151 0.0632321122010304 0.0989045624536658 0.118646945395764 0.0746394491425817 0.0563364973414752 0.0699405386310658 0.176313530448904 0.0805085967423252 0.0711951706934392 0.0985873777833532 0.0361123753750750 0.107620322990626 0.155953011309276 0.0703844359222595 0.0698300349659653 0.0133693291619266 0.0563251538076025 0.0420594335773483 0.0209929807249319 0.0836480611756999 0.0918760153342581 0.109071023988030 0.112011148456982 0.0932946841816926 0.0527273305572443 0.0603965022689918 0.0997831970370141 0.0865565948550563 0.0451864919017301 0.0534928555898882 0.0753611849580176 0.0975080705342945 0.0641363967713138 0.0492528378137929 0.0115895963831613 0.0395675354645857 0.0543562812505730 0.0302912028316128 0.0952996657586537 0.0735569358213397 0.0204415291346991 0.0291487407942866 0.0677917391211721 0.0439215658951729 0.0333816549041610 0.0456403551952354 0.0486553399302202 0.0470538432901701 0.0811957857944899 0.0515061853483591 0.0914618319303829 0.0545584954664158 0.0496585033306348 0.0394672348319468 0.0551272949007350 0.0520644196553922 0.0834432764756573 0.0734959836126599 0.0464406549571500 0.0597728280588548 0.0410213962648829 0.0690404853961215 0.0496809976564987 0.0361669205094010 0.0730322704667141 0.107730597350034 0.0248405355005877 0.0511942382576493 0.0189503937752495 0.0408680288793921 0.120785037290073 0.0594515231221030 0.0328245966542376 0.0548458148686953 0.0529949038865531 0.0791029104470528 0.0554237252866484 0.0994908340125133 0.0542399243766295 0.0534448107553341 0.0673593273333577 0.0939791192900367 0.0823300945762324 0.0442566600240820 0.0610617121274878 0.0898884338644292 0.0589796682804715 0.0976209948973652 0.0868444801571454 0.0652049359417058 0.0537021499678730 0.0301654519501758 0.0512536154351539 0.0345987437628045 0.0779282336255190 0.0825154044337572 0.0859799777142292 0.0442207168368985 0.00987243263527813 0.0950021713184207 0.0613437822551752 0.142441526816436 0.100802430273912 0.0414216701498731 0.109019675608790 0.0296913082336243 0.0347742860159156 0.135809763420103 0.0984506029234962 0.0674037418593082 0.0229407666296069 0.0189996852270467 0.0743571335114989 0.0847192814122880 0.115083690612888 0.123696178966709 0.0956417803959083 0.0519792874058779 0.0393076075577976 0.0677365377410694 0.0591426857839045 0.0847946264671823 0.0606756648202872 0.0724067734299440 0.0430835201376981 0.0808110853292779 0.0498423375322701 0.0194708896974106 0.0751131186727741 0.0348726389820165 0.0740612288670435 0.0594335516286884 0.0594348198017655 0.0682406308413635 0.0531894655861687 0.0743790302594293 0.0219558176107492 0.0272123662258039 0.108538439537859 0.0747059896282369 0.0946858822329740 0.0569276702396826 0.0149383525169137 0.0598050184040844 0.0545400309304210 0.0343002731803569 0.0811687405215188 0.0388267954856432 0.108631800100204 0.0674577426363181 0.0568544341837804 0.0108167619730890 0.0423029642373116 0.0377751860077256 0.0393552722330052 0.0710153987147849 0.121832351138184 0.0769382376509565 0.0943585640526814 0.0257138841804495 0.106163904814368 0.0364331244691357 0.0604418270748409 0.0738744794980614 0.137693441001674 0.0459022534232133];
y= [0.0250861693912511 0.00585190458464980 0.0680369926791528 0.0123549562586842 0.0282578426263928 0.0289878878512877 0.0183247815240238 0.0264046769351247 0.0298394588829129 0.0184375401541612 0.0128234393998260 0.0252050387926058 0.0295589374777074 0.0172581512714285 0.0198322914317110 0.0510612122109021 0.0265392101972772 0.0166542459114324 0.0614014018743578 0.0121560004556879 0.00718748693092065 0.0153044736253397 0.0293176969573287 0.0175869174921616 0.0114693237575025 0.0506590001559409 0.00610465839888697 0.0126487109773635 0.0702555922896282 0.0418570299060915 0.00236060532142230 0.0455516831459313 0.0110800140143406 0.0220056298859820 0.0105652706864878 0.0373136496652635 0.0387907583399752 0.0405489056158515 0.0548330383830699 0.0313105046274129 0.0163361492031127 0.0224256799386937 0.0340918357832205 0.0856587592079277 0.0237281965942535 0.0185048402639438 0.0160553627979275 0.0248764504594100 0.00671041155944468 0.00860225270339545 0.0284231520878838 0.0148858821441466 0.0240840534643192 0.0324876487500475 0.0186392352007912 0.0290360108040825 0.0392029319357457 0.00514774680763223 0.0403734491344147 0.00553514948128624 0.0159472912736301 0.0391014747411382 0.0124504294222637 0.0356226945365535 0.0181628348862609 0.0178681779187954 0.00852772653415102 0.0138155179277036 0.0128797506883640 0.0175729080257820 0.0332649622959198 0.0132144919616132 0.0142189960587721 0.00867472220872289 0.0138585643963398 0.0808096300868375 0.0275859559565118 0.0430658837925290 0.0437529579942120 0.0432143087750007 0.0216644020803545 0.0282467031310019 0.0264198042749484 0.0186536205834701 0.0204944617589289 0.0161420750508947 0.0419642104728729 0.0266222127717096 0.0238260684162398 0.0167323386525880 0.0211166237490162 0.0138489451454549 0.0171711493485741 0.0198154734805207 0.0288256016492744 0.0217744398062373 0.0224640137433889 0.0119203543107133 0.0185285105932922 0.0276843904380591 0.0260460293444823 0.0116739173491231 0.0364410484579242 0.0261799258765370 0.0143295728798527 0.0618040252119864 0.0360762297813938 0.0299219101457212 0.0122104419238876 0.0224533153865488 0.0466342969006853 0.0439241647906221 0.0239186659728951 0.0429536063056506 0.0233204180398710 0.0257952340865214 0.0442263810315239 0.00465267102348124 0.00831411975658338 0.0236893052499721 0.0357391132784113 0.0214472226156945 0.0328176016665120 0.0167988766248693 0.0202837833033065 0.0119148052105929 0.0228826867462709 0.0343781708171176 0.0540834236256089 0.0253221711808191 0.0468323896367522 0.0742686819509908 0.0149921603233425 0.0299363682861656 0.0140422800476185 0.00923466574443423 0.0339342465603126 0.0291556774581404 0.0264800566361867 0.0254096416475342 0.0272339901951617 0.0306175784023154 0.0265822813377005 0.0285688811434644 0.0264281844291106 0.00846994973923620 0.0126390347157110 0.0117823693638192 0.0106438568823798 0.0262067037531181 0.0321805438741519 0.0148750170389071 0.0152049101084041 0.0107917917822201 0.0360250109804213 0.0227016064329870 0.0213593475134725 0.0244221264928307 0.0411664668012472 0.0401967822462838 0.00310215060314217 0.0332718783908383 0.0529584710776588 0.0135445495737136 0.00860916487815600 0.0279323739814191 0.0160524304304993 0.0171457403515751 0.0135776547989487 0.0106334907007662 0.0283472514790111 0.00776385651946830 0.0239886330282252 0.0199217325793269 0.0168300020490545 0.0198169217822589 0.0101689024408679 0.0421715201897865 0.0150827502752557 0.0226402615670400 0.0384003754097582 0.0243950799583878 0.0221702790775174 0.00567635788148187 0.0184935680511484 0.00957457983846342 0.0394434047176948 0.0137801538455711 0.00669108366533619 0.0175697985610505 0.0257116011994790 0.0206042977054387 0.0135485237464691 0.00741308386535177 0.0179047140558557 0.0258981916037672 0.0157092482144979 0.0301373200050465 0.0137831539061441 0.0171413050125207 0.0150572044609457 0.00938318206721905 0.0288920956887529 0.0568936987092498 0.0563216023427668 0.0238673837411345 0.00797360309686464 0.0204317022233995 0.0326997916439986 0.0726127769302676 0.0151435039662788 0.0123217164808681 0.0183259874692433 0.0214846876559265 0.0140200853872136 0.0376692550080025 0.0157611272316032 0.0258560042059497 0.0146939649993002 0.0253102202706720 0.0188959246844452];
disp('Scatter graph ');
Scatter graph
scatter(x',y','.')
xlabel('x')
ylabel('y')
mdl = fitlm(x,y)
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _________ ______ __________ (Intercept) 0.017481 0.0022911 7.6298 7.1359e-13 x1 0.1106 0.031273 3.5367 0.00049439 Number of observations: 221, Error degrees of freedom: 219 Root Mean Squared Error: 0.0144 R-squared: 0.054, Adjusted R-Squared: 0.0497 F-statistic vs. constant model: 12.5, p-value = 0.000494
plot(mdl)
%Here I need to display only the scatter points and the regression line (y=1+x) without confidence bounds.

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-2-15
%Two data x and y
x = [0.0630983945707523 0.0569310761963337 0.0902849174433386 0.0586557564536381 0.0334144697267278 0.0712631880471929 0.0759936968192505 0.123738339182888 0.0297032637109049 0.0795892282583819 0.0282261596294058 0.0537183249987105 0.150572257432409 0.0977963699033021 0.0885269208326175 0.0866396385249492 0.0669298504865110 0.0344312413925116 0.0348667960896803 0.0493656188493081 0.0270650538299466 0.0380968337434081 0.0888445642954378 0.0895945298885049 0.0541359853955182 0.0744321995464817 0.0913504586635817 0.0656554300621712 0.117259195293069 0.0713277353350769 0.0337360552372940 0.0806738045893977 0.0187938769214277 0.112203928934848 0.0344890616611609 0.112070100462341 0.0354340210241964 0.0485753458099790 0.100718883223941 0.0875411740696460 0.0356133031858813 0.0582817986356205 0.0482790634228989 0.158997322992062 0.0729992290097077 0.0471244743021920 0.0446084809659350 0.0511316023897959 0.0506496253843976 0.149380752893838 0.0689045480827398 0.0518533061920089 0.0209788552111653 0.0853321217563026 0.0933452759672151 0.0632321122010304 0.0989045624536658 0.118646945395764 0.0746394491425817 0.0563364973414752 0.0699405386310658 0.176313530448904 0.0805085967423252 0.0711951706934392 0.0985873777833532 0.0361123753750750 0.107620322990626 0.155953011309276 0.0703844359222595 0.0698300349659653 0.0133693291619266 0.0563251538076025 0.0420594335773483 0.0209929807249319 0.0836480611756999 0.0918760153342581 0.109071023988030 0.112011148456982 0.0932946841816926 0.0527273305572443 0.0603965022689918 0.0997831970370141 0.0865565948550563 0.0451864919017301 0.0534928555898882 0.0753611849580176 0.0975080705342945 0.0641363967713138 0.0492528378137929 0.0115895963831613 0.0395675354645857 0.0543562812505730 0.0302912028316128 0.0952996657586537 0.0735569358213397 0.0204415291346991 0.0291487407942866 0.0677917391211721 0.0439215658951729 0.0333816549041610 0.0456403551952354 0.0486553399302202 0.0470538432901701 0.0811957857944899 0.0515061853483591 0.0914618319303829 0.0545584954664158 0.0496585033306348 0.0394672348319468 0.0551272949007350 0.0520644196553922 0.0834432764756573 0.0734959836126599 0.0464406549571500 0.0597728280588548 0.0410213962648829 0.0690404853961215 0.0496809976564987 0.0361669205094010 0.0730322704667141 0.107730597350034 0.0248405355005877 0.0511942382576493 0.0189503937752495 0.0408680288793921 0.120785037290073 0.0594515231221030 0.0328245966542376 0.0548458148686953 0.0529949038865531 0.0791029104470528 0.0554237252866484 0.0994908340125133 0.0542399243766295 0.0534448107553341 0.0673593273333577 0.0939791192900367 0.0823300945762324 0.0442566600240820 0.0610617121274878 0.0898884338644292 0.0589796682804715 0.0976209948973652 0.0868444801571454 0.0652049359417058 0.0537021499678730 0.0301654519501758 0.0512536154351539 0.0345987437628045 0.0779282336255190 0.0825154044337572 0.0859799777142292 0.0442207168368985 0.00987243263527813 0.0950021713184207 0.0613437822551752 0.142441526816436 0.100802430273912 0.0414216701498731 0.109019675608790 0.0296913082336243 0.0347742860159156 0.135809763420103 0.0984506029234962 0.0674037418593082 0.0229407666296069 0.0189996852270467 0.0743571335114989 0.0847192814122880 0.115083690612888 0.123696178966709 0.0956417803959083 0.0519792874058779 0.0393076075577976 0.0677365377410694 0.0591426857839045 0.0847946264671823 0.0606756648202872 0.0724067734299440 0.0430835201376981 0.0808110853292779 0.0498423375322701 0.0194708896974106 0.0751131186727741 0.0348726389820165 0.0740612288670435 0.0594335516286884 0.0594348198017655 0.0682406308413635 0.0531894655861687 0.0743790302594293 0.0219558176107492 0.0272123662258039 0.108538439537859 0.0747059896282369 0.0946858822329740 0.0569276702396826 0.0149383525169137 0.0598050184040844 0.0545400309304210 0.0343002731803569 0.0811687405215188 0.0388267954856432 0.108631800100204 0.0674577426363181 0.0568544341837804 0.0108167619730890 0.0423029642373116 0.0377751860077256 0.0393552722330052 0.0710153987147849 0.121832351138184 0.0769382376509565 0.0943585640526814 0.0257138841804495 0.106163904814368 0.0364331244691357 0.0604418270748409 0.0738744794980614 0.137693441001674 0.0459022534232133];
y = [0.0250861693912511 0.00585190458464980 0.0680369926791528 0.0123549562586842 0.0282578426263928 0.0289878878512877 0.0183247815240238 0.0264046769351247 0.0298394588829129 0.0184375401541612 0.0128234393998260 0.0252050387926058 0.0295589374777074 0.0172581512714285 0.0198322914317110 0.0510612122109021 0.0265392101972772 0.0166542459114324 0.0614014018743578 0.0121560004556879 0.00718748693092065 0.0153044736253397 0.0293176969573287 0.0175869174921616 0.0114693237575025 0.0506590001559409 0.00610465839888697 0.0126487109773635 0.0702555922896282 0.0418570299060915 0.00236060532142230 0.0455516831459313 0.0110800140143406 0.0220056298859820 0.0105652706864878 0.0373136496652635 0.0387907583399752 0.0405489056158515 0.0548330383830699 0.0313105046274129 0.0163361492031127 0.0224256799386937 0.0340918357832205 0.0856587592079277 0.0237281965942535 0.0185048402639438 0.0160553627979275 0.0248764504594100 0.00671041155944468 0.00860225270339545 0.0284231520878838 0.0148858821441466 0.0240840534643192 0.0324876487500475 0.0186392352007912 0.0290360108040825 0.0392029319357457 0.00514774680763223 0.0403734491344147 0.00553514948128624 0.0159472912736301 0.0391014747411382 0.0124504294222637 0.0356226945365535 0.0181628348862609 0.0178681779187954 0.00852772653415102 0.0138155179277036 0.0128797506883640 0.0175729080257820 0.0332649622959198 0.0132144919616132 0.0142189960587721 0.00867472220872289 0.0138585643963398 0.0808096300868375 0.0275859559565118 0.0430658837925290 0.0437529579942120 0.0432143087750007 0.0216644020803545 0.0282467031310019 0.0264198042749484 0.0186536205834701 0.0204944617589289 0.0161420750508947 0.0419642104728729 0.0266222127717096 0.0238260684162398 0.0167323386525880 0.0211166237490162 0.0138489451454549 0.0171711493485741 0.0198154734805207 0.0288256016492744 0.0217744398062373 0.0224640137433889 0.0119203543107133 0.0185285105932922 0.0276843904380591 0.0260460293444823 0.0116739173491231 0.0364410484579242 0.0261799258765370 0.0143295728798527 0.0618040252119864 0.0360762297813938 0.0299219101457212 0.0122104419238876 0.0224533153865488 0.0466342969006853 0.0439241647906221 0.0239186659728951 0.0429536063056506 0.0233204180398710 0.0257952340865214 0.0442263810315239 0.00465267102348124 0.00831411975658338 0.0236893052499721 0.0357391132784113 0.0214472226156945 0.0328176016665120 0.0167988766248693 0.0202837833033065 0.0119148052105929 0.0228826867462709 0.0343781708171176 0.0540834236256089 0.0253221711808191 0.0468323896367522 0.0742686819509908 0.0149921603233425 0.0299363682861656 0.0140422800476185 0.00923466574443423 0.0339342465603126 0.0291556774581404 0.0264800566361867 0.0254096416475342 0.0272339901951617 0.0306175784023154 0.0265822813377005 0.0285688811434644 0.0264281844291106 0.00846994973923620 0.0126390347157110 0.0117823693638192 0.0106438568823798 0.0262067037531181 0.0321805438741519 0.0148750170389071 0.0152049101084041 0.0107917917822201 0.0360250109804213 0.0227016064329870 0.0213593475134725 0.0244221264928307 0.0411664668012472 0.0401967822462838 0.00310215060314217 0.0332718783908383 0.0529584710776588 0.0135445495737136 0.00860916487815600 0.0279323739814191 0.0160524304304993 0.0171457403515751 0.0135776547989487 0.0106334907007662 0.0283472514790111 0.00776385651946830 0.0239886330282252 0.0199217325793269 0.0168300020490545 0.0198169217822589 0.0101689024408679 0.0421715201897865 0.0150827502752557 0.0226402615670400 0.0384003754097582 0.0243950799583878 0.0221702790775174 0.00567635788148187 0.0184935680511484 0.00957457983846342 0.0394434047176948 0.0137801538455711 0.00669108366533619 0.0175697985610505 0.0257116011994790 0.0206042977054387 0.0135485237464691 0.00741308386535177 0.0179047140558557 0.0258981916037672 0.0157092482144979 0.0301373200050465 0.0137831539061441 0.0171413050125207 0.0150572044609457 0.00938318206721905 0.0288920956887529 0.0568936987092498 0.0563216023427668 0.0238673837411345 0.00797360309686464 0.0204317022233995 0.0326997916439986 0.0726127769302676 0.0151435039662788 0.0123217164808681 0.0183259874692433 0.0214846876559265 0.0140200853872136 0.0376692550080025 0.0157611272316032 0.0258560042059497 0.0146939649993002 0.0253102202706720 0.0188959246844452];
scatter(x,y,'.')
xlabel('x')
ylabel('y')
mdl = fitlm(x,y);
% use handle for plotting
h = plot(mdl)
h =
4×1 Line array: Line (Data) Line (Fit) Line (Confidence bounds) Line
delete(h([3 4])) %delete bounds
  9 个评论
Dyuman Joshi
Dyuman Joshi 2023-2-15
No, it is not the regression fit.
It is an approximation, giving an idea of the order of the equation (via a particular notation).
The regression fit is as mentioned by Les in the comment above.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by