I am trying to add columns to my df for woba, an advanced baseball stat, but when I try to mutate the column it gives an unexpected input.
This is my code
library(baseballr)
output <- bref_daily_batter("2015-05-10", "2015-05-30")
output <- mutate(output, wOBA = (0.687*uBB + 0.718*HBP + 0.881*X1B + 1.256*X2B + 1.594*X3B + 2.065*HR) / (AB + BB β IBB + SF + HBP))
Error: unexpected input in "output<- mutate(output, wOBA = (0.687uBB + 0.718HBP + 0.881X1B + 1.256X2B + 1.594X3B + 2.065HR) / (AB + BB β"
The problem is in whatever negative symbol you are using.
This does not work:
output <- mutate(output, wOBA = (0.687*uBB + 0.718*HBP + 0.881*X1B + 1.256*X2B + 1.594*X3B + 2.065*HR) / (AB + BB β IBB + SF + HBP))
but this does:
output <- mutate(output, wOBA = (0.687*uBB + 0.718*HBP + 0.881*X1B + 1.256*X2B + 1.594*X3B + 2.065*HR) / (AB + BB - IBB + SF + HBP))
I just replaced the minus sign with a new minus sign, which looks slightly smaller on my screen
Related
I am trying to add equations to my code which are quite long. When I do so, R does not quite grasp where the end of the equation is and when I go to the next line filled with whitespace, it puts the cursor to the middle of the line rather than the beginning, as usually.
When I run the code, it ignores the end of the long equation, as one can tell from the console output
+ # 3.b) Initialize ODEs
+ initialstate <- c(
Error: unexpected symbol in:
"# 3.b) Initialize ODEs
initialstate"
-- so using + rather than > -- following this input:
C_PL_s_ss = (Rin_s*(CL_dis_s*T1*T2*V_IS + CL_dis_s*T1*T2*V_PE + CL_dis_s*T1*T2*V_PL + CL_dis_s*T1*T3_s*V_IS + CL_dis_s*T1*T4_s*V_IS + CL_dis_s*T2*T3_s*V_IS + CL_dis_s*T2*T4_s*V_IS + CL_dis_s*T2*T5_s*V_IS + CL_dis_s*T2*T6_s*V_IS + CL_dis_s*T1*T3_s*V_PE + CL_dis_s*T1*T4_s*V_PE + CL_dis_s*T2*T3_s*V_PE + CL_dis_s*T2*T4_s*V_PE + CL_dis_s*T2*T5_s*V_PE + CL_dis_s*T2*T6_s*V_PE + CL_dis_s*T1*T3_s*V_PL + CL_dis_s*T1*T4_s*V_PL + CL_dis_s*T2*T3_s*V_PL + CL_dis_s*T2*T4_s*V_PL + CL_dis_s*T2*T5_s*V_PL + CL_dis_s*T2*T6_s*V_PL + CL_dis_s*T1*T2*V_IS*delta_Rin_s_TI + CL_dis_s*T1*T3_s*V_IS*delta_Rin_s_TI + CL_dis_s*T1*T4_s*V_IS*delta_Rin_s_TI + CL_dis_s*T2*T3_s*V_IS*delta_Rin_s_TI + CL_dis_s*T2*T4_s*V_IS*delta_Rin_s_TI + CL_dis_s*T2*T5_s*V_IS*delta_Rin_s_TI + CL_dis_s*T2*T6_s*V_IS*delta_Rin_s_TI + CL_dis_s*CLup_s^2*V_IS*V_PE*V_VC + CL_dis_s*CLup_s^2*V_IS*V_PL*V_VC + CLup_s^2*T1*V_IS*V_PE*V_PL + CLup_s^2*T2*V_IS*V_PE*V_VC + CLup_s^2*T2*V_PE*V_PL*V_VC + CLup_s^2*T3_s*V_IS*V_PE*V_PL + CLup_s^2*T4_s*V_IS*V_PE*V_PL + CLup_s^2*T5_s*V_IS*V_PE*V_PL + CLup_s^2*T6_s*V_IS*V_PE*V_PL + CLup_s^2*T3_s*V_PE*V_PL*V_VC + CLup_s^2*T4_s*V_PE*V_PL*V_VC + CLup_s^3*V_IS*V_PE*V_PL*V_VC + CL_dis_s*CLup_s*T1*V_IS*V_PE + CL_dis_s*CLup_s*T1*V_IS*V_PL + CL_dis_s*CLup_s*T2*V_IS*V_VC + CL_dis_s*CLup_s*T2*V_PE*V_VC + CL_dis_s*CLup_s*T2*V_PL*V_VC + CL_dis_s*CLup_s*T3_s*V_IS*V_PE + CL_dis_s*CLup_s*T4_s*V_IS*V_PE + CL_dis_s*CLup_s*T5_s*V_IS*V_PE + CL_dis_s*CLup_s*T6_s*V_IS*V_PE + CL_dis_s*CLup_s*T3_s*V_IS*V_PL + CL_dis_s*CLup_s*T4_s*V_IS*V_PL + CL_dis_s*CLup_s*T5_s*V_IS*V_PL + CL_dis_s*CLup_s*T6_s*V_IS*V_PL + CL_dis_s*CLup_s*T3_s*V_PE*V_VC + CL_dis_s*CLup_s*T4_s*V_PE*V_VC + CL_dis_s*CLup_s*T3_s*V_PL*V_VC + CL_dis_s*CLup_s*T4_s*V_PL*V_VC + CLup_s*T1*T2*V_IS*V_PE + CLup_s*T1*T2*V_PE*V_PL + CLup_s*T1*T3_s*V_IS*V_PE + CLup_s*T1*T4_s*V_IS*V_PE + CLup_s*T2*T3_s*V_IS*V_PE + CLup_s*T2*T4_s*V_IS*V_PE + CLup_s*T2*T5_s*V_IS*V_PE + CLup_s*T2*T6_s*V_IS*V_PE + CLup_s*T1*T3_s*V_PE*V_PL + CLup_s*T1*T4_s*V_PE*V_PL + CLup_s*T2*T3_s*V_PE*V_PL + CLup_s*T2*T4_s*V_PE*V_PL + CLup_s*T2*T5_s*V_PE*V_PL + CLup_s*T2*T6_s*V_PE*V_PL + CL_dis_s*CLup_s*T2*V_IS*V_VC*delta_Rin_s_TI + CLup_s*T1*T2*V_IS*V_PE*delta_Rin_s_TI + CLup_s*T1*T3_s*V_IS*V_PE*delta_Rin_s_TI + CLup_s*T1*T4_s*V_IS*V_PE*delta_Rin_s_TI + CLup_s*T2*T3_s*V_IS*V_PE*delta_Rin_s_TI + CLup_s*T2*T4_s*V_IS*V_PE*delta_Rin_s_TI + CLup_s*T2*T5_s*V_IS*V_PE*delta_Rin_s_TI + CLup_s*T2*T6_s*V_IS*V_PE*delta_Rin_s_TI + CLup_s^2*T2*V_IS*V_PE*V_VC*delta_Rin_s_TI))/(CLR_s*CL_dis_s*T1*T2 + CLR_s*CL_dis_s*T1*T3_s + CLR_s*CL_dis_s*T1*T4_s + CLR_s*CL_dis_s*T2*T3_s + CLR_s*CL_dis_s*T2*T4_s + CLR_s*CL_dis_s*T2*T5_s + CLR_s*CL_dis_s*T2*T6_s + CLR_s*CL_dis_s*CLup_s^2*V_IS*V_VC + CL_dis_s*CLup_s^2*Q*V_IS*V_VC + CLR_s*CLup_s^2*T1*V_IS*V_PE + CLR_s*CLup_s^2*T2*V_PE*V_VC + CL_dis_s*CLup_s^2*T1*V_IS*V_PE + CL_dis_s*CLup_s^2*T1*V_IS*V_PL + CL_dis_s*CLup_s^2*T2*V_PE*V_VC + CL_dis_s*CLup_s^2*T2*V_PL*V_VC + CLR_s*CLup_s^2*T3_s*V_IS*V_PE + CLR_s*CLup_s^2*T4_s*V_IS*V_PE + CLR_s*CLup_s^2*T5_s*V_IS*V_PE + CLR_s*CLup_s^2*T6_s*V_IS*V_PE + CLR_s*CLup_s^2*T3_s*V_PE*V_VC + CLR_s*CLup_s^2*T4_s*V_PE*V_VC + CL_dis_s*CLup_s^2*T3_s*V_IS*V_PE + CL_dis_s*CLup_s^2*T4_s*V_IS*V_PE + CL_dis_s*CLup_s^2*T5_s*V_IS*V_PE + CL_dis_s*CLup_s^2*T6_s*V_IS*V_PE + CL_dis_s*CLup_s^2*T3_s*V_IS*V_PL + CL_dis_s*CLup_s^2*T4_s*V_IS*V_PL + CL_dis_s*CLup_s^2*T5_s*V_IS*V_PL + CL_dis_s*CLup_s^2*T6_s*V_IS*V_PL + CL_dis_s*CLup_s^2*T3_s*V_PE*V_VC + CL_dis_s*CLup_s^2*T4_s*V_PE*V_VC + CL_dis_s*CLup_s^2*T3_s*V_PL*V_VC + CL_dis_s*CLup_s^2*T4_s*V_PL*V_VC + CLR_s*CLup_s^3*V_IS*V_PE*V_VC + CL_dis_s*CLup_s^3*V_IS*V_PE*V_VC + CL_dis_s*CLup_s^3*V_IS*V_PL*V_VC + CLup_s^2*Q*T2*V_PE*V_VC + CLup_s^2*Q*T3_s*V_IS*V_PE + CLup_s^2*Q*T4_s*V_IS*V_PE + CLup_s^2*Q*T5_s*V_IS*V_PE + CLup_s^2*Q*T6_s*V_IS*V_PE + CLup_s^2*Q*T3_s*V_PE*V_VC + CLup_s^2*Q*T4_s*V_PE*V_VC + CLup_s^3*Q*V_IS*V_PE*V_VC + CLup_s^2*T1*T2*V_PE*V_PL + CLup_s^2*T1*T3_s*V_PE*V_PL + CLup_s^2*T1*T4_s*V_PE*V_PL + CLup_s^2*T2*T3_s*V_PE*V_PL + CLup_s^2*T2*T4_s*V_PE*V_PL + CLup_s^2*T2*T5_s*V_PE*V_PL + CLup_s^2*T2*T6_s*V_PE*V_PL + CLup_s^3*T1*V_IS*V_PE*V_PL + CLup_s^3*T2*V_PE*V_PL*V_VC + CLup_s^3*T3_s*V_IS*V_PE*V_PL + CLup_s^3*T4_s*V_IS*V_PE*V_PL + CLup_s^3*T5_s*V_IS*V_PE*V_PL + CLup_s^3*T6_s*V_IS*V_PE*V_PL + CLup_s^3*T3_s*V_PE*V_PL*V_VC + CLup_s^3*T4_s*V_PE*V_PL*V_VC + CLup_s^4*V_IS*V_PE*V_PL*V_VC + CLR_s*CL_dis_s*CLup_s*T1*V_IS + CLR_s*CL_dis_s*CLup_s*T2*V_VC + CLR_s*CL_dis_s*CLup_s*T3_s*V_IS + CLR_s*CL_dis_s*CLup_s*T4_s*V_IS + CLR_s*CL_dis_s*CLup_s*T5_s*V_IS + CLR_s*CL_dis_s*CLup_s*T6_s*V_IS + CLR_s*CL_dis_s*CLup_s*T3_s*V_VC + CLR_s*CL_dis_s*CLup_s*T4_s*V_VC + CL_dis_s*CLup_s*Q*T2*V_VC + CL_dis_s*CLup_s*Q*T3_s*V_IS + CL_dis_s*CLup_s*Q*T4_s*V_IS + CL_dis_s*CLup_s*Q*T5_s*V_IS + CL_dis_s*CLup_s*Q*T6_s*V_IS + CL_dis_s*CLup_s*Q*T3_s*V_VC + CL_dis_s*CLup_s*Q*T4_s*V_VC + CLR_s*CLup_s*T1*T2*V_PE + CL_dis_s*CLup_s*T1*T2*V_PE + CL_dis_s*CLup_s*T1*T2*V_PL + CLR_s*CLup_s*T1*T3_s*V_PE + CLR_s*CLup_s*T1*T4_s*V_PE + CLR_s*CLup_s*T2*T3_s*V_PE + CLR_s*CLup_s*T2*T4_s*V_PE + CLR_s*CLup_s*T2*T5_s*V_PE + CLR_s*CLup_s*T2*T6_s*V_PE + CL_dis_s*CLup_s*T1*T3_s*V_PE + CL_dis_s*CLup_s*T1*T4_s*V_PE + CL_dis_s*CLup_s*T2*T3_s*V_PE + CL_dis_s*CLup_s*T2*T4_s*V_PE + CL_dis_s*CLup_s*T2*T5_s*V_PE + CL_dis_s*CLup_s*T2*T6_s*V_PE + CL_dis_s*CLup_s*T1*T3_s*V_PL + CL_dis_s*CLup_s*T1*T4_s*V_PL + CL_dis_s*CLup_s*T2*T3_s*V_PL + CL_dis_s*CLup_s*T2*T4_s*V_PL + CL_dis_s*CLup_s*T2*T5_s*V_PL + CL_dis_s*CLup_s*T2*T6_s*V_PL)
# 3.b) Initialize ODEs
initialstate <- c(*[...some other cade that work unless you add the long equation above...])*
Bizarrely, if I write more than one of these longer equations, it recognises the end of exactly every second one, i.e. it pairs two, which leads to the issue. For the ones that it does not pair, it also has the > rather than + in the respective place in the console. I could get it to work fine for 5 short equations.
As I am exporting the equations from Matlab (as I need to use the symbolics toolbox to find the solution), I checked whether any of
cutting whitespaces out online, incl. equation by equation, so line by line
pasting the many equations into Word to see whether there is a difference in the tabs/new lines etc. between the equations for which the pasting interrupts between equations appropriately versus not (no both are the same)
past into text editor before moving to R, incl. equation by equation, so line by line
using my local version of R studio rather than the R studio workbench I was using previously
would help but they did not.
I have tried for a long time now and would be super grateful for any insight!
R is built with a maximum line length of 4096 characters as shown in this thread. Lines that are longer than that will need to be broken.
I am running the following code:
mydata1 = data.frame(dataset)
mydata1 <- na.omit(mydata1)
bw <- npplregbw(mydata1$X1 ~ mydata1$X2 + mydata1$X3 + mydata1$X4 + mydata1$effect_1993 + mydata1$effect_1994 + mydata1$effect_1995 + mydata1$effect_1996 + mydata1$effect_1997 + mydata1$country_2 + mydata1$country_3 + mydata1$country_4 + mydata1$country_5 + mydata1$country_6 + mydata1$effect_1998 + mydata1$effect_1999 + mydata1$effect_2000 + mydata1$effect_2001 + mydata1$effect_2002 + mydata1$effect_2003 + mydata1$effect_2004 + mydata1$effect_2005 + mydata1$effect_2006 + mydata1$effect_2007 + mydata1$effect_2008 + mydata1$effect_2009 + mydata1$effect_2010 + mydata1$effect_2011 + mydata1$effect_2012 + mydata1$effect_2013 + mydata1$effect_2014 + mydata1$effect_2015 + mydata1$effect_2016 + mydata1$effect_2017 + mydata1$effect_2018 + mydata1$effect_2019 + mydata1$effect_2020 + mydata1$effect_2021|mydata1$X5 + mydata1$X6 + mydata1$X7 + mydata1$X8, data = mydata1, na.action = na.omit)
summary(bw)
reg_np <- npplreg(bw)
The code is running fine except the last command which gives the following error:
Error in chol.default(t(model.matrix(model)) %*% model.matrix(model)) :
the leading minor of order 4 is not positive definite
My data do not have 0 (except the fixed effects data) or NA values.
Is there any way I can proceed with the npplreg regression without getting that error?
Thanks a lot in advance
I have this recurrence relation
L^2 G[p]= 2(p-1)(2p-1)G[p-1] + ((p-1)(p-2)+a^2) G[p-2], where L and a are parameters.
Does anyone could help me to find the solution? Thanks
This looks like quite a complicated calculation, especially when no start values are given.
To get some more insight, one could use sympy, Python's symbolic math library to print the formulas for small values of p:
from sympy import symbols
def func_G(p):
if p == 0:
return G0
elif p == 1:
return G1
else:
return (2 * (p - 1) * (2 * p - 1) * func_G(p - 1) + ((p - 1) * (p - 2) + a ** 2) * func_G(p - 2)) / L ** 2
a, L, G0, G1 = symbols('a L G0 G1')
for p in range(8):
print(p, ':', func_G(p).simplify())
Prints out:
0 : G0
1 : G1
2 : (G0*a**2 + 6*G1)/L**2
3 : (20*G0*a**2 + G1*L**2*(a**2 + 2) + 120*G1)/L**4
4 : (840*G0*a**2 + 42*G1*L**2*(a**2 + 2) + 5040*G1 + L**2*(a**2 + 6)*(G0*a**2 + 6*G1))/L**6
5 : (60480*G0*a**2 + 3024*G1*L**2*(a**2 + 2) + 362880*G1 + 72*L**2*(a**2 + 6)*(G0*a**2 + 6*G1) + L**2*(a**2 + 12)*(20*G0*a**2 + G1*L**2*(a**2 + 2) + 120*G1))/L**8
6 : (G0*L**4*a**6 + 26*G0*L**4*a**4 + 120*G0*L**4*a**2 + 10960*G0*L**2*a**4 + 90720*G0*L**2*a**2 + 6652800*G0*a**2 + 158*G1*L**4*a**4 + 2620*G1*L**4*a**2 + 5040*G1*L**4 + 398400*G1*L**2*a**2 + 1209600*G1*L**2 + 39916800*G1)/L**10
7 : (248*G0*L**4*a**6 + 7488*G0*L**4*a**4 + 38880*G0*L**4*a**2 + 1770240*G0*L**2*a**4 + 15966720*G0*L**2*a**2 + 1037836800*G0*a**2 + G1*L**6*a**6 + 44*G1*L**6*a**4 + 444*G1*L**6*a**2 + 720*G1*L**6 + 28224*G1*L**4*a**4 + 526080*G1*L**4*a**2 + 1088640*G1*L**4 + 62513280*G1*L**2*a**2 + 199584000*G1*L**2 + 6227020800*G1)/L**12
I'm trying to mutate a character column in a dataframe, the result should return another character column, but in reality some of the elements in the column are converted to numeric. How could this happen?
This is the dataframe, both sumspec and rate.express are character vectors
sumspec= c("TA","TCO2")
rate.express = c("(rNC-rPC)*RO2POC[i]*dstopw[i] + (rNC-rPC+4/3)*RNO2POC[i]*dstopw[i] + (rNC-rPC)*RNO3POC[i]*dstopw[i] + (rNC-rPC+4)*RMnO2POC[i]*dstopw[i] + (rNC-rPC+8)*RFeOOHPOC[i]*dstopw[i] + (rNC-rPC+1)*RSO4POC[i]*dstopw[i] + (rNC-rPC)*RCH4POC[i]*dstopw[i] + -2*RO2NH4[i] + -2*RO2Mn[i] + -1*RO2Mn_ads[i] + -2*RO2Fe[i] + -1*RO2Fe_ads[i] + -2*RO2H2S[i] + -2*RO2FeS[i]*dstopw[i] + -8/5*RNO3Mn[i] + -1.8*RNO3Fe[i] + 2*RSO4CH4[i] + -1*RMnO2Fe[i]*dstopw[i] + 2*RMnO2H2S[i]*dstopw[i] + 4*RFeOOHH2S[i]*dstopw[i] + -2*RFeS_pre[i]*dstopw[i] + 2*RFeS_dis[i]*dstopw[i] + 2*RCaCO3_dis[i]*dstopw[i] + -2*RCaCO3_pre[i]*dstopw[i] + 2*RMnCO3_dis[i]*dstopw[i] + -2*RMnCO3_pre[i]*dstopw[i] + 2*RFeCO3_dis[i]*dstopw[i] + -2*RFeCO3_pre[i]*dstopw[i] + 10*RAnnite_dis[i]",
"1*RO2POC[i]*dstopw[i] + 1*RNO2POC[i]*dstopw[i] + 1*RNO3POC[i]*dstopw[i] + 1*RMnO2POC[i]*dstopw[i] + 1*RFeOOHPOC[i]*dstopw[i] + 1*RSO4POC[i]*dstopw[i] + 0.5*RCH4POC[i]*dstopw[i] + 1*RO2CH4[i] + 1*RSO4CH4[i] + 1*RCaCO3_dis[i]*dstopw[i] + -1*RCaCO3_pre[i]*dstopw[i] + 1*RMnCO3_dis[i]*dstopw[i] + -1*RMnCO3_pre[i]*dstopw[i] + 1*RFeCO3_dis[i]*dstopw[i] + -1*RFeCO3_pre[i]*dstopw[i]")
Now mutate the colume rate.express
dH <- data.frame(sumspec,
rate.express) %>%
mutate(RHS = ifelse(
sumspec == "TA", rate.express,
paste0("-1.0*(",rate.express,")*dTA/d",sumspec,"[i]")
))
Which returns
> dH$RHS
[1] "1"
[2] "-1.0*(1*RO2POC[i]*dstopw[i] + 1*RNO2POC[i]*dstopw[i] + 1*RNO3POC[i]*dstopw[i] + 1*RMnO2POC[i]*dstopw[i] + 1*RFeOOHPOC[i]*dstopw[i] + 1*RSO4POC[i]*dstopw[i] + 0.5*RCH4POC[i]*dstopw[i] + 1*RO2CH4[i] + 1*RSO4CH4[i] + 1*RCaCO3_dis[i]*dstopw[i] + -1*RCaCO3_pre[i]*dstopw[i] + 1*RMnCO3_dis[i]*dstopw[i] + -1*RMnCO3_pre[i]*dstopw[i] + 1*RFeCO3_dis[i]*dstopw[i] + -1*RFeCO3_pre[i]*dstopw[i])*dTA/dTCO2[i]"
So the mutate rate.express should just return the original value for the first element but it is converted to numeric. The second element is OK. I think it is converted to numeric because
> as.numeric(dH$rate.express[1])
[1] 1
In stead, if I explicitly use as.character to rate.express
> dH <- data.frame(sumspec,
+ rate.express) %>%
+ mutate(RHS = ifelse(
+ sumspec == "TA", as.character(rate.express), # explicitly state type
+ paste0("-1.0*(",rate.express,")*dTA/d",sumspec,"[i]")
+ ))
Then the result is correct
Why did this type conversion happen?
π = π΄'π΅'πΆ'π·' + π΄'π΅'πΆπ·' + π΄'π΅πΆ'π· + π΄'π΅πΆπ· + π΄'π΅πΆπ·' + π΄'π΅'πΆπ·' + π΄π΅'πΆD'
I've done the K-Map for the following and got A'D' + A'B but can't figure out how to show the simplification that the K-Map derived.