stata

What is the equivalent of Stata function inlist() in R?

有些话、适合烂在心里 提交于 2021-02-18 10:53:12
问题 Stata's inlist allows us to refer to the real or string values of a variable. I was wondering whether R has such a function. Examples: I want to choose eight states from the variable state (you can think this as column state in any dataframe where state takes 50 string values (states of United States)). inlist(state,"NC","AZ","TX","NY","MA","CA","NJ") I want to choose nine values of age from the variable age (you can think this as column age in any dataframe where age takes numerical values

R equivalent of Stata's for-loop over macros

二次信任 提交于 2021-02-11 07:02:00
问题 I have a variable x that is between 0 and 1, or (0,1]. I want to generate 10 dummy variables for 10 deciles of variable x . For example x_0_10 takes value 1 if x is between 0 and 0.1, x_10_20 takes value 1 if x is between 0.1 and 0.2, ... The Stata code to do above is something like this: forval p=0(10)90 { local Next=`p'+10 gen x_`p'_`Next'=0 replace x_`p'_`Next'=1 if x<=`Next'/100 & x>`p'/100 } Now, I am new at R and I wonder how I can do above in R? 回答1: cut is your friend here; its output

R-squared within for a regression with multiple fixed effects [closed]

筅森魡賤 提交于 2021-02-11 06:30:32
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 10 months ago . Improve this question I would like to get the R-squared within for a fixed effect regression with multiple fixed effects (let's say Country, Year, Trimester). The least squared dummy variable (LSDV) model ( lm in R/ reg in Stata) would only provide the overall R-squared. The same is

Error reading Stata data in R

心已入冬 提交于 2021-02-07 10:15:40
问题 I am trying to read a Stata dataset in R with the foreign package, but when I try to read the file using: library(foreign) data <- read.dta("data.dta") I got the following error: Error in read.dta("data.dta") : a binary read error occurred The file works fine in Stata. This site suggests saving the file in Stata without labels and then reading it into R. With this workaround I am able to load the file into R, but then I lose the labels. Why am I getting this error and how can I read the file

R - Testing equivalence of coefficients in multivariate multiple regression

核能气质少年 提交于 2021-02-07 09:01:56
问题 I'm trying to conduct a multivariate multiple regression analysis. Fortunately, I've found an excellent page demonstrating how to do so in Stata: http://www.ats.ucla.edu/stat/stata/dae/mvreg.htm The issue is that I'm using R, while I've figured out how to do the basics of running a multivariate multiple regression model in R, I'm still not sure how to see if coefficients are different for each dependent variable (as shown in the link). Does anyone know how to compute this analysis in R? Seems

How to get the same values for AIC and BIC in R as in Stata?

别说谁变了你拦得住时间么 提交于 2021-02-05 07:34:24
问题 Say I have a very simple model library(foreign) smoke <- read.dta("http://fmwww.bc.edu/ec-p/data/wooldridge/smoke.dta") smoking.reg <- lm(cigs ~ educ, data=smoke) AIC(smoking.reg) BIC(smoking.reg) In R I get the following results: > AIC(smoking.reg) [1] 6520.26 > BIC(smoking.reg) [1] 6534.34 Running the same regression however in Stata use http://fmwww.bc.edu/ec-p/data/wooldridge/smoke.dta reg cigs educ returns the following result estat ic How can I get R to return exactly the same values as

Convert Stata 16 files to Stata 12 files using R

两盒软妹~` 提交于 2021-01-28 09:51:00
问题 I am using RStudio (running R 4.0.1) and Stata 12 for Windows and have got a large number of folders with Stata 16 .dta files (and other types of files not relevant to this question). I want to create an automated process of converting all Stata 16 .dta files into Stata 12 format (keeping all labels) to then analyze. Ideally, I want to keep the names of the original folders and files but save the converted versions into a new location. This is what I have got so far: setwd("C:/FilesLocation")

Getting unknown function mean() in a forvalues loop

我们两清 提交于 2021-01-28 05:30:55
问题 Getting unknown function mean for this. Can't use egen because it has to be calculated for each value. A little confused. edu_mov_avg=. forvalues current_year = 2/133 { local current_mean = mean(higra) if longitbirthqtr >= current_year - 2 & longitbirthqtr >= current_year + 2 replace edu_mov_avg = current_mean if longitbirthqtr = } 回答1: Your code is a long way from working. This should be closer. gen edu_mov_avg = . qui forvalues current_qtr = 2/133 { su higra if inrange(longitbirthqtr,

Reshape in R with variable name patterns

倖福魔咒の 提交于 2021-01-01 13:08:45
问题 I am trying to reproduce the results of a reshape in Stata using base R's reshape function. Stata webuse reshape3, clear li, clean // reshape long reshape long inc@r ue, i(id) j(year) list, sepby(id) clean This produces, before the reshape : . li, clean id sex inc80r inc81r inc82r ue80 ue81 ue82 1. 1 0 5000 5500 6000 0 1 0 2. 2 1 2000 2200 3300 1 0 0 3. 3 0 3000 2000 1000 0 0 1 Note the pattern of the names for the stub inc . After the reshape , I get: . list, sepby(id) clean id year sex incr

Reshape in R with variable name patterns

久未见 提交于 2021-01-01 12:50:53
问题 I am trying to reproduce the results of a reshape in Stata using base R's reshape function. Stata webuse reshape3, clear li, clean // reshape long reshape long inc@r ue, i(id) j(year) list, sepby(id) clean This produces, before the reshape : . li, clean id sex inc80r inc81r inc82r ue80 ue81 ue82 1. 1 0 5000 5500 6000 0 1 0 2. 2 1 2000 2200 3300 1 0 0 3. 3 0 3000 2000 1000 0 0 1 Note the pattern of the names for the stub inc . After the reshape , I get: . list, sepby(id) clean id year sex incr