Plotting Y=f(X) after a regression where X contains 2 predictors

조회 수: 1 (최근 30일)
AwedBy Matlab
AwedBy Matlab 2014년 8월 1일
댓글: Tom Lane 2014년 8월 14일
I used
mdl=fitlm(X,y)
to regress the y vector as a function of two predictors x1 and x2, which together form the X matrix.
I would like to obtain a plot of y=f(x1), with the data points, regression line and regression bands displayed. I know I can do all of this manually but I'm wondering if there's an easier way. I can see that plot(mdl) gives me an added variable plot with adjusted values, which in this case is not helpful, and I can see that there are no options to single out a single predictor for the plot - or is it the case that it would not make sense statistically to only plot y=f(x1) as long as there is an x2 predictor as well?
Thanks!

답변 (3개)

Star Strider
Star Strider 2014년 8월 1일
Does predict do what you want?
  댓글 수: 2
AwedBy Matlab
AwedBy Matlab 2014년 8월 1일
I don't think so, as that just seems to give me the y values of the regression line, so it would be the same as manually constructing the plot with the data points, regression line and confidence bands
Star Strider
Star Strider 2014년 8월 1일
You could plot f(x1) with your independent variable and some arbitrary single value for x2, but the error bars would likely not be meaningful, and I doubt you would get any meaningful information from the plot.
If you want to understand the effect of each parameter, your best option is to take the Jacobian of your function with respect to each parameter, [ df(x1,x2)/dx1 df(x1,x2)/dx2 ] where d here is understood to be the partial derivative.

댓글을 달려면 로그인하십시오.


Ahmet Cecen
Ahmet Cecen 2014년 8월 1일
It doesn't make sense to plot y vs f(x1). Here you have several options.
1) You can plot y vs x1 and y vs x2, which can show you how well each variable agrees with y. Cftool is an easy way to play around fits and scatter plots.
2) You can plot y vs x2 vs x1, which is another way to see how variables agree with y. (Cftool does this too)
3) You can plot y vs f(x1,x2) as a scatter plot, which is arguably the most meaningful thing you can plot.
There are a variety of diagnostic plots you can go for if your answer is unsatisfactory. I won't get into those here, check linear regression (assumptions) on wikipedia.
  댓글 수: 3
Ahmet Cecen
Ahmet Cecen 2014년 8월 2일
I am not aware of any such pre-loaded figure functions myself. The best I can suggest is to use a scatter() than a line() function and manually format the figure in the GUI. Afterwards you can go File-Generate Code, thus creating a function that can generate the figure instantly thereafter.

댓글을 달려면 로그인하십시오.


Tom Lane
Tom Lane 2014년 8월 8일
Have you considered the plotSlice function? It will plot predicted y vs. x1 for a fixed value of x2, and predicted y vs. x2 for a fixed value of x1.
  댓글 수: 2
AwedBy Matlab
AwedBy Matlab 2014년 8월 8일
I see, but does it also plot the regression line&bands, in addition to the individual data points?
Tom Lane
Tom Lane 2014년 8월 14일
It plots the line and bands, but not the points unless there is just one predictor. That's because, for example, with two predictors a plot of f(x1,5) is not comparable to all the data points, only the points with x2=5.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Smoothing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by