 # How to plot a Cubic polynomial function?

Hello
I've run a panel Fixed Effects model, and i would like to make a graph which detail a part of my result:

``````fe_model <- plm(formula =invest~lag(debt)+lag(I(debt^2))+ lag(I(debt^3))+bcour+gvex+tradeop,data = DataN, model = "within",index = c("country","year"))
``
Coefficients:
Estimate  Std. Error t-value  Pr(>|t|)
lag(debt)       -0.0913478  0.02180357  6.1512 2.517e-09 ***
lag(I(debt^2)) 0.002783274  0.00010127 -5.7105 2.762e-08 ***
lag(I(debt^3))  -0.0000192  0.01620570  1.2259   0.22120 ***
bcour           0.23662511  0.03926279  6.0267 5.018e-09 ***
gvex           -0.52866311  0.12588780 -4.1995 3.553e-05 ***
tradeop        -3.57865159  1.58891292 -2.2523   0.02505 *
``````

[/quote]

What i want, its to plot (graph) the cubic relation between invest and debt by using each debt coefficients foud in my Fixed Effect model:

plot : invest~lag(debt)+lag(I(debt^2))+ lag(I(debt^3))

Have you tried using the predict() function? You would need to make a data frame, let's call it NewDF, containing various values of debt and fixed values of bcour, gvex, and tradeop. Then you could get predicted values of invest with

``````NewDF\$PredInvest <- predict(fe_model, newdata = NewDF)
``````

I have never used plm but I think that will work. You can then plot with

``````library(ggplot2)
ggplot(NewDF, aes(x = debt, y = PredInvest)) + geom_line()
``````

Hi @FJCC, Thanks for your help. Did you please try with your own data for example?In fact i've tried this but i could not make a plot due to the below error message ;

ggplot(d.NewDFf, aes(x = debt, y = PredInvest)) + geom_line()
Error in FUN(X[[i]], ...) : objet 'debt' introuvable

The message means it couldn't found the debt object.
So, i don't know if you foud the same issu?

Does d.NewDFf contain a column named debt? You can check this with

``````colnames(d.NewDFf)
``````

It should contain the debt values that were used to generate PredInvest. Can you show the code you used to generate PredInvest?

Yes, kindly see here below the details;

#change my data to data.fram#
DataBN.p = data.frame(DataBN,index = c("country","year"))
#model regression#
fe_model=plm(formula =PredInvest~lag(debt)+lag(I(debt^2))+ lag(I(debt^3))+bcour+gvex+tradeop,data = DataBN.p, model = "within",index = c("country","year"))
summary(fe_model)

#create a variable for predict model (fe_model) result#
NewDFf <- predict(fe_mod, Data = NewDF)
library(ggplot2)
#create a data.frame ton contain predict#
d.NewDFf = data.frame(NewDFf)
ggplot(d.NewDFf, aes(x = debt, y = PredInvest)) + geom_line()

The colnames(d.NewDFf) doesn't contain a a column named debt. And you are right, i think it doesn't work perhaps because the column is empty.
Regards,

The step you are missing is putting data in NewDF. I do not know what kind of debt you a dealing with so I will assume personal debt and I will put values from 0 to 500,000 in NewDF and set bcour, gvex, and tradeop to 1. You will have to adjust all of that to use appropriate numbers.

``````NewDF <- data.frame(debt = seq(0, 500000, 50000), bcour = 1, gvex = 1, tradeop = 1)
NewDF\$PredInvest <- predict(fe_model, newdata = NewDF)
``````

You should then be able to plot the result.

1 Like

Yes, now it seems working well.
Thanks you dear @FJCC for your kind assistance.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.