library(plm)
library(readxl)

#read data
df <- read_excel("Pak Indra Data Panel.xlsx")
head(df)
#data panel
df_p <- pdata.frame(df, index = c("Perusahaan", "Tahun"))
head(df_p)
#regresi panel
pooling_ecik <- plm(Profit ~ Ekonomi + Coal + Inflasi + Kurs, 
               data = df_p, model = "pooling")
model_fe_ecik <- plm(Profit ~ Ekonomi + Coal + Inflasi + Kurs,
                data  = df_p, model = "within")
model_re_ecik <- plm(Profit ~ Ekonomi + Coal + Inflasi + Kurs,
                data  = df_p, model = "random")

#model regresi panel
summary(pooling_ecik)
## Pooling Model
## 
## Call:
## plm(formula = Profit ~ Ekonomi + Coal + Inflasi + Kurs, data = df_p, 
##     model = "pooling")
## 
## Unbalanced Panel: n = 8, T = 4-16, N = 76
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -433.745  -77.401  -17.035   88.993  405.676 
## 
## Coefficients:
##                Estimate  Std. Error t-value Pr(>|t|)    
## (Intercept) -88.4625513 188.1835804 -0.4701 0.639734    
## Ekonomi       1.1038282  10.1311916  0.1090 0.913547    
## Coal          3.1767717   0.8660966  3.6679 0.000469 ***
## Inflasi       5.1658485  11.1143632  0.4648 0.643504    
## Kurs         -0.0041918   0.0133722 -0.3135 0.754842    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    2147800
## Residual Sum of Squares: 1631900
## R-Squared:      0.24018
## Adj. R-Squared: 0.19737
## F-statistic: 5.61069 on 4 and 71 DF, p-value: 0.00055491
summary(model_fe_ecik)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = Profit ~ Ekonomi + Coal + Inflasi + Kurs, data = df_p, 
##     model = "within")
## 
## Unbalanced Panel: n = 8, T = 4-16, N = 76
## 
## Residuals:
##      Min.   1st Qu.    Median   3rd Qu.      Max. 
## -427.5695  -63.6005   -5.7678   67.6231  340.6014 
## 
## Coefficients:
##           Estimate Std. Error t-value  Pr(>|t|)    
## Ekonomi -1.4272950 10.0751238 -0.1417 0.8877894    
## Coal     3.5747440  0.8860670  4.0344 0.0001485 ***
## Inflasi  5.6961722 10.9628114  0.5196 0.6051415    
## Kurs    -0.0066245  0.0134170 -0.4937 0.6231798    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1964200
## Residual Sum of Squares: 1408900
## R-Squared:      0.28269
## Adj. R-Squared: 0.1594
## F-statistic: 6.3056 on 4 and 64 DF, p-value: 0.00024243
summary(model_re_ecik)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = Profit ~ Ekonomi + Coal + Inflasi + Kurs, data = df_p, 
##     model = "random")
## 
## Unbalanced Panel: n = 8, T = 4-16, N = 76
## 
## Effects:
##                   var std.dev share
## idiosyncratic 22014.4   148.4     1
## individual        0.0     0.0     0
## theta:
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       0       0       0       0       0       0 
## 
## Residuals:
##     Min.  1st Qu.   Median  3rd Qu.     Max. 
## -433.745  -77.401  -17.035   88.993  405.676 
## 
## Coefficients:
##                Estimate  Std. Error z-value  Pr(>|z|)    
## (Intercept) -88.4625513 188.1835804 -0.4701 0.6382933    
## Ekonomi       1.1038282  10.1311916  0.1090 0.9132394    
## Coal          3.1767717   0.8660966  3.6679 0.0002445 ***
## Inflasi       5.1658485  11.1143632  0.4648 0.6420816    
## Kurs         -0.0041918   0.0133722 -0.3135 0.7539228    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    2147800
## Residual Sum of Squares: 1631900
## R-Squared:      0.24018
## Adj. R-Squared: 0.19737
## Chisq: 22.4428 on 4 DF, p-value: 0.00016358
#uji model
phtest(model_fe_ecik, model_re_ecik)
## 
##  Hausman Test
## 
## data:  Profit ~ Ekonomi + Coal + Inflasi + Kurs
## chisq = 12.12, df = 4, p-value = 0.01648
## alternative hypothesis: one model is inconsistent
pFtest(model_fe_ecik, pooling_ecik)
## 
##  F test for individual effects
## 
## data:  Profit ~ Ekonomi + Coal + Inflasi + Kurs
## F = 1.4471, df1 = 7, df2 = 64, p-value = 0.2024
## alternative hypothesis: significant effects
plmtest(pooling_ecik, type = "bp")
## 
##  Lagrange Multiplier Test - (Breusch-Pagan)
## 
## data:  Profit ~ Ekonomi + Coal + Inflasi + Kurs
## chisq = 0.099892, df = 1, p-value = 0.752
## alternative hypothesis: significant effects