-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex2.R
209 lines (149 loc) · 5.19 KB
/
ex2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#Practical class 2
############
#1)
############
# plot the density function for N(0,1)
xseq <- seq(-5,5,0.01)
xdnorm.0.1 <- dnorm(x,0,1)
plot(xseq,xdnorm.0.1,type="l",lwd=2.5)
# add the density function for T(2) in the plot
xdt.2 <- dt(xseq,df=2)
points(xseq,xdt.2,col="blue",type="l")
# add the density function for T(5) in the plot
xdt.5 <- dt(xseq,df=5)
points(xseq,xdt.5,col="blue",type="l")
# add the density function for T(5) in the plot
xdt.10 <- dt(xseq,df=10)
points(xseq,xdt.10,col="blue",type="l")
# add the density function for T(5) in the plot
xdt.30 <- dt(xseq,df=30)
points(xseq,xdt.30,col="blue",type="l")
# add the density function for T(5) in the plot
xdt.100 <- dt(xseq,df=100)
points(xseq,xdt.100,col="blue",type="l")
# As the sample size increases (or degrees of freedom), the variance from the t-student distribution decreases and approaches the standard normal distribution.
# See that the degrees of freedom are computed as n-1, where n is the sample size.
############
#2)
############
binom.1K.20.5 <- rbinom(n=1000,size = 20,prob = 0.5)
hist(binom.1K.20.5)
set.seed(3)
binom.1K.50.5 <- rbinom(n=1000,size = 50,prob = 0.5)
hist(binom.1K.50.5,freq = F)
# The normal distribution can be used to approximate the binomial by defining the mean as n*p and variance as n*p*(1-p),
# where n is the number of trials and p the probability of success.
# Do not confuse the number of trials with the sample size, which can also be represented by the letter n.
# Note: the normal distribution is continuous and the binomial is discrete.
# Note: the normal distribution can be used as an approximation when n*p > 5 and n*p* (1-p) > 5.
# binom.1K.50.5 distribution will be used for the approximation
binmo.1K.50.5.mean <- 50*0.5
binmo.1K.50.5.sd <- sqrt(50*0.5*0.5)
xseq<-seq(0,50,0.1)
norm.approx <- dnorm(xseq,binmo.1K.50.5.mean,binmo.1K.50.5.sd)
lines(xseq,norm.approx,col="red")
############
#3)
############
# The lambda parameter of the Poisson distribution is both the expected value and the variance
# Note: the normal distribution is continuous and the Poisson is discrete.
# Note: the normal distribution can be used as an approximation when lambda is bigger than 10.
# Overlay histograms
hist(rpois(1000,12),xlim=c(0,30),ylim=c(0,250),col=rgb(0,0,0.5,0.5))
hist(rnorm(1000,12,sqrt(12)),add=T,col=rgb(0.5,0,0,0.5))
############
#4)
############
pnorm(1.96,0,1)
pt(1.96,2)
pt(1.96,5)
pt(1.96,10)
pt(1.96,30)
pt(1.96,100)
pt(1.96,1000000)
#5)
MyTableBio<-read.csv("~/Dropbox/Doctorado/CoursesAndWorkshops/MarMic_Statistics/bioenv-2.csv",header=T,sep=",")
# case a vs temperature
#x<-MyTableBio$Temperature
#y <- 4 * x^3 + rnorm(150, 300, 300)
#y<-abs(y)
#x.df<-as.data.frame(round(x,1))
#y.df<-as.data.frame(round(abs(y),0))
#plot(x,y)
#plot(lm(y~x))
plot(MyTableBio$a,MyTableBio$Temperature)
#case b vs temperature
#MyTableBio$Temperature
#x<-MyTableBio$Temperature
#y<- 4*x + 3 +rnorm(150,0,5)
#y.df<-as.data.frame(round(y,0))
#plot(x,y)
#plot(lm(y~x))
plot(MyTableBio$b,MyTableBio$Temperature)
#case c vs temperature
#y<-x
#x<-MyTableBio$Temperature
#quantile(x)
#sum(x<=8.9)
#y[x<=9.3]=rnorm(sum(x<=9.3),10,3)
#y[x>9.3]=rnorm(sum(!x<=9.3),100,10)
#y.df<-as.data.frame(round(y,0))
plot(MyTableBio$Temperature,MyTableBio$c)
#case d vs temperature
#x<-MyTableBio$Temperature
#y <- 5*x + rnorm(150,3,2)
#y.df<-as.data.frame(round(y,0))
#y[x>9.9] <- rnorm(sum(x>9.9),120,4)
#plot(x,y)
plot(MyTableBio$Temperature,MyTableBio$d)
smlc<-lm(MyTableBio$d~MyTableBio$Temperature)
sum(var(cbind(MyTableBio$c,MyTableBio$Temperature)))
var(residuals(smlc))
var(fitted(smlc))
####################
#6)
#### a
plot(MyTableBio$Temperature,MyTableBio$a)
slm.a_vs_t <- lm(MyTableBio$a~MyTableBio$Temperature)
abline(slm.a_vs_t,col="red",lwd=2)
hist(residuals(slm.a_vs_t))
plot(fitted(slm.a_vs_t),residuals(slm.a_vs_t))
nq <- quantile(dnorm(seq(-3.5,3.5,by=0.1),0,1),probs=seq(0,1,by=0.01))
eq <- quantile(residuals(slm.a_vs_t),probs=seq(0,1,by=0.01))
plot(nq,eq)
#### b
plot(MyTableBio$Temperature,MyTableBio$b)
slm.b_vs_t <- lm(MyTableBio$b~MyTableBio$Temperature)
abline(slm.b_vs_t,col="red",lwd=2)
hist(residuals(slm.b_vs_t))
plot(fitted(slm.b_vs_t),residuals(slm.b_vs_t))
text()
nq <- quantile(dnorm(seq(-3.5,3.5,by=0.1),0,1),probs=seq(0,1,by=0.01))
eq <- quantile(residuals(slm.b_vs_t),probs=seq(0,1,by=0.01))
plot(nq,eq)
##### c
plot(MyTableBio$Temperature,MyTableBio$c)
slm.c_vs_t <- lm(MyTableBio$c~MyTableBio$Temperature)
abline(slm.c_vs_t,col="red",lwd=2)
hist(residuals(slm.c_vs_t))
plot(fitted(slm.c_vs_t),residuals(slm.c_vs_t))
nq <- quantile(dnorm(seq(-3.5,3.5,by=0.1),0,1),probs=seq(0,1,by=0.01))
eq <- quantile(residuals(slm.c_vs_t),probs=seq(0,1,by=0.01))
plot(nq,eq)
##### d
plot(MyTableBio$Temperature,MyTableBio$d)
slm.d_vs_t <- lm(MyTableBio$d~MyTableBio$Temperature)
abline(slm.d_vs_t,col="red",lwd=2)
hist(residuals(slm.d_vs_t))
plot(fitted(slm.d_vs_t),residuals(slm.d_vs_t))
nq <- quantile(dnorm(seq(-3.5,3.5,by=0.1),0,1),probs=seq(0,1,by=0.01))
eq <- quantile(residuals(slm.d_vs_t),probs=seq(0,1,by=0.01))
plot(nq,eq)
#7)
slm<-lm(MyTableBio$a~MyTableBio$Temperature)
summary(slm)
r <- var(residuals(slm))
f <- var(fitted(slm))
t <- var(MyTableBio$d)
(f/(r +f))
var(residuals(slm))*(150-1) + var(fitted(slm))*(150-1)