Panel data - Polynomial fitting

Hallo All,
Please I will need help to visualize the fitting of my panel data. I tried the below option but got an error.

library(plm) 
library(stargazer)
d_data <- pdata.frame (data, index = c("country", "time"), row.names = TRUE)

 mod3 <- plm(psratio~Gvar+Yvar+debtratiolagged +poly(debtratiolagged^2) ,effects = "fd",data = d_data)

 ggplot(d_data, aes(x = debtratiolagged, y = psratio)) + geom_point() +   
 geom_smooth(method="plm", formula = psratio~Gvar+Yvar+debtratiolagged +poly(debtratiolagged^2))

# Below errror message
Error in self$layout$PANEL == i : 
  comparison of these types is not implemented

#Below is dataset

structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("france", "greece", "ireland", 
"italy", "spain"), class = "factor"), time = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("2000", 
"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008"
), class = "factor"), debtratio = c(0.656444082, 0.642582116, 
0.672947232, 0.714335405, 0.73915635, 0.756934652, 0.708728612, 
0.698630419, 0.75668994, 1.149301794, 1.177385002, 1.172419371, 
1.120250124, 1.142408803, 1.144968062, 1.078943832, 1.039441518, 
1.025544592, 0.401504435, 0.373848304, 0.35234645, 0.340542596, 
0.327092578, 0.326933575, 0.288380127, 0.283194398, 0.48494, 
1.210265375, 1.201772992, 1.193600418, 1.168348371, 1.173048597, 
1.198675672, 1.17142645, 1.124589065, 1.144046996, 0.664687599, 
0.618633186, 0.603273972, 0.553479307, 0.533709375, 0.50641841, 
0.461602546, 0.421057631, 0.470381313), debtratiolagged = c(0.667915003, 
0.656444082, 0.642582116, 0.672947232, 0.714335405, 0.73915635, 
0.756934652, 0.708728612, 0.698630419, 1.010952058, 1.149301794, 
1.177385002, 1.172419371, 1.120250124, 1.142408803, 1.144968062, 
1.078943832, 1.039441518, 0.49173212, 0.401504435, 0.373848304, 
0.35234645, 0.340542596, 0.327092578, 0.326933575, 0.288380127, 
0.283194398, 1.257765009, 1.210265375, 1.201772992, 1.193600418, 
1.168348371, 1.173048597, 1.198675672, 1.17142645, 1.124589065, 
0.693896976, 0.664687599, 0.618633186, 0.603273972, 0.553479307, 
0.533709375, 0.50641841, 0.461602546, 0.421057631), psratio = c(0.012035919, 
0.011887018, -0.004394202, -0.015176917, -0.010234036, -0.004826981, 
0.000728384, -0.00235114, -0.007996255, 0.029607942, 0.01522251, 
0.004009843, -0.010288165, -0.028055141, -0.009630348, 0.009860723, 
0.002075637, -0.034087434, 0.063164288, 0.019401231, 0.006102295, 
0.01408126, 0.02329294, 0.025130439, 0.038446877, 0.009840465, 
-0.074529506, 0.052244526, 0.029241213, 0.02422795, 0.01413175, 
0.010890698, 0.001449075, 0.011385617, 0.032371003, 0.021718532, 
0.019282427, 0.019649526, 0.018887667, 0.018723281, 0.014659197, 
0.025234597, 0.03330238, 0.030256571, -0.03042252), Gvar = c(-0.004712222, 
-0.008291792, 0.002012657, 0.004307803, 0.007605119, 0.010388632, 
0.000204818, -0.003631645, -0.009587997, 0.026860099, 0.004072491, 
-0.001439887, 0.009775558, 0.029098676, -0.026676188, -0.049963125, 
-0.017539132, 0.039530358, -0.037999127, 0.019219114, 0.026820726, 
-0.003402595, -0.015679321, -0.018838617, -0.023555133, 0.019388286, 
0.062794296, -0.045206759, 0.00564615, -0.010231218, 0.00037246, 
-0.007057622, -0.0012214, 0.019660039, 0.00491315, 0.001952561, 
-0.015221727, -0.016177007, -0.008542232, -0.020393622, -0.007854358, 
-0.018460224, -0.017550073, -0.000626516, 0.009996432), Yvar = c(0.021226031, 
0.017211769, 0.007239468, -0.002049028, 0.001779343, 0.002843716, 
0.009516547, 0.01507843, 0.00415123, -0.008793841, -0.001005127, 
-0.001341636, 0.01934498, 0.027531262, 0.011869937, 0.018643919, 
0.02652024, 0.009814725, 0.058270348, 0.044979, 0.04036418, 0.020138016, 
0.005123791, 0.007591272, 0.004673169, 0.008775165, -0.075327141, 
0.014623925, 0.017801442, 0.009918604, -0.001129901, 0.001284105, 
0.001405439, 0.014689731, 0.0200741, 0.00524348, 0.014466906, 
0.014893907, 0.008355232, 0.005440263, 0.005686076, 0.010470633, 
0.020533181, 0.02732526, 0.005050353)), row.names = c("france-2000", 
"france-2001", "france-2002", "france-2003", "france-2004", "france-2005", 
"france-2006", "france-2007", "france-2008", "greece-2000", "greece-2001", 
"greece-2002", "greece-2003", "greece-2004", "greece-2005", "greece-2006", 
"greece-2007", "greece-2008", "ireland-2000", "ireland-2001", 
"ireland-2002", "ireland-2003", "ireland-2004", "ireland-2005", 
"ireland-2006", "ireland-2007", "ireland-2008", "italy-2000", 
"italy-2001", "italy-2002", "italy-2003", "italy-2004", "italy-2005", 
"italy-2006", "italy-2007", "italy-2008", "spain-2000", "spain-2001", 
"spain-2002", "spain-2003", "spain-2004", "spain-2005", "spain-2006", 
"spain-2007", "spain-2008"), class = c("pdata.frame", "data.frame"
), index = structure(list(country = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("france", "greece", 
"ireland", "italy", "spain"), class = "factor"), time = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("2000", 
"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008"
), class = "factor")), class = c("pindex", "data.frame"), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L)))

