So I'm trying to do a recursive calculation with time-steps h, and the time is t.
I want the second if-function (in the while-loop) to check if the time t is an integer. It works for the first loop when t=9, but after that it ignores when t=8,7,6,... and so on. And right now I just don't understand why.
I would be very grateful for any help or ideas!
h=1/12;
b1=10000;
b2=25000*0.15*12; #45000
mu_10=0.004183;
mu_12=0.002136;
mu_20=0.0050196;
mu_21=0.005;
V1_start=h*(-b1);
V2_start=h*(b2);
t_vektor<-c(10);
V1_vektor<-c(V1_start);
V2_vektor<-c(V2_start);
t=as.integer(9);
while (t>0){
if(t==1){
V1_ny=V1_start+h*(-log(1.04)*V1_start+b1-mu_10*V1_start+mu_12*(V2_start-V1_start));
}else{
V1_ny=V1_start+h*(-log(1.04)*V1_start-mu_10*V1_start+mu_12*(V2_start-V1_start));
}
V2_ny=V2_start+h*(-log(1.04)*V2_start+b2-mu_20*V2_start+mu_21*(V1_start-V2_start));
if(round(t)==t){
V1_vektor<-c(V1_vektor,V1_ny);
V2_vektor<-c(V2_vektor,V2_ny);
t_vektor<-c(t_vektor,t);
V2_start=V2_ny;
V1_start=V1_ny;
t=t-h;
}else{
V2_start=V2_ny;
V1_start=V1_ny;
t=t-h;
print(t)
}
}
This has to do with the way numbers are stored, see also here.
An example for your case, see the output of the following code:
t = 9
h=1/12
for(i in 1:12)
{
t=t-h
}
print(t) # 8
print(t==8) # FALSE
all.equal(t,8) # TRUE
in your case, try:
isTRUE(all.equal(round(t),t))
Hope this helps!
Instead of if(round(t)==t) use:
tolerance = h/2;
if(min(abs(c(t%%1, t%%1-1))) < tolerance){
...
}
You make your test on t, but you change t before printing it by the line "t=t-h";
So you don't see the value witch was tested ..
Related
Write a function named countConsecutivePairs that accepts a numeric vector as an argument and counts the pairs of consecutive elements whose difference is less than 2.
You should stop the execution and print an appropriate error message if the argument is not numeric.
Here is my code
countConsecutivepairs=function(z){
if(!(is.vector(z)))
stop("Error"
if(class(z)!="numeric")
stop("Error")
for(i in z) {
if(abs(z[i]-z[i+1])!=2){
next
}
print(c(z[i],z[i+1])
}
}
I get a bunch of errors when I do this. And Im not sure how to fix it.
You miss several ) in your code.
And thanks to #Jan, as #Ace123 use z[i]'s in code and also want to check consecutive elements, we should call those elements by i against length of z.
countConsecutivepairs<- function(z){
if(!(is.vector(z))){
stop("Error")
}
if(class(z)!="numeric"){
stop("Error")
}
for(i in 1:(length(z)-1)) {
if(abs(z[i]-z[i+1])!=2){
next
}
print(c(z[i],z[i+1]))
}
}
countConsecutivepairs(c(1,2,3,5,7))
[1] 3 5
[1] 5 7
This is my code I wish to have:
a=1
b=c(2,1.5,0.7)
if (a==1 & b<1) {
b=1 # but here's the problem, that only the first value of the vector b is considered
} # end if loop
print(b)
Okay, I could also write this code like this but I hope I can prevent it with your help.
a=1
b=c(2,1.5,0.7)
if (a==1) {
for (i in 1:length(b)) {
if (b[i]<1) {
b[i]=1
} # end if loop
} # end for loop
} # end if loop
print(b)
I also have found this question Vectorized IF statement in R? but I cannot transfer it to my issue...
Thanks for your help in advance.
b <- ifelse(a==1 & b<1, 1, b)
I am trying to compare two number in my code, when it comes to compare one digit number , it works fine whether I use == or all.equall function, but when it comes to comparing 2 digit number or more like 17, it can't say they are the same, I have already go through this thread and all.equall is not working as well. beside my numbers are all integers. can any one tell me what the problem is here ?
I'll put the code here so the problem can be reproducible.
library(igraph)
node1<- c(1,1,1,2,2,2,3,3,4,4,5,5,7,8,9,9,10,12,14,14,17,17,19)
node2<-c(2,3,4,5,6,17,12,14,7,8,6,13,14,9,10,11,11,13,16,15,18,19,20)
AZADEH_GRAPH.data <- data.frame(node1,node2)
dataframe_AZADEH_GRAPH<-AZADEH_GRAPH
graph_AZADEH_GRAPH=graph.data.frame(dataframe_AZADEH_GRAPH,directed=FALSE)
Nodes1_AZADEH_GRAPH<- replicate(vcount(graph_AZADEH_GRAPH), 0)
SuperEgo_AZADEH_GRAPH<- list()
Com_AZADEH_GRAPH<- list()
community_member <-matrix()
neghbor_list<-list()
count_neighbors<-list()
community_1<-list()
SuperEgo_AZADEH_GRAPH[[2]]=make_ego_graph(graph_AZADEH_GRAPH,2,
V(graph_AZADEH_GRAPH)$name[2],
mode = "all",mindist = 0)
Com_AZADEH_GRAPH[[2]] <- cluster_infomap(SuperEgo_AZADEH_GRAPH[[2]][[1]])
community_member<-data.matrix(membership(Com_AZADEH_GRAPH[[2]]))
neghbor_list[2]=ego(graph_AZADEH_GRAPH, order = 1,
nodes = V(graph_AZADEH_GRAPH)$name[2], mode = "all",mindist = 1)
count_neighbors[2]=length(neghbor_list[[2]])
for (k in 1:nrow(community_member))
{
RRR<-cbind(community_member,as.integer(rownames(community_member)[k]))
}
for (n in 1:nrow(RRR))
{
RRR[n,2]<-as.integer(rownames(RRR)[n])
}
for (i in 1: length(neghbor_list[[2]]))
{
for (j in 1:nrow(RRR))
{
if (neghbor_list[[2]][i]==RRR[[j,2]])
{
community_1[i]=RRR[[j,1]]
}
}
}
the problem is with if statements and more specifically when i=3 and j=6 neghbor_list[[2]][3],
RRR[[6,2]] both return 17 but still it gives False it is working fine when i=1 & 2
(Posted solution on behalf of the question author).
The issue is found, it was referring to the indexes, I should have use $name instead after neghbor_list[[2]][3].
I am m trying to run the following code:
data_greene<-read.delim(file.choose(),header=T)
result_b2_HC0<-matrix(1:2000,ncol=4)
for (i in 1:500){
X1<-data_greene[[3]]*10^-4
X2<-X1^2
e<-rnorm(50,0,1)
sigma2<-exp(5.30+5.30*X1)
lambda<-max(sigma2)/min(sigma2)
Y<-1+1*X1+0*X2+sqrt(sigma2)*e
lms<-lmsreg(Y~X1+X2)
yhat<-lms$fitted
resid<-lms$residual
s<-abs(resid)
lms2<-lmsreg(s~yhat)
shat<-lms2$fitted
w1<-1/shat^2
scale<-lms$scale[1]
stdres<-resid/scale
e=abs(stdres)
w2<-NULL
for (i in 1:50){
if(e[i]<=1.345) w2[i]<-1 else w2[i]<-1.345/e[i]
}
w<-w1*w2
WLS<-lm(Y~X1+X2,weights=w)
res1<-WLS$residual
HCCMEHC0<-function(Y,X1,X2){
X<-cbind(1,X1,X2)
W<-diag(w)
inv<-solve(t(X)%*%W%*%X)
psi0<-diag(res1^2)
HC0<-inv%*%t(X)%*%W%*%psi0%*%W%*%X%*%inv
return(HC0)
}
result_b2_HC0[i,1]<-WLS$coef[3]
result_b2_HC0[i,2]<-sqrt(HCCMEHC0(Y,X1,X2)[3,3])
result_b2_HC0[i,3]<-result_b2_HC0[i,1]/result_b2_HC0[i,2]
result_b2_HC0[i,4]<-2*pt(-abs(result_b2_HC0[i,3]),df=47)
}
result_b2_HC0
I would expect the matrix to be complete, but the result only appears at row 50 in the matrix. What am I doing wrong?
You are using the same variable i in two nested for loops. Change the second for loop to use the variable j instead.
To avoid this error, make sure you always use indentation. Also, learn how to use vector mathematics. Your second loop can be rewritten from
e=abs(stdres)
w2<-NULL
for (i in 1:50){
if(e[i]<=1.345) w2[i]<-1 else w2[i]<-1.345/e[i]
}
to
e=abs(stdres)
w2<-ifelse( e <= 1.345, 1, 1.345/e )
This is cleaner, easier to read, and faster.
I have the following R code:
y <-round(runif(100, min=0, max=800))
for(i in y) {
if((i+1)>i) print("bigger")
if((i+1)<i) print("smaller")
}
I want to know if the next number in the list is bigger or smaller.
It always prints bigger. I guess because I am doing it wrong.
Any help would be great..
Thanks
You can use diff for this.
yd <- diff(y)
ifelse(yd > 0, print('bigger'), print('smaller'))
The reason your for loop always prints bigger is because i is always less than i+1... look at what you're asking... you mean y[which(y==i) + 1] > i or something... If you must use a loop, you can do something like this:
for (i in seq_along(y)) {
if (y[i+1] > y[i]) {
print('bigger')
} else {
print('smaller')
}
}
But, the vectorized version using diff will be much more efficient and easier to understand in my opinion.
You can create a vector in the following way:
c("smaller", "bigger")[(diff(y) > 0) + 1]