I keep getting error on this, can someone help me - r

delta_gamma = 0.05
MRW.data <- MRW.data %>%
mutate(ln.gdp.85 = log(gdp.85),
ln.gdp.60 = log(gdp.60),
ln.gdp.growth = ln.gdp.85 - ln.gdp.60,
ln.inv.gdp = log(Inv.gdp/100),
Non.oil = factor(Non.oil),
intermediate = factor(intermediate),
OECD= factor(OECD),
ln.ndg = log(pop.growth /100 + delta_gamma)) %>%
select(country, ln.gdp.85, ln.gdp.60, ln.inv.gdp, Non.oil, intermediate, OECD, ln.ndg, ln.school, gdp.growth, ln.gdp.growth)
skim(MRW.data)
Show in New Window
Error in select():
! Must subset columns with a valid subscript vector.
x Can't convert from to due to loss of precision.
Backtrace:
... %>% ...
rlang::cnd_signal(x)
Error in select(., country, ln.gdp.85, ln.gdp.60, ln.inv.gdp, Non.oil, :
x Can't convert from to due to loss of precision.

Related

Tidysynth error -- Please specify only one treated unit

I am trying to calculate a Synthetic control using the tidysynth package. I'm fairly new to the package and the data but here is my code:
#Import data
synth <- read.csv("https://raw.githubusercontent.com/FDobkin/coal_paper/main/synth_data.csv")
#Convert year to date
synth$year <- strptime(synth$year, format = "%Y")
synth_out <- synth %>%
synthetic_control(
outcome=saleprice,
unit=fips,
time=year,
i_unit=47145,
i_time=2009-10-19,
generate_placebos=T
) %>%
generate_predictor(
time_window=2000-10-19:2009-10-19,
population = pop,
white = white_p,
age = age65p_p,
rucc = rucc_code,
income = median_income,
unemploy = unemprate,
laborforce = lfrate
) %>%
generate_weights(optimization_window = 2000-10-19:2009-10-19, # time to use in the optimization task
margin_ipop = .02,sigf_ipop = 7,bound_ipop = 6 # optimizer options
)
%>%
generate_control()
The error is:
Error in synth_method(treatment_unit_covariates = treatment_unit_covariates, :
Please specify only one treated unit.
The error seems to becoming from the generate_weights() statement. I am specifying the specific county that is receiving the treatment in the synthetic_control() statement. What is the error noting is wrong?

R package {gtsummary} - Can't convert .f (NULL) to function using custom stat function

I'm using gtsummary::tbl_custom_summary to present data analyzed through a test function I've written.
I'm getting this error:
Error in `mutate()`:
! Problem while computing `df_stats = pmap(...)`.
Caused by error in `as_group_map_function()`:
! Can't convert `.f`, NULL, to a function.
Backtrace:
1. gtsummary::tbl_custom_summary(...)
18. dplyr:::group_modify.grouped_df(...)
19. dplyr:::as_group_map_function(.f)
20. rlang::as_function(.f)
I haven't been able to tell if it has to do with data masking/quotations (which I tried to implement but, honestly, haven't understood much of…), or if it something related to gtsummary.
This is the code—please note the custom function's argument are modeled after tbl_custom_summary's requirements and have convenience defaults at the moment.
library(tidyverse)
library(infer)
library(gtsummary)
# Custom function supposedly uses infer package functions to determine the difference
# in medians and its bootstrapped CIs, returning a DF with values specified and
# 'glued' in the tbl_custom_summary call (statistic argument).
testdiffCI <- function(group_data,
full_data,
variable,
by,
type,
stat_display,
...,
point_stat = "median",
# This default is in order to use mtcars data
order = c("0", "1"),
ci_type = "bias-corrected"
) {
# change the function arg into one compatible with infer syntax, i.e. string
stat <- str_glue("diff in {point_stat}s") |> toString()
# These variables are passed as strings (names). Convert them to symbols.
variable <- sym(variable)
by <- sym(by)
# Calculate point estimate:
point <- full_data |>
# Trying to use shorthand defuse|>inject operator {{
# I'm really not sure if this is correct
specify(response={{variable}}, explanatory = {{by}}) |>
calculate(stat = stat, order = order) |> suppressWarnings()
# Bootstrap (population) object
boot <- full_data |>
specify(response = {{variable}}, explanatory = {{by}}) |>
generate(reps = 1000, type = "bootstrap") |>
calculate(stat = stat, order = order)
ci <- get_confidence_interval(boot,
type = ci_type,
point_estimate = point
)
# Return a tibble; column names are the same as those appearing in the statistic
# argument in tbl_custom_summary call
tibble(
diff = point$stat,
ci_lo = ci$lower_ci,
ci_up = ci$upper_ci
)
}
This is the call to the summary function:
mtcars |>
mutate(vs = as.factor(vs)) |>
tbl_custom_summary(
by = vs,
stat_fns = list(all_continuous() ~ testdiffCI),
statistic = ~ "{diff} ({ci_lo} – {ci_hi})"
)
Thanks for any insights you guys may share!

Error: must rename columns with a valid subscript vector

I'm just trying to import a kaggle data set to study R on and it's being a nightmare.
I'm trying to rename the columns in my data frame but I keep getting errors.
library(tidyverse)
library(dplyr)
library(ggplot2)
library(tibble)
library(janitor)
food_advs<- read.csv("CAERS_ASCII_2004_2017Q2.csv")
food_df <- data.frame(food_advs)
food_df %>% rename(food_df, Product = PRI_Reported.Brand.Product.Name, Industry = PRI_FDA.Industry.Name, Person_age = CI_Age.at.Adverse.Event, Gender = CI_Gender, Outcomes = AEC_One.Row.Outcomes, Symptoms = SYM_One.Row.Coded.Symptoms)
> food_df %>% rename(food_df, "Product" = "PRI_Reported.Brand.Product.Name", "Industry" = "PRI_FDA.Industry.Name", "Person_age" = "CI_Age.at.Adverse.Event", "Gender" = "CI_Gender", "Outcomes" = "AEC_One.Row.Outcomes", "Symptoms" = "SYM_One.Row.Coded.Symptoms")
Error: Must rename columns with a valid subscript vector.
x Subscript has the wrong type `data.frame<
RA_Report.. : integer
RA_CAERS.Created.Date : character
AEC_Event.Start.Date : character
PRI_Product.Role : character
PRI_Reported.Brand.Product.Name: character
PRI_FDA.Industry.Code : integer
PRI_FDA.Industry.Name : character
CI_Age.at.Adverse.Event : integer
CI_Age.Unit : character
CI_Gender : character
AEC_One.Row.Outcomes : character
SYM_One.Row.Coded.Symptoms : character
>`.
i It must be numeric or character.
Run `rlang::last_error()` to see where the error occurred.
Try the following,
food_df %>%
rename(Product = PRI_Reported.Brand.Product.Name,
Industry = PRI_FDA.Industry.Name,
Person_age = CI_Age.at.Adverse.Event,
Gender = CI_Gender,
Outcomes = AEC_One.Row.Outcomes,
Symptoms = SYM_One.Row.Coded.Symptoms
)
Your mistake is in your usage of %>%; It is abundant to use rename(data, ...) when you already have data %>% before your call.

Error replacing a column with other values data frames R

I'm trying to replace the values which I've set by default in a data frame by the calculated ones but I get an error that I don't understand as far as I've no factors.
Here is the code :
nb_agences_iris <- agences %>%
group_by(CODE_IRIS) %>%
summarise(nb_agences = n()) %>%
arrange(CODE_IRIS)
int <- data.frame("CODE_IRIS" = as.character(intersect(typo$X0, nb_agences_iris$CODE_IRIS)))
typo$nb_agences <- as.character(rep(0, nrow(typo)))
typo[int$CODE_IRIS,]$nb_agences <- as.character(nb_agences_iris[int$CODE_IRIS,]$nb_agences)
And I get the following error:
Error in Summary.factor(1:734, na.rm = FALSE) :
‘max’ not meaningful for factors
In addition: Warning message:
In Ops.factor(i, 0L) : ‘>=’ not meaningful for factors
Thanks in advance for your help.

Mutate_impl error in running R code

Trying to run following R code.
> sp_500 <- sp_500 %>%
+
+ mutate(
+ stock.prices = map(ticker.symbol,
+ function(.x) get_stock_prices(.x,
+ return_format = "tibble",
+ from = "2017-01-01",
+ to = "2017-09-21")
+ ),
+ log.returns = map(stock.prices,
+ function(.x) get_log_returns(.x, return_format = "tibble")),
+ mean.log.returns = map_dbl(log.returns, ~ mean(.$Log.Returns)),
+ sd.log.returns = map_dbl(log.returns, ~ sd(.$Log.Returns)),
+ n.trade.days = map_dbl(stock.prices, nrow)
But I keep getting this error:
Warning: BRK.B download failed; trying again.
Error in mutate_impl(.data, dots) :
Evaluation error: BRK.B download failed after two attempts. Error message:
HTTP error 404..
Does anyone have an idea what am I doing wrong?
Best Regards
AnSa
It seems to have a problem downloading specific tickers. I am not a developer but I had the same problem as you do and fixed it by removing these tickers (less than 10). The code for removing them can be found on the same page where this piece of code is taken from and its
sp_500 <- sp_500 %>%
filter(ticker.symbol != "BRK.B")
I hope it helped.
Basically there is something wrong with the BRK.B stock, I'm not sure what it is, but the way to solve it is by eliminating it/them.
There are other stocks that get stuck in the function, this is how I solve it:
sp_500 <- sp_500[c(-72,-86, -82, -163, -268, -460, -392),] %>%
mutate(
stock.prices = map(ticker.symbol,
function(.x) get_stock_prices(.x,
return_format = "tibble",
from = "2007-01-01",
to = "2018-10-23")
),
log.returns = map(stock.prices,
function(.x) get_log_returns(.x, return_format = "tibble")),
mean.log.returns = map_dbl(log.returns, ~ mean(.$Log.Returns)),
sd.log.returns = map_dbl(log.returns, ~ sd(.$Log.Returns)),
n.trade.days = map_dbl(stock.prices, nrow)
)
The [c(-72,-86, -82, -163, -268, -460, -392),] are the stocks that didn't work for me because it display and error, basically find the columns in which you get errors by looking at the stock name and see in which columns they are and eliminate them
Hope it helps

Resources