I'm thinking along the following lines.

library(plm) 
library(stargazer)
library(ggplot2)
# d_data <- pdata.frame (data, index = c("country", "time"), row.names = TRUE)

d_data <- structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("france", "greece", "ireland", 
 "italy", "spain"), class = "factor"), time = structure(c(1L, 
  2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
  9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 
  7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("2000", 
  "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008"
  ), class = "factor"), debtratio = c(0.656444082, 0.642582116, 
  0.672947232, 0.714335405, 0.73915635, 0.756934652, 0.708728612, 
  0.698630419, 0.75668994, 1.149301794, 1.177385002, 1.172419371, 
  1.120250124, 1.142408803, 1.144968062, 1.078943832, 1.039441518, 
  1.025544592, 0.401504435, 0.373848304, 0.35234645, 0.340542596, 
  0.327092578, 0.326933575, 0.288380127, 0.283194398, 0.48494, 
  1.210265375, 1.201772992, 1.193600418, 1.168348371, 1.173048597, 
  1.198675672, 1.17142645, 1.124589065, 1.144046996, 0.664687599, 
  0.618633186, 0.603273972, 0.553479307, 0.533709375, 0.50641841, 
  0.461602546, 0.421057631, 0.470381313), debtratiolagged = c(0.667915003, 
  0.656444082, 0.642582116, 0.672947232, 0.714335405, 0.73915635, 
  0.756934652, 0.708728612, 0.698630419, 1.010952058, 1.149301794, 
  1.177385002, 1.172419371, 1.120250124, 1.142408803, 1.144968062, 
  1.078943832, 1.039441518, 0.49173212, 0.401504435, 0.373848304, 
  0.35234645, 0.340542596, 0.327092578, 0.326933575, 0.288380127, 
  0.283194398, 1.257765009, 1.210265375, 1.201772992, 1.193600418, 
  1.168348371, 1.173048597, 1.198675672, 1.17142645, 1.124589065, 
  0.693896976, 0.664687599, 0.618633186, 0.603273972, 0.553479307, 
  0.533709375, 0.50641841, 0.461602546, 0.421057631), psratio = c(0.012035919, 
  0.011887018, -0.004394202, -0.015176917, -0.010234036, -0.004826981, 
  0.000728384, -0.00235114, -0.007996255, 0.029607942, 0.01522251, 
  0.004009843, -0.010288165, -0.028055141, -0.009630348, 0.009860723, 
  0.002075637, -0.034087434, 0.063164288, 0.019401231, 0.006102295, 
  0.01408126, 0.02329294, 0.025130439, 0.038446877, 0.009840465, 
  -0.074529506, 0.052244526, 0.029241213, 0.02422795, 0.01413175, 
  0.010890698, 0.001449075, 0.011385617, 0.032371003, 0.021718532, 
  0.019282427, 0.019649526, 0.018887667, 0.018723281, 0.014659197, 
  0.025234597, 0.03330238, 0.030256571, -0.03042252), Gvar = c(-0.004712222, 
 -0.008291792, 0.002012657, 0.004307803, 0.007605119, 0.010388632, 
 0.000204818, -0.003631645, -0.009587997, 0.026860099, 0.004072491, 
 -0.001439887, 0.009775558, 0.029098676, -0.026676188, -0.049963125, 
 -0.017539132, 0.039530358, -0.037999127, 0.019219114, 0.026820726, 
 -0.003402595, -0.015679321, -0.018838617, -0.023555133, 0.019388286, 
 0.062794296, -0.045206759, 0.00564615, -0.010231218, 0.00037246, 
 -0.007057622, -0.0012214, 0.019660039, 0.00491315, 0.001952561, 
 -0.015221727, -0.016177007, -0.008542232, -0.020393622, -0.007854358, 
 -0.018460224, -0.017550073, -0.000626516, 0.009996432), Yvar = c(0.021226031, 
  0.017211769, 0.007239468, -0.002049028, 0.001779343, 0.002843716, 
  0.009516547, 0.01507843, 0.00415123, -0.008793841, -0.001005127, 
  -0.001341636, 0.01934498, 0.027531262, 0.011869937, 0.018643919, 
  0.02652024, 0.009814725, 0.058270348, 0.044979, 0.04036418, 0.020138016, 
  0.005123791, 0.007591272, 0.004673169, 0.008775165, -0.075327141, 
  0.014623925, 0.017801442, 0.009918604, -0.001129901, 0.001284105, 
  0.001405439, 0.014689731, 0.0200741, 0.00524348, 0.014466906, 
  0.014893907, 0.008355232, 0.005440263, 0.005686076, 0.010470633, 
  0.020533181, 0.02732526, 0.005050353)), row.names = c("france-2000", 
  "france-2001", "france-2002", "france-2003", "france-2004", "france-2005", 
  "france-2006", "france-2007", "france-2008", "greece-2000", "greece-2001", 
  "greece-2002", "greece-2003", "greece-2004", "greece-2005", "greece-2006", 
  "greece-2007", "greece-2008", "ireland-2000", "ireland-2001", 
  "ireland-2002", "ireland-2003", "ireland-2004", "ireland-2005", 
  "ireland-2006", "ireland-2007", "ireland-2008", "italy-2000", 
  "italy-2001", "italy-2002", "italy-2003", "italy-2004", "italy-2005", 
  "italy-2006", "italy-2007", "italy-2008", "spain-2000", "spain-2001", 
  "spain-2002", "spain-2003", "spain-2004", "spain-2005", "spain-2006", 
  "spain-2007", "spain-2008"), class = c("pdata.frame", "data.frame"
  ), index = structure(list(country = structure(c(1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
  3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("france", "greece", 
  "ireland", "italy", "spain"), class = "factor"), time = structure(c(1L, 
  2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
  9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 
  7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("2000", 
  "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008"
  ), class = "factor")), class = c("pindex", "data.frame"), row.names = c(1L, 
  2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 19L, 20L, 21L, 22L, 23L, 24L, 
  25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
  38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 10L, 11L, 12L, 13L, 14L, 
  15L, 16L, 17L, 18L)))

mod3 <- plm(psratio~Gvar+Yvar+poly(debtratiolagged,2) ,
            model = "fd",
            effect="individual",
            data = d_data)

d_data$predicted_vals <- predict(mod3,newdata = d_data)

ggplot(as.data.frame(d_data), aes(x = debtratiolagged, y = psratio)) + 
  geom_point() + 
  geom_point(aes(y=predicted_vals),color="red") +
  geom_smooth(aes(y=predicted_vals),color="orange")

Thank you for the support

Hello,

geom_smooth() doesn't take plm as an argument. It looks giving it a PKG::function () might work.

method	
Smoothing method (function) to use, accepts either NULL or a character vector, e.g. "lm", "glm", "gam", "loess" or a function, e.g. MASS::rlm or mgcv::gam, stats::lm, or stats::loess. "auto" is also accepted for backwards compatibility. It is equivalent to NULL

Thanks fredoxvii for the feedback. Please can you help with the proposed PKG::function () ?

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.