我只是想知道是否存在将rmarkdown文件编织到Word docx文件中的通用方法,但是我可以保留相应的数字和代码行。我希望能够在解释代码时通过引用其特定的编号行来轻松引用和引用特定的代码行。
例如,我想说:“在代码行223中,出现了x y和z”
这是两个示例代码块:
## Monetary value of harm from accident
D = 5
## Maximum and minimum effort costs
maxw = 5
minw = 1
## Ex-ante average distribution cost of effort
wbar = (maxw+minw)/2
#True a priori negligece standard based on ex-ante average of distribution of cost of effort
estar = 1 - wbar/(2*D)
## Number (100) of simulations run in the loop
iteration = (1)
## Series of empty vectors used later for aggregating values from each simulation. In order of appearance, we have vector of driver 1
age1 = c()
age2 = c()
age3 = c()
age4 = c()
aga = c()
agb = c()
agna = c()
## Series of empty vectors used later for aggregating drawn costs by each driver in each simulation.
w1 = c()
w2 = c()
w3 = c()
w4 = c()
这部分代码包含iteration
个模拟的播放顺序的逐步概述。
#The simulation is encapsulated by a for-loop that repeats the simulation for the number of iterations that is set above.
for (i in iteration) {
# Before the simulation begins, I create a series of empty vectors that serve as placeholders for the changes in the legal threshold, the decisions of each driver, and the number of periods that have passed since a driver has been in an accident. These are all encapsulated by a "reset" call object, which resets all the vectors back to their empty forms for the next iteration/run-through of the simulation.
reset = {
t = 1
a = c() #lower bound of belief in estar
b = c() #upper bound of belief in estar
e1 = c() #empty vector of driver one's decisions
e2 = c()
e3 = c()
e4 = c()
e = c() #two empty vectors that will serve as placeholders for effort levels of drivers that get in an accident
eaccident = c()
na = c() #number of periods that take place before an accident takes place.
na1 = c() #number of periods that take place before Driver One has an accident.
na2 = c()
na3 = c()
na4 = c()
# Each driver draws a constant marginal cost of effort from a uniform four-point distribution. This constant marginal cost will be fixed for the entirety of the simulation.
w1[i] = sample(c(1,2,4,5),size = 1, replace = TRUE, prob = c(0.25,0.25,0.25,0.25))
w2[i] = sample(c(1,2,4,5),size = 1, replace = TRUE, prob = c(0.25,0.25,0.25,0.25))
w3[i] = sample(c(1,2,4,5),size = 1, replace = TRUE, prob = c(0.25,0.25,0.25,0.25))
w4[i] = sample(c(1,2,4,5),size = 1, replace = TRUE, prob = c(0.25,0.25,0.25,0.25))
}
# Below is a while-loop imbedded within the for-loop above
while (TRUE){
if(t==1){a[t]=0.40;b[t]=0.90}
if(t==1){na1[t]=0;na2[t]=0;na3[t]=0;na4[t]=0}
e[t] = 0
##Drivers problem: pick the e that will minimize your cost --- piecewise optimization. First object is if (e<=a), second is if (a<e<b), and third is when (e>=b).
e11 = (1-(w1[i]/(2*D)))
e12 = ((2+b[t])-sqrt((b[t]^2)-2*b[t]+1+3*(w1[i]/D)*(b[t]-a[t])))/3
e13 = b[t]
c11 = (w1[i]*e11)+(((e11-1)^2)*D)
c12 = (w1[i]*e12)+(((e12-1)^2)*((b[t]-e12)/(b[t]-a[t]))*D)
c13 = w1[i]*e13
## Same for Driver Two
e21 = (1-(w2[i]/(2*D)))
e22 = ((2+b[t])-sqrt((b[t]^2)-2*b[t]+1+3*(w2[i]/D)*(b[t]-a[t])))/3
e23 = b[t]
c21 = (w2[i]*e21)+(((e21-1)^2)*D)
c22 = (w2[i]*e22)+(((e22-1)^2)*((b[t]-e22)/(b[t]-a[t]))*D)
c23 = w2[i]*e23
## Same for Driver Three
e31 = (1-(w3[i]/(2*D)))
e32 = ((2+b[t])-sqrt((b[t]^2)-2*b[t]+1+3*(w3[i]/D)*(b[t]-a[t])))/3
e33 = b[t]
c31 = (w3[i]*e31)+(((e31-1)^2)*D)
c32 = (w3[i]*e32)+(((e32-1)^2)*((b[t]-e32)/(b[t]-a[t]))*D)
c33 = w3[i]*e33
## Same for Driver Four
e41 = (1-(w4[i]/(2*D)))
e42 = ((2+b[t])-sqrt((b[t]^2)-2*b[t]+1+3*(w4[i]/D)*(b[t]-a[t])))/3
e43 = b[t]
c41 = (w4[i]*e41)+(((e41-1)^2)*D)
c42 = (w4[i]*e42)+(((e42-1)^2)*((b[t]-e42)/(b[t]-a[t]))*D)
c43 = w4[i]*e43
## These lines of code
c1star = min(c11,c12,c13)
c2star = min(c21,c22,c23)
c3star = min(c31,c32,c33)
c4star = min(c41,c42,c43)
e1[t] = if(c1star==c11){e11} else if(c1star==c12 & (e12 < e13)){e12} else if(c1star==c13 || (e12 >= e13)){e13}
e2[t] = if(c2star==c21){e21} else if(c2star==c22 & (e22 < e23)){e22} else if(c2star==c23 || (e22 >= e23)){e23}
e3[t] = if(c3star==c31){e31} else if(c3star==c32 & (e32 < e33)){e32} else if(c3star==c33 || (e32 >= e33)){e33}
e4[t] = if(c4star==c41){e41} else if(c4star==c42 & (e42 < e43)){e42} else if(c4star==c43 || (e42 >= e43)){e43}
proba1 = function(e1){(e1[t]-1)^2} #probability of accident only given driver1's effort level.
probn1 = 1 - proba1(e1) #probability of not getting in an accident given driver1's effort level
proba2 = function(e2){(e2[t]-1)^2}
probn2= 1 - proba2(e2)
proba3 = function(e3){(e3[t]-1)^2}
probn3= 1 - proba3(e3)
proba4 = function(e4){(e4[t]-1)^2}
probn4= 1 - proba4(e4)
while(e[t]!="e1[t]" & e[t]!="e2[t]" & e[t] != "e3[t]" & e[t] != "e4[t]"){e[t] = sample(c("e1[t]","e2[t]","e3[t]","e4[t]",0,0,0,0),size = 1, replace = TRUE, prob = c(proba1(e1),proba2(e2), proba3(e3),proba4(e4), probn1, probn2, probn3, probn4))
if(e[t]== "0"){
na[t] = na[t]+1
na1[t] = na1[t]+1
na2[t] = na2[t]+1
na3[t] = na3[t]+1
na4[t] = na4[t]+1}}
show(e[t])
eaccident[t]={if(e[t]=="e1[t]"){"Driver 1"}
else if(e[t]=="e2[t]"){"Driver 2"}
else if(e[t]=="e3[t]"){"Driver 3"}
else if(e[t]=="e4[t]"){"Driver 4"}}
{if(e[t]=="e1[t]"){
na1[t+1] = 0
na2[t+1] = na2[t]+1
na3[t+1] = na3[t]+1
na4[t+1] = na4[t]+1}
else if(e[t]=="e2[t]"){
na2[t+1] = 0
na1[t+1] = na1[t]+1
na3[t+1] = na3[t]+1
na4[t+1] = na4[t]+1}
else if(e[t]=="e3[t]"){
na3[t+1] = 0
na2[t+1] = na2[t]+1
na1[t+1] = na1[t]+1
na4[t+1] = na4[t]+1}
else if(e[t]=="e4[t]"){
na4[t+1] = 0
na2[t+1] = na2[t]+1
na1[t+1] = na1[t]+1
na3[t+1] = na3[t]+1}}
if (e[t]=="e1[t]"){e[t] = e1[t];e = as.numeric(e)} else if (e[t]=="e2[t]"){e[t] = e2[t];e = as.numeric(e)} else if (e[t]=="e3[t]"){e[t] = e3[t];e = as.numeric(e)} else if (e[t]=="e4[t]"){e[t] = e4[t];e = as.numeric(e)}
probg = function(e,a,b){(b[t]-e[t])/(b[t]-a[t])} ### probability of being found guilty given e in the ambiguous region
probi = function(e,a,b){1-probg(e,a,b)} ### probability of being found innocent given e in the ambiguous region
rulings = if(e[t]>=b[t]){ruling = print("Not Negligent")
} else if(e[t]<=a[t]) {ruling = print("Negligent")
} else if(e[t]>a[t] & e[t]<b[t]) {ruling = if(e[t]<estar){"guilty"} else if(e[t]>=estar){"not guilty"}; show(ruling)}
if(ruling == "guilty") {a[t+1] = e[t]; b[t+1] = b[t]} else if (ruling == "not guilty") {b[t+1] = (e[t]); a[t+1]=a[t]} else {a[t+1]=a[t]; b[t+1] = b[t]; print("thresholds unchanged")}
e = round(e,digits= 5)
e1 = round(e1,digits = 5)
e2 = round(e2, digits = 5)
e3 = round(e3, digits = 5)
e4 = round(e4, digits = 5)
a = round(a, digits = 5)
b = round(b, digits = 5)
threshold = 0.00009
lower_limit = e - threshold
upper_limit = e + threshold
if((a[t+1]==a[t]) && (b[t+1] == b[t]) && (all(sapply(c(e1[t], e2[t], e3[t],e4[t]), function(c) any(c >= lower_limit & c <= upper_limit)))) && ((e1[t] <= a[t]) | (e1[t] >= b[t])) && ((e2[t] <= a[t]) | (e2[t] >= b[t])) && ((e3[t] <= a[t]) | (e3[t] >= b[t])) && ((e4[t] <= a[t]) | (e4[t] >= b[t]))){break} else {print("not equilibrium")}
t= t+1
}
print("Simulation End")
age1[[i]] = e1
age2[[i]] = e2
age3[[i]] = e3
age4[[i]] = e4
aga[[i]] = a
agb[[i]] = b
}
谢谢