franzmohr / prais Goto Github PK
View Code? Open in Web Editor NEWPrais-Winsten estimator for AR(1) serial correlation
Prais-Winsten estimator for AR(1) serial correlation
Hello Franz,
you've created a great package! Thank you!
Now, I'm using the prais-winsten function. Is it possible to do a prediction with the results, similar to the predict-function and lm-Modell?
Her's my code. How do I solve this problem?
Thanks, Franziska
Code:
library(prais)
library(sandwich)
y = c(2413.10, 2421.26, 2434.95, 2429.89, 2431.36, 2421.47, 2431.23, 2420.21 ,2402.80, 2330.75, 2269.42, 2258.62,
2177.25, 2141.12)
x1= c(177.0424, 174.2006, 178.3835, 168.5238, 168.4668, 170.4619, 176.6676, 165.3332, 160.8694, 166.1567, 168.7098,
171.1672, 168.3668,182.1042)
x2 = c( 2839, 3249, 3929, 3725, 3204, 3061, 2485, 2073, 15202, 4074, 1996, 1887, 420, 1022)
Daten = data.frame(y, x1, x2)
Modell = lm(y ~ x1+x2)
summary(Modell)
dwtest(lm(Modell), alternative = c("greater","two.sided", "less"),iterations=15, tol=1e-10, data =list())
Modell_pw = prais_winsten(lm(Modell), data=Daten)
summary(Modell_pw)
NewData = data.frame(y= 2500, x1= 190, x2= 1500)
#Prognose des "einfachen" Modells
predict(Modell, newdata = NewData)
#Prognose des transformierten Modells
predict(Modell_pw, newdata = NewData)
The following call yields the error "subscript out of bounds"
prais_winsten(Sales ~ January + Variable_L1, data = dataset, index = dataset$Date)
Iteration 0: rho = 0
Error in `[<-`(`*tmp*`, groups[[i]], 2, value = c((1 - rho[j]^2)^(1/2), :
subscript out of bounds
If I run "prais_winsten(Sales ~ January + Variable, data = dataset, index = dataset$Date)" it works fine. Any lagged variable, i.e., for a 1 period lag the first observation = "NA" and the second is equal to observation 1, etc. causes this error.
I will disclose that this could be due to improper use of the "index." Index was not required in v1.1.1, so I've never had to use it before. Regardless, reverting to v1.1.1 solves the issue.
Dear Franz,
thank you for the great package. I am doing a fixed effects estimation and intend to use the Prais_Winsten estimator to account for the serial correlation of my model. I have set up both a Least Squared Dummy Variable Model of type lm and a plm model. Both have the exact same estimates for my independent variables (see code below):
LSDV_model <- lm(EPS_relative_lead ~ share_femalemin + avg_agemin + GDPlog + green_parl + Schmidt_Index + factor (Country) + factor (Year), data=data_OECD.p_noNA)
FE_model <- plm(EPS_relative_lead ~ share_femalemin + avg_agemin + GDPlog + green_parl + Schmidt_Index, data=data_OECD.p_noNA,effect = "twoways", model = "within")_
Yet, when using prais_winsten, I receive completely different results depending on whether I use my LSDV_model or my FE_model. I was therefore wondering, whether the usage of prais_winsten basen on a plm model was even possible or whether this is an error?
Thanks for your help!
First of all, thanks for this package. I'm looking at the following data and getting the following result.
dep = c(59512,69565,78395,86085,68288,76692,86647,80146,76118,79969,82338,79269,68427,80309,87515,81462,62390,67105)
ind = c(0.12820512821,0.20512820513,0.16666666667,0.11904761905,0.0625,0.08333333333,0.11494252874,0.17857142857,0.16049382716,0.13636363636,0.16842105263,0.136842105263158,0.161616161616162,0.105882352941176,0.150442477876106,0.176470588235294,0.101123595505618,0.138888888888889)
summary(prais_winsten(dep ~ ind, data = data.frame(dep, ind)))
Iteration 0: rho = 0
Iteration 1: rho = 0.2433
Iteration 2: rho = 0.2662
Iteration 3: rho = 0.2688
Iteration 4: rho = 0.2691
Iteration 5: rho = 0.2691
Iteration 6: rho = 0.2691
Iteration 7: rho = 0.2691
Call:
prais_winsten(formula = dep ~ ind, data = data.frame(dep, ind))
Residuals:
Min 1Q Median 3Q Max
-15618 -7152 3157 5068 12154
AR(1) coefficient rho after 7 Iterations: 0.2691
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 68970 8124 8.490 2.54e-07 ***
ind 48051 55657 0.863 0.401
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 8158 on 16 degrees of freedom
Durbin-Watson statistic (original): 1.248
Durbin-Watson statistic (transformed): 1.556
I'm not sure why it's not outputting an r2. If I save the regression as a variable and access the r.squared value it returns 'NULL.' The Durbin Watson test returns the following DW = 1.248, p-value = 0.04158. Does the independent variable need to be significant to return a r2? Thanks!
Dear Franz,
thanks for your help. I have my dataframe attached to this e-mail, so feel free to test it. My DV is “EPS_relative_lead” and my IVs are “share_femalemin” and “avg_agemin”. Controls are GDPlog, green_parl and Schmidt_index. I have also included factors for Country and Year.
The code I used for the Prais-Winsten estimation is:
prais_winsten (EPS_relative_lead ~ share_femalemin + avg_agemin + Schmidt_Index + green_parl + GDPlog + factor (Country) + factor (Year), data = data_OECD_noNA, index = c("Country","Year"), panelwise = TRUE, rhoweight = "T1")
I was furthermore wondering whether the prais_winsten command would also work using a model of class ‘plm’.
Happy to receive your comments and thanks a lot for your help.
Best, Julian
Von: Franz Mohr @.>
Gesendet: Samstag, 25. Februar 2023 10:54
An: franzmohr/prais @.>
Cc: julianjaecker @.>; Author @.>
Betreff: Re: [franzmohr/prais] Usage with plm function possible? (Issue #8)
Hi Julian! Thanks for using the package! And yes, since version 1.1 it supports panel regression. An explanation on how to use the package with panel data can be found in the README file here on GitHub. But I probably should add a vignette to the package. Regarding the results, I did quite intensive testing of the package by making sure that the results are equal to the output from Stata. In your case I could offer to the same if you feel comfortable sharing your data. Best, Franz
—
Reply to this email directly, view it on GitHub #8 (comment) , or unsubscribe https://github.com/notifications/unsubscribe-auth/A5ZAZ27CJEM2RBBQL3ZWDI3WZHJEHANCNFSM6AAAAAAVGVTXWY .
You are receiving this because you authored the thread. https://github.com/notifications/beacon/A5ZAZ2ZQCUTCIQV2MLMTH23WZHJEHA5CNFSM6AAAAAAVGVTXW2WGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTSWEGPWA.gif Message ID: @.*** @.***> >
Originally posted by @julianjaecker in #8 (comment)
Dear Franz,
It is a good package, but would it be posible to add SE for the predictions in this type of model?
All the best,
Angel
Originally posted by @angelpaternina in #6 (comment)
Thank you! That solves the problem of including factors. But how to get in this version a "lm-equivalent" output (i.e. suitable for presenting results with stargazer or calculating robust standard errors with lmtest::coeftest)? In the previous version changing the return function from "results" to "lm" did the job (as explained here), but that's not the case anymore. For example:
devtools::install_github("franzmohr/prais")
library(prais)
df<- data.frame(x = runif(50),
y = runif(50),
factor = factor(rep(c(1:10), 5)))
While lm works
lmfit<- lm(y ~ x + factor, data = df)
coeftest(lmfit)
coeftest(lmfit, vcov = vcovHC)
the new prais_winsten returns the following errors:
pwfit<- prais_winsten(y ~ x + factor, data = df)
coeftest(pwfit)
Error in UseMethod("vcov") :
no applicable method for 'vcov' applied to an object of class "prais"
coeftest(pwfit, vcov = vcovHC)
Error in eval(predvars, data, env) : object 'y' not found
Originally posted by @jlprol in #1 (comment)
Thank you very much for using the package. I have a request: would it be possible to include the p-value of the D-W test in the summary output? I would greatly appreciate it.
tian liu
Hello Franz,
thank you for your great package! I would like to make this suggestion to you: My recently developed C/C++ package collapse provides a function fdiff
which supports extremely fast quasi-differences on (unordered) time series and panel data. The documentation for that function is here. I'd love to see this performance implemented in Prais. collapse depends on Rcpp, so you'd have two extra dependencies (more or less one as 99% of R users have Rcpp installed).
If you don't want to create hard dependencies, you could think of adding a soft one by using if(requireNamespace("collapse", quietly = TRUE))
to check if collapse
is available, and use your base R implementation otherwise.
If you go for a hard dependency, collapse
also offers other functions like na_omit
, qDF
, qM
, roworderv
, funique
, ss
, get_vars
etc. that would also help to dramatically speed up the rest of the prais code.
I don't want to intrude into your development choices of course, but It would be really cool to run prais on large panel data. Let me know what you think about this.
Best regards,
Sebastian
Thanks for this great package.
Is there any way to return an object of class "lm" rather than "prais" from prais_winsten()
to work with other packages, such as {stargazer} and {broom}?
In the previous version of the function prais.winsten()
, the return()
function could be changed to "lm" (as explained here: https://stackoverflow.com/questions/49109282/how-to-work-with-prais-winsten-results-in-stargazer-and-broom-r), but that doesn't work anymore.
Thank you.
Is it possible to include categorical (factor) variables in the prais.winsten() model.
A minimal reproducible example: When running this
library(prais)
df<- data.frame(x = runif(50),
y = runif(50),
factor = factor(rep(c(1:10), 5)))
prais.winsten(y ~ x + factor, data = df)
I get the error
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases
In addition: Warning messages:
1: In Ops.factor((1 - rho^2)^(1/2), mod[1, (k + 1)]) :
‘*’ not meaningful for factors
2: In Ops.factor(rho, mod[1:(n - 1), (k + 1)]) :
‘*’ not meaningful for factors
3: In Ops.factor(mod[2:n, (k + 1)], rho * mod[1:(n - 1), (k + 1)]) :
‘-’ not meaningful for factors
Check, how data gaps in the time index within a group could be dealt with.
Thank you for this useful tool. Would you know of a convenient method to transform the output of prais_winsten into input for gtsummary or similar tools?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.