How do I include my code and plots in a PDF file created by R markdown?

Hello, I have written codes that makes plot of different dists. I want the PDF file to include my codes . How can I do that? My code is in the comment section.


#gaussisk

liste = c(29.59, 29.23, 31.05, 17.43, 31.27, 23.79, 36.49, 40.09)

#posterior hyperparametere

k0 = 0
S0 = 0
C0 = 0
n = 8 #viktig faktor

k1 = k0  + n
k1 #k1 verdi

S1 = S0 + sum(liste)
S1

C1 = C0 + sum(liste^2)
C1

v1 = -1 + n

m = S1/k1 #mean


SS1 = C1 - k1 * m^2
SS1

s1 = sqrt(SS1/v1)

#posterior values

#tau
xVals = seq(0,0.1,0.001)
yVals = dgamma(xVals,v1/2,SS1/2)
plot(xVals,yVals,type = "l", col = "red")

#forventet
xVals1 = seq(0,50,0.01)
yVals1 = dt.scaled(xVals1, v1 , m , s1 * sqrt(1/k1))
plot(xVals1, yVals1 , type = "l", col = "blue")
s1 * sqrt(1/k1)
#prediktiv
xVals2 = seq(0,50,0.01) 
yVals2 = dt.scaled(xVals1, v1 , m , s1 * sqrt(1+1/k1))
plot(xVals1, yVals2 , type = "l", col = "blue")
s1 * sqrt(1+1/k1)

#II P(X+ < 18 ) #oppgavene varier
pt.scaled(18, v1 , m , s1 * sqrt(1+1/k1))
# P(X+ < 18 ) = 0.0772085

#III svarveien #oppgavenen varierer #gjort noe feil
sd = sd(liste)
sd
m = m
m

#putter tallene i normalfordelingen
yVals4 = dnorm(xVals2, m , sd)
plot(xVals2,yVals4,type = "l", col = "green", main = "Snarvei(normal) er grønn og X+ er rød")
lines(xVals2, yVals2,type = "l", col = "red")

# ingen kommentar

#IV #hypotesetest referanse punkt

# H1: X+ > 18
# H0 : ≤ X+ 18

pt.scaled(18,v1,m,s1 * sqrt(1+1/k1))

# 0.0772 > 0.05
#forkaster ikke H0 i fordel for H1


#Normalfordeling

pnorm(18,m,sd)

# 0.04519901 < 0.05
#forkaster H0 til fordel for H1


If you are not familiar with R Markdown yet, you can start by here to get a quick tutorial and overview: Introduction

You'll find plenty other resources only to start with R Makrdown.

Once you'll have a working document mixing your text, and your code, you'll be able to render to an output format. If you encounter issue, you can come back here and share your struggle.

Hope it helps

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.