how to rename variables in datasummary (rmarkdown, latex pdf) - latex

I am unable to rename the variables in datasummary. I don't think it has an option to do that. Any tips?
summary_vars <- as.formula(glue("any_prio + any_prio_on + any_prio_off + war_prio + war_prio_on +
war_prio_off + war_col + war_inc + war + GPCP + GPCP_g + GPCP_g_l + gdp_g +
gdp_g_l + y_0 + polity2l + polity2l_6 + deml + ethfrac + relfrac + Oil +
mtnest + lpopl1 + tot_100_g ~ mean*Arguments(na.rm = TRUE) + sd*Arguments(na.rm = TRUE) + N"))
TableA1 <- datasummary(data = mss_rep, summary_vars, booktabs = T,
fmt = 2,
col.names = c("", "Mean", "Standard Deviation", "Observations"),
caption = "DESCRIPTIVE STATISTICS") |>
kable_styling() |>
pack_rows("A. Civil Conflict Measures (1981–99)", 1, 9) |>
pack_rows("B. Rainfall Measures (1981–99)", 10, 12) |>
pack_rows("C. Economic Growth", 13, 14) |>
pack_rows("D. Country Characteristics", 15, 24)
TableA1

You could save the datasummary as a dataframe, do the renaming and apply a new kbl afterwards, e.g.
library(modelsummary)
library(kableExtra)
summary_df <- datasummary(mpg + cyl ~ mean, data = mtcars, output = "data.frame", fmt = 2)
# renaming
summary_df[,1] <- c("MPG", "CYL")
summary_df |>
kbl(booktabs = TRUE) |>
kable_styling() |>
pack_rows("A. Civil Conflict Measures (1981–99)", 1, 2)

Related

Unit root test on lagged variables

guys!
I'm using this sintaxe but when I use the last part ("modelo = ...") it shows the following: "Error in merge.zoo(difetanol, detanol, mes2, mes3, mes4, mes5, mes6, mes7, :
all(sapply(args, function(x) is.zoo(x) || !is.plain(x) || (is.plain(x) && .... is not TRUE"
ethidra = (tsrelatorio[,"etanol"])
ethidra
length(ethidra)
acuavhp = (tsrelatorio[,"avhp"])
acuavhp
length(acuavhp)```
matr <- matrix(0, nr = 12, nc = 12)
matr[row(matr) == col(matr)] <- 1
matr
Djt <- rbind(matr,matr,matr,matr); Djt
dim(Djt)
names(Djt)
dimnames(Djt) = list(c(),c("M1", "M2", "M3", "M4","M5","M6","M7","M8","M9","M10","M11","M12"))
Djt
dim(Djt)```
teste=data.frame(Djt)
attach(teste)
mes2= teste[1:47,2]
mes2
length(mes2)
mes3= teste[1:47,3]
mes3
length(mes3)
mes4= teste[1:47,4]
mes4
length(mes4)
mes5= teste[1:47,5]
mes5
length(mes5)
mes6= teste[1:47,6]
mes6
length(mes6)
mes7= teste[1:47,7]
mes7
length(mes7)
mes8= teste[1:47,8]
mes8
length(mes8)
mes9= teste[1:47,9]
mes9
length(mes9)
mes10= teste[1:47,10]
mes10
length(mes10)
mes11= teste[1:47,11]
mes11
length(mes11)
mes12= teste[1:47,12]
mes12
length(mes12)
detanol = (ethidra[1:47])
detanol
length(detanol)
difetanol=diff(detanol)
difetanol
length(difetanol)
tempo = tempo <- seq(1:47)
modelo = dynlm(difetanol ~ detanol + mes2 +mes3 +mes4 + mes5 + mes6 + mes7 + mes8 + mes9 + mes10 + mes11 + mes12 + tempo + L(difetanol,1) + L(difetanol,2))
summary(modelo)

Regular expression from this dfa

what will be regular express of this transition graph anyone?can make and explain this please i will be very thankful to him.
We can write some equations for this:
(q0) = e + (q1)a + (q3)b
(q1) = (q0)a + (q2)b
(q2) = (q1)b + (q3)a
(q3) = (q0)b + (q2)a
You can read these equations like this: "the set of strings that leads me to state X is the set of strings that lead me to state Y followed by symbol c, or the set of strings that lead me to state Z followed by symbol d, or..."
We can now solve these equations using substitution and a rule to eliminate self-references, namely:
if (q) = (q)x + y, then (q) = y(x*)
We can start solving the system by eliminating (q3):
(q0) = e + (q1)a + [(q0)b + (q2)a]b
(q1) = (q0)a + (q2)b
(q2) = (q1)b + [(q0)b + (q2)a]a
Distributing:
(q0) = e + (q1)a + (q0)bb + (q2)ab
(q1) = (q0)a + (q2)b
(q2) = (q1)b + (q0)ba + (q2)aa
We can get rid of (q1) now:
(q0) = e + [(q0)a + (q2)b]a + (q0)bb + (q2)ab
(q2) = [(q0)a + (q2)b]b + (q0)ba + (q2)aa
Distributing:
(q0) = e + (q0)aa + (q2)ba + (q0)bb + (q2)ab
(q2) = (q0)ab + (q2)bb + (q0)ba + (q2)aa
Grouping like terms:
(q0) = e + (q0)(aa + bb) + (q2)(ab + ba)
(q2) = (q0)(ab + ba) + (q2)(aa + bb)
Using the rule to remove self-reference:
(q0) = (aa + bb)* + (q2)(ab + ba)(aa + bb)*
(q2) = (q0)(ab + ba)(aa + bb)*
Substituting to get rid of (q2):
(q0) = (aa + bb)* + [(q0)(ab + ba)(aa + bb)*](ab + ba)(aa + bb)*
Distributing:
(q0) = (aa + bb)* + (q0)(ab + ba)(aa + bb)*(ab + ba)(aa + bb)*
Using the rule for self-reference:
(q0) = (aa + bb)*[(ab + ba)(aa + bb)*(ab + ba)(aa + bb)*]*
from the given transition diagram or DFA, equations for each state can be written as:
q0 = ε+q1.a+q3.b
q1 = q0.a+q2.b
q2 = q1.b+q3.a
q3 = q0.b+q2.a
substitute q3 in q2,
q2=q1.b+(q0.b+q2.a)a
q2=q2.aa+q0.ba+q1.b
substitute q1 in q2
q2=q2.aa+q0.ba+(q0.a+q2.b)b
q2=q2.aa+q0.ba+q0.ab+q2.bb
finally,
q2=q2(aa+bb)+q0(ba+ab)
It is in the form of t=tr+s
According to Arden's theorem,
the solution is t=sr*
So,q2=q0(ba+ab)(aa+bb)
Now substitute q3 in the equation q0,
q0=ε+q1.a+(q0.b+q2.a)b
q0=ε+q1.a+q0.bb+q2.ab
q0=q0.bb+(q1.a+q2.ab+ε)
substitute q1 in q0,
q0=q0.bb+(q0.a+q2.b)a+q2.ab+ε
q0=q0.bb+q0.aa+q2.ba+q2.ab+ε
q0=q0(bb+aa)+q2(ba+ab)+ε
Now substitute the q2 in the above equation,
q0=q0(bb+aa)+q0(ba+ab)(aa+bb)*(ba+ab)+ε
q0=q0((bb+aa)+(ba+ab)(aa+bb)*(ba+ab))+ε
Now q0 is in the form of t=tr+s so the solution is t=sr*
q0=((bb+aa)+(ba+ab)(aa+bb)(ab+ba))
The Regular Expression for the given DFA is:
q0=((bb+aa)+(ba+ab)(aa+bb)*(ab+ba))

Recurrence relation: T(n) = T(n - 1) + n - 1

I am trying to understand how to solve recurrence relations. I understand it to the point where we have to simplify.
T(N) = T(N-1) + N-1 Initial condition: T(1)=O(1)=1
T(N) = T(N-1) + N-1
T(N-1) = T(N-2) + N-2
T(N-2) = T(N-3) + N-3
……
T(2) = T(1) + 1
**Summing up right and left sides**
T(N) + T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) =
= T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) + T(1) +
(N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
** Canceling like terms and simplifying **
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
T(N) = 1 + N*(N - 1)/2
I really don't understand the last part. I understand canceling like terms but don't understand how the simplification below works:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
How is the second line derived from the first? Doesn't make any sense to me.
Would be a great help if someone can help me understand this. Thanks =)
In your second-to-last-line:
S = (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
You can say:
2S = S + S
= (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
1 + 2 + 3 + ... + (N-3) + (N-2) + (N-1)
= N + N + N + ... + N + N + N
|__________________ N-1 times ________________|
You're counting from N - 1 to 1, so there are N - 1 terms in the sequence. But the whole sequence is just N so you can say:
2S = N * (N - 1)
S = (N * (N - 1)) / 2
So in your last chunk:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
= T(1) + (N * (N - 1)) / 2
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
= T(1) + (N-1) + (N-2) + (N-3) + ..... + ( N-(N-3)) + (N-(N-2)) + (N-(N-1))
= T(1) + [N+N+N+..... n-1 times] - [1+2+3+......+(N-3)+(N-2)+(N-1)]
= T(1) + N*(N-1) - (N*(N-1))/2
= T(1) + N*(N-1)/2

Recurrence Relation Homework Struggles

Here's the question:
Solve the recurrence by obtaining a theta bound for T(n) given that T(1) = theta(1).
T(n) = n + T(n-3)
Attempted Solution:
T(n) = T(n-6) + (n-3) + n
= T(n-9) + (n-6) + (n-3) + n
= T(n-(n-1)) + [(n-n) + (n-(n-3)) + (n-(n-6)) + ... + n]
= T(1) + [0 + 3 + 6 + ... + n]
= theta(1) = 3[1 + 2 + 3 + ... + n/3]
= theta(1) + [(n/3)(n/3 + 1)]/2
= theta(1) + (n^2+3n)/6
When I double check to see if the solution fits the recurrence, it doesn't work.
The issue was that you were getting the wrong summation.
It doesn't start at 0, since your last T function was T(n - (n-1)) , which means previous one was T(n-(n-4)). So the summation starts at 4, and goes up till n.
If you don't know how to find the summation of this, I'd suggest you look at some of the proofs from the summation formula. This is what the solution looks like.
T(n) = T(n-3) + n
= T(n-6) + (n-3) + n
= T(n-(n-1)) + [ (n-(n-4)) + (n-(n-7)) + ... + n]
= T(1) + [4 + 7 + ... + n]
= theta(1) + (4 + n) * (n - 1)/6

Recurrence Relation: Finding Big O

I am trying to find the big O bound for the following recurrence relation:
T(n) = T(n-1) + n^c, where c >= 1 is a constant
So I've decided to solve this by using iteration:
T(n) = T(n-1) + n^c
T(n-1) = T(n-2) + (n-1)^c
T(n) = T(n-2) + n^c + (n-1)^c
T(n-2) = T(n-3) + (n-2)^c
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + .... + (n-k+1)^c
Suppose k = n-1, then:
T(n) = T(1) + n^c + (n-1)^c + (n-n+1+1)^c
T(n) = n^c + (n-1)^c + 2^c + 1
I'm not sure if this is correct however, plus I would really appreciate some guidance as to how to derive Big O from this. Thanks a lot!
Yes, you are correct:
T(n) = nc + (n-1)c + (n-2)c + … + 3c + 2c + 1,
and this sum is
T(n) = O(nc+1). See e.g. Faulhaber's formula. In fact, you can even determine the constant in the leading term (even if it's not germane to the algorithm's asymptotics): the sum is nc+1/(c+1) + O(c), as you can determine through e.g., using, say, integration.
What you have is not correct, but you were on the right track.
The mistake you made:
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + (n-k+1)^c
You cannot just go from the first line to the second line.
As you increase k, the number of terms in the right hand side increases too.
To see that think of writing it this way:
T(n) - T(n-1) = n^c.
T(n-1) - T(n-2) = (n-1)^c
..
T(n-k) - T(n-k-1) = (n-k)^c.
..
T(2) - T(1) = 2^c
What happens if you add these up?
Once you do that, can you see what the answer will be for c=1 and c=2? Can you figure out a pattern for the final answer from there?
Instead of working you way down from n, how about start by working your way up from 0 (I assume the recursion terminates at 0 and you left that bit out). When you start noticing a fixed point (ie a pattern which repeats the same in all cases) you have a good candidate for an answer. Try proving the answer, e.g. through induction.
I would start by observing that n^c, whilst of course influenced by the value of n, is not going to be any more complex for n vs. n + 1 - it's c that determines the "runtime" of that particular term. Given that, you can assume (at least I would) that the term has constant runtime and determine how many times the recursion will execute for a given n and you'll get your bound.
To figure these out, fill out a couple of terms and look for the pattern:
T(1) = 0 + 1^c
T(2) = T(1) + 2^c = 0 + 1^c + 2^c
T(3) = T(2) + 3^c = 0 + 1^c + 2^c + 3^c
T(4) = ...
Now express the pattern in terms of n and you have your answer.
Here it is:
T(n) = n^c + (n-1)^c + (n-2)^c + ... + 2^c + 1^c
< n^c + n^c + n^c + ... + n^c + n^c
= n * n^c
= n^(c+1)
which is O(nc+1).
To show this is a reasonable bound, note that when c = 1,
T(n) = n + (n-1) + (n-2) + ... + 2 + 1
= n * (n-1) / 2
which is clearly Θ(n2).

Resources