Math behind normalization of features (fpfh)? - point-cloud-library

I am trying to get behind the math of how the features are normalized in fpfh.hpp.
nr_bins_f1 * ((pfh_tuple[0] + M_PI) * d_pi_))
nr_bins_f2 * ((pfh_tuple[1] + 1.0) * 0.5))
nr_bins_f3 * ((pfh_tuple[2] + 1.0) * 0.5))
Why is (pfh_tuple[]+M_PI) * d_pi_ or ...+1.0)*0.5?

Related

Compute sequence of summed fractions in R

I want to compute the following sequence using R, without loops, i.e. for cycles.
1 + (2/3) + ((2/3)*(4/5)) + ((2/3)*(4/5)*(6/7)) + ... + ((2/3)*(4/5)*...(20/21))
So far, I tried different approaches with a sequence as well as a while function, but could not came up with a suitable solution. Help would be highly appreciated.
We may use cumprod
v1 <- seq(2, 20, by = 2)
v2 <- seq(3, 21, by = 2)
1 + sum(cumprod(v1/v2))
[1] 4.945724
-manual calculation
1 + (2/3) + ((2/3)*(4/5)) + ((2/3)*(4/5)*(6/7)) + ((2/3)*(4/5)*(6/7) * (8/9)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17) * (18/19)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17) * (18/19) * (20/21))
[1] 4.945724

Truncated warning or error massages

When I try to build a model using PKSFC, I get this message:
[... truncated]
R lists three mistakes and a half (the equations end with *_EC).
I guess I can correct the mistake, but I would like to correct them all in one pass.
I guess there are many more (maybe 15?).
The full message:
5: In sfc.check(vn_mod, fill = FALSE) : Equations
57192.4187750356 + POPAGE_1 * (0.5 * (0 - 55335.9694996733)/POPAGE_1 + 0.2 * (POPAGE - POPAGE_1)/POPAGE_1 - 0.2 * (57192.4187750356 - 0.5 * 55335.9694996733 - 0.508679781654891 * POPAGE_1)/POPAGE_1 + ACPOP_EC)
28.7269160437129 * exp(0.548725 * log(0/1.489181) + 0.704327 * log(0/37.4835480991331) - 0.247704 *
(log(28.7269160437129/37.4835480991331 * (1 + R_SCP_1)) - 0.5 *
log(1.489181) - (1 - 0.5) * log(1.52438302758911)) - 0.5 * 0 -
0.0511046682618056 + WP_EC)
1.25809781599844 * exp(0.2 * log(0/1.46079630578026) + 0.8 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.25809781599844) - 0.2 * log(1.46079630578026) - (1 - 0.2) * log(PPX_1 * 1.1362002193817)) -
0.00325177611673638 * (T - 2016) * (T <= 2016) - 0.0576138445722962 + PM_EC)
1.30468806319261 * exp(0.7 * log(0/1.46079630578026) + 0.3 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.30468806319261) - 0.7 * log(1.46079630578026) - (1 - 0.7) * log(PPX_1 * 1.1362002193817)) -
0.0033333860060119 * (T - 201 [... truncated]

Fast Quaternion-Vector Multiplication with other Transformation Matrices

I am currently trying to find an equivalent for the following equation:
vec_res = inverse(VM) * (q * (VM * vec_input) * conjugate(q))
where VM is a standard view matrix , q is a normalized quaternion, and vec_input a vector.
In the form:
vec_res = A * vec_input;
or
vec_res = q' * vec_input * conjugate(q');
From https://molecularmusings.wordpress.com/2013/05/24/a-faster-quaternion-vector-multiplication/ I am already calculating
vec_res = inverse(VM) * q * VM * vec_input * conjugate(q)
as
pN = (VM * vec_input);
vec3 tempVec = 2.0 * cross(q.xyz, pN);
pN = pN + q.w * tempVec + cross(q.xyz, tempVec);
pN = inverse(VM) * pN;
My question is, do I have the right to rewrite the equation like this ?
vec_res = (inverse(VM) * conversion_to_matrix(q) * VM) * vec_input
Where conversion_to_matrix is the rotation matrix calculated as explained in: http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/
If no, what is the math to be used behind ?
Note that multiplying your vector with
inverse(VM) * conversion_to_matrix(q) * VM
is not the same as multiplying it with
conversion_to_matrix(q)
since matrix multiplication is not commutative. So you really have to compute the entire matrix given in the first formula above.
vec_res = (inverse(VM) * conversion_to_matrix(q) * VM) * vec_input
Is perfectly valid.
The problem is...
inverse(VM) * conversion_to_matrix(q) * VM
is NOT equal to
conversion_to_matrix(q)
Therefore you have to keep the original equation in its entirety.

R erasing objects without command line for this [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a this problem with my code: when I run all the script I get the error Error: object 'i' not found and Error: object 'scp.L50' not found. The data frame and de counter i are erased without order, and after that, (obviously) R cannot find scp.L50 and add information on it. But, I don't know why, when I run just a part of the code (without the #bla bla bla) it works ok.
A few days ago all code was working fine, but now I have this issue. I've tried to update R (smooth sidewalk to full of ingredients) but still not working. Can someone help me?
Thanks!
Here is the code:
library("minpack.lm", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
library("gdata", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
sex<-c("FEMALE","MALE","ALL")
files <- list.files(pattern = "txt")
scp.L50<-data.frame()
for (i in 1:length(files)){
dados<-read.table(files[i],header=TRUE,fill = TRUE)
dados<-na.exclude(dados)
dados<-dados[order(dados[,1]),]
rownames(dados)<-1:nrow(dados)
species<-gsub(".txt", "", files[i]);print(paste("Species: ",gsub("_", " ", species)))
if (ncol(dados)==3){
dadosF<-dados[(which(dados[,3]=="F",arr.ind=TRUE)),]; dadosM<-dados[(which(dados[,3]=="M",arr.ind=TRUE)),]; dados.total<-dados
if (length(dadosF[,1])!=0){
dadosF<-dadosF[order(dadosF[,1]),]
} else {
rm(dadosF)}
if (length(dadosM[,1])!=0){
dadosM<-dadosM[order(dadosM[,1]),]
} else {
rm(dadosM)}}
if(ncol(dados)==3){
for (gender in sex){
try({
if (gender=="FEMALE") {
dados<-dadosF
}},silent=TRUE)
try({
if (gender=="MALE"){
dados<-dadosM
}},silent=TRUE)
try({
if (gender=="ALL"){
dados<-dados.total
}},silent=TRUE)
dados<-dados[,-3]
par(mfrow=c(1,1))
if (dados[1,1]==0) {
dados<-dados[-1,]}
unifitted<-nlsLM(Weight~a*Length^b,
data=dados,
start=list(a=0.1,b=3),
weights=wfct(fitted),
control=nls.lm.control(maxiter=1024))
coeff.uni<-as.data.frame(coef(summary(unifitted))[,1:2])
dados<-cbind(dados,resid(unifitted)/dados[,2],predict(unifitted))
sd.dados<-sd(dados[,3])
rownames(dados)<-1:nrow(dados)
sd.dados<-sd(dados[,3])
for (j in nrow(dados):1){
if (dados[j,3]/sd.dados > 2.5){dados<-dados[-j,]
}else{
if (dados[j,3]/sd.dados < -2.5){dados<-dados[-j,]}
}}
interval.cons(25)
unifitted.aver<-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1])
unifitted.aver.resid<-(dataLC[,2]-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1]))/dataLC$AvWeight
dataLC<-cbind(dataLC,unifitted.aver.resid,unifitted.aver)
rownames(dados)<-1:nrow(dados)
dados.1.3<-dados[1:(round(nrow(dados)/3)),]
dados.3.3<-dados[(nrow(dados)-(round(nrow(dados)/3))):nrow(dados),]
assign("best.loop",.GlobalEnv)
sum.resid<-999999999999999999999999999999
polyfitted.1.3<-nlsLM(Weight ~ a * Length^b,
data=dados.1.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.1.3<-as.vector(coef(polyfitted.1.3))
polyfitted.3.3<-nlsLM(Weight ~ a * Length^b,
data=dados.3.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.3.3<-as.vector(coef(polyfitted.3.3))
scp.matrix<-seq(from=dados[30,1], to=dados[(nrow(dados)-30),1], by=1)
r.squared.test<-2
for (scp.loop in scp.matrix){
for (fit.rate in 1:10){
try({
polyfitted.total<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - scp.loop)))) +
(a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - scp.loop))))),
data=dados,
start=list(a1=coeff.poly.1.3[[1]],b1=coeff.poly.1.3[[2]],a2=coeff.poly.3.3[[1]],b2=coeff.poly.3.3[[2]]),
lower=c(a1=coeff.poly.1.3[[1]]-coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]-coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]-coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]-coeff.poly.3.3[[2]]*0.7),
upper=c(a1=coeff.poly.1.3[[1]]+coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]+coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]+coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]+coeff.poly.3.3[[2]]*0.7),
control=nls.lm.control(maxiter=1024))
coeff.poly<-as.vector(coef(polyfitted.total))
polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))) +
(coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))))))/dataLC$AvWeight
polyfitted.aver<-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))) +
((coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))))
polyfitted.resid<-(dados[,2]-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))) +
(coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))))))/dados[,2]
polyfitted<-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))) +
((coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))))
dados<-dados[,-5:-6]
dataLC<-dataLC[,-5:-6]
dados<<-cbind(dados,polyfitted.resid,polyfitted)
dataLC<<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)
phases<-apply(dados,2,function(x) length(x[x<=scp.loop]));phases<-phases[-2:-6]
polyfitted.phase1.res<-dados[1:phases,5]
polyfitted.phase2.res<-dados[(phases+1):nrow(dados),5]
polyfitted.phase1.lr<<-lm(polyfitted.phase1.res~dados[1:phases,1])
polyfitted.phase2.lr<<-lm(polyfitted.phase2.res~dados[(phases+1):nrow(dados),1])
r.squared.lr<-(summary(polyfitted.phase1.lr)$r.squared)+(summary(polyfitted.phase2.lr)$r.squared)
if(sum(resid(polyfitted.total)^2)<sum.resid){
if(r.squared.test>r.squared.lr){
print(">> Best combination found <<")
best.loop<<-polyfitted.total
sum.resid<-sum(resid(polyfitted.total)^2)
rate<<-fit.rate
scp<<-scp.loop
r.squared.test<-r.squared.lr
coeff.poly<<-as.vector(coef(polyfitted.total))
}}else{coeff.poly.only.res<<-coef(polyfitted.total)}
},silent=FALSE)}}
sd.dados<-sd(dados[,5])
rownames(dados)<-1:nrow(dados)
for (j in nrow(dados):1){
if (dados[j,5]/sd.dados > 1.96){dados<-dados[-j,]
}else{
if (dados[j,5]/sd.dados < -1.96){dados<-dados[-j,]}
}}
rownames(dados)<-1:nrow(dados)
rm(j,sd.dados)
keep(dados,dadosF,dadosM,dados.total,interval.cons,files,sex,gender,sure=T)
if (nrow(dados)<150){next}
cut.point<-round((nrow(dados)*0.025),digits=0)
dados<-dados[-1:-cut.point,]
dados<-dados[(nrow(dados)-cut.point:nrow(dados)),]
dados<-dados[order(dados[,1]),]
rownames(dados)<-1:nrow(dados)
print(paste("Calculating ", gender, " data: ",length(dados[,1])," points"))
interval.cons(25)
dados<-dados[,-3:-6]
unifitted<-nlsLM(Weight~a*Length^b,
data=dados,
start=list(a=0.1,b=3),
weights=wfct(fitted),
control=nls.lm.control(maxiter=1024))
coeff.uni<-as.data.frame(coef(summary(unifitted))[,1:2])
unifitted.aver.resid<-(dataLC[,2]-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1]))/dataLC$AvWeight
unifitted.aver<-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1])
dados<-cbind(dados,resid(unifitted)/dados[,2],predict(unifitted))
dataLC<-cbind(dataLC,unifitted.aver.resid,unifitted.aver)
Linf<-exp(0.044 + 0.9841*log(max(dados[,1])))
if (gender=="FEMALE"){
L50<-exp(0.9469*log(Linf) - 0.1162)
}
if (gender=="MALE"){
L50<-exp(0.8915*log(Linf) - 0.1032)
}
if(gender=="ALL"){
L50<-exp(0.8979*log(Linf) - 0.0782)
}
dados.1.3<-dados[1:(round(nrow(dados)/3)),]
dados.3.3<-dados[(nrow(dados)-(round(nrow(dados)/3))):nrow(dados),]
assign("best.loop",.GlobalEnv)
sum.resid<-999999999999999999999999999999
polyfitted.1.3<-nlsLM(Weight ~ a * Length^b,
data=dados.1.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.1.3<-as.vector(coef(polyfitted.1.3))
polyfitted.3.3<-nlsLM(Weight ~ a * Length^b,
data=dados.3.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.3.3<-as.vector(coef(polyfitted.3.3))
scp.matrix<-seq(from=dados[30,1], to=dados[(nrow(dados)-30),1], by=1)
r.squared.test<-2
for (scp.loop in scp.matrix){
for (fit.rate in 1:10){
try({
polyfitted.total<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - scp.loop)))) +
(a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - scp.loop))))),
data=dados,
start=list(a1=coeff.poly.1.3[[1]],b1=coeff.poly.1.3[[2]],a2=coeff.poly.3.3[[1]],b2=coeff.poly.3.3[[2]]),
lower=c(a1=coeff.poly.1.3[[1]]-coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]-coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]-coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]-coeff.poly.3.3[[2]]*0.7),
upper=c(a1=coeff.poly.1.3[[1]]+coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]+coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]+coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]+coeff.poly.3.3[[2]]*0.7),
control=nls.lm.control(maxiter=1024))
coeff.poly<-as.vector(coef(polyfitted.total))
polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))) +
(coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))))))/dataLC$AvWeight
polyfitted.aver<-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))) +
((coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))))
polyfitted.resid<-(dados[,2]-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))) +
(coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))))))/dados[,2]
polyfitted<-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))) +
((coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))))
dados<-dados[,-5:-6]
dataLC<-dataLC[,-5:-6]
dados<<-cbind(dados,polyfitted.resid,polyfitted)
dataLC<<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)
phases<-apply(dados,2,function(x) length(x[x<=scp.loop]));phases<-phases[-2:-6]
polyfitted.phase1.res<-dados[1:phases,5]
polyfitted.phase2.res<-dados[(phases+1):nrow(dados),5]
polyfitted.phase1.lr<<-lm(polyfitted.phase1.res~dados[1:phases,1])
polyfitted.phase2.lr<<-lm(polyfitted.phase2.res~dados[(phases+1):nrow(dados),1])
r.squared.lr<-(summary(polyfitted.phase1.lr)$r.squared)+(summary(polyfitted.phase2.lr)$r.squared)
if(sum(resid(polyfitted.total)^2)<sum.resid){
if(r.squared.test>r.squared.lr){
print(">> Best combination found <<")
best.loop<<-polyfitted.total
sum.resid<-sum(resid(polyfitted.total)^2)
rate<<-fit.rate
scp<<-scp.loop
r.squared.test<-r.squared.lr
coeff.poly<<-as.vector(coef(polyfitted.total))
}}else{coeff.poly.only.res<<-coef(polyfitted.total)}
},silent=FALSE)}}
coeff.poly<-as.vector(coef(best.loop))
polyfitted.total.rate.scp<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - fit.scp)))) +
(a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - fit.scp))))),
data=dados,
start=list(a1=coeff.poly[[1]],b1=coeff.poly[[2]],a2=coeff.poly[[3]],b2=coeff.poly[[4]],fit.rate=rate,fit.scp=scp),
lower=c(a1=coeff.poly[[1]]-coeff.poly[[1]]*0.5,b1=coeff.poly[[2]]-coeff.poly[[2]]*0.5,a2=coeff.poly[[3]]-coeff.poly[[3]]*0.5,b2=coeff.poly[[4]]-coeff.poly[[4]]*0.5,fit.rate=rate-(rate*0.2),fit.scp=(scp-(scp*0.2))),
upper=c(a1=coeff.poly[[1]]+coeff.poly[[1]]*0.5,b1=coeff.poly[[2]]+coeff.poly[[2]]*0.5,a2=coeff.poly[[3]]+coeff.poly[[3]]*0.5,b2=coeff.poly[[4]]+coeff.poly[[4]]*0.5,fit.rate=rate+(rate*0.2),fit.scp=(scp+(scp*0.2))),
weights=wfct(fitted),
control=nls.lm.control(maxiter=1024))
coeff.poly.rate.scp<-as.vector(coef(polyfitted.total.rate.scp))
polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly.rate.scp[[3]] * dataLC$LengthClass^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]])))) +
(coeff.poly.rate.scp[[1]] * dataLC$LengthClass^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]])))))))/dataLC$AvWeight
polyfitted.aver<-((coeff.poly.rate.scp[[3]] * dataLC$LengthClass^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]]))))) +
((coeff.poly.rate.scp[[1]] * dataLC$LengthClass^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]]))))))
polyfitted.resid<-(dados[,2]-((coeff.poly.rate.scp[[3]] * dados[,1]^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]])))) +
(coeff.poly.rate.scp[[1]] * dados[,1]^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]])))))))/dados[,2]
polyfitted<-((coeff.poly.rate.scp[[3]] * dados[,1]^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]]))))) +
((coeff.poly.rate.scp[[1]] * dados[,1]^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]]))))))
dados<-dados[,-5:-6]
dataLC<-dataLC[,-5:-6]
dados<-cbind(dados,polyfitted.resid,polyfitted)
dataLC<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)
coeff.poly<-as.data.frame(coef(summary(polyfitted.total.rate.scp))[,1:2])
phases<-apply(dataLC,2,function(x) length(x[x<=coeff.poly[6,1]]));phases<-phases[-2:-6]
unifitted.phase1.res<-dataLC[,3]; unifitted.phase1.res<-unifitted.phase1.res[1:phases];
unifitted.phase2.res<-dataLC[,3]; unifitted.phase2.res<-unifitted.phase2.res[(phases+1):nrow(dataLC)]
unifitted.phase1.lr<-lm(unifitted.phase1.res~dataLC[1:phases,1]); unifitted.phase2.lr<-lm(unifitted.phase2.res~dataLC[(phases+1):nrow(dataLC),1])
polyfitted.phase1.res<-dataLC[1:phases,5]
polyfitted.phase2.res<-dataLC[(phases+1):nrow(dataLC),5]
polyfitted.phase1.lr<-lm(polyfitted.phase1.res~dataLC[1:phases,1])
polyfitted.phase2.lr<-lm(polyfitted.phase2.res~dataLC[(phases+1):nrow(dataLC),1])
graph.name<-paste(files[i],gender,sep=" ");graph.name<-gsub(".txt", "", graph.name);graph.name<-gsub("_", " ", graph.name)
a.values<-paste("a = ",round(coeff.uni[1,1],digits=4)," (se: ",round(coeff.uni[1,2],digits=4),")",sep="")
b.values<-paste("b = ",round(coeff.uni[2,1],digits=4)," (se: ",round(coeff.uni[2,2],digits=4),")",sep="")
a1.values<-paste("a = ",round(coeff.poly[1,1],digits=4)," (se: ",round(coeff.poly[3,2],digits=4),")",sep="")
b1.values<-paste("b = ",round(coeff.poly[2,1],digits=4)," (se: ",round(coeff.poly[4,2],digits=4),")",sep="")
a2.values<-paste("a = ",round(coeff.poly[3,1],digits=4)," (se: ",round(coeff.poly[1,2],digits=4),")",sep="")
b2.values<-paste("b = ",round(coeff.poly[4,1],digits=4)," (se: ",round(coeff.poly[2,2],digits=4),")",sep="")
scp.values<-paste("SCP = ",round(coeff.poly[6,1],digits=2)," (se: ",round(coeff.poly[6,2],digits=2),")",sep="")
rate.values<-paste("rate = ",round(coeff.poly[5,1],digits=3)," (se: ",round(coeff.poly[5,2],digits=3),")",sep="")
L50.values<-paste("L = ",round(L50,digits=2),sep="")
par(mfrow=c(2,2))
plot(dados[,1],dados[,3],pch=21,col="gray65",bg="white",xlab="Length (cm)",ylab="Residuals",main="Huxley")
points(dataLC[,1],dataLC[,3],pch=21,col="black",bg="black")
try({
lines(dataLC[1:phases,1],predict(unifitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(unifitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Huxley",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,4],lwd=3)
mtext(a.values,line=-1.5,adj=0.05,cex=.6)
mtext(b.values,line=-2.4,adj=0.05,cex=.6)
plot(dados[,1],dados[,5],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Residuals")
points(dataLC[,1],dataLC[,5],pch=21,col="black",bg="black")
abline(v=coeff.poly[6,1],lty=2:2)
try({
lines(dataLC[1:phases,1],predict(polyfitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(polyfitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
abline(v=L50,lty=3)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,6],lwd=3)
abline(v=coeff.poly[6,1],lty=2:2)
abline(v=L50,lty=3)
mtext(a1.values,line=-1.5,adj=0.05,cex=.6)
mtext("1",line=-1.6,adj=0.05,cex=.35)
mtext(b1.values,line=-2.4,adj=0.05,cex=.6)
mtext("1",line=-2.5,adj=0.05,cex=.35)
mtext(a2.values,line=-3.3,adj=0.05,cex=.6)
mtext("2",line=-3.4,adj=0.05,cex=.35)
mtext(b2.values,line=-4.2,adj=0.05,cex=.6)
mtext("2",line=-4.3,adj=0.05,cex=.35)
mtext(scp.values,line=-5.1,adj=0.05,cex=.6)
mtext(rate.values,line=-6,adj=0.05,cex=.6)
mtext(L50.values,line=-6.9,adj=0.04,cex=.6)
mtext("50",line=-7,adj=0.06,cex=.35)
par(mfrow=c(2,2),oma = c(0, 0, 1.5, 0))
mtext(graph.name, outer = TRUE, cex = 1.5)
mypath <- file.path("~/Documents/Doutorado/Dados R/Gráficos",paste(graph.name, ".pdf", sep = ""))
mypath.rdata <- file.path("~/Documents/Doutorado/Dados R/Resultados novos",paste(graph.name, ".Rdata", sep = ""))
save(list=ls(all=TRUE),file=mypath.rdata)
pdf(file=mypath)
par(mfrow=c(2,2))
plot(dados[,1],dados[,3],pch=21,col="gray65",bg="white",xlab="Length (cm)",ylab="Residuals",main="Huxley")
points(dataLC[,1],dataLC[,3],pch=21,col="black",bg="black")
try({
lines(dataLC[1:phases,1],predict(unifitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(unifitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Huxley",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,4],lwd=3)
mtext(a.values,line=-1.5,adj=0.05,cex=.6)
mtext(b.values,line=-2.4,adj=0.05,cex=.6)
plot(dados[,1],dados[,5],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Residuals")
points(dataLC[,1],dataLC[,5],pch=21,col="black",bg="black")
abline(v=coeff.poly[6,1],lty=2:2)
try({
lines(dataLC[1:phases,1],predict(polyfitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(polyfitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
abline(v=L50,lty=3)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,6],lwd=3)
abline(v=coeff.poly[6,1],lty=2:2)
abline(v=L50,lty=3)
mtext(a1.values,line=-1.5,adj=0.05,cex=.6)
mtext("1",line=-1.6,adj=0.05,cex=.35)
mtext(b1.values,line=-2.4,adj=0.05,cex=.6)
mtext("1",line=-2.5,adj=0.05,cex=.35)
mtext(a2.values,line=-3.3,adj=0.05,cex=.6)
mtext("2",line=-3.4,adj=0.05,cex=.35)
mtext(b2.values,line=-4.2,adj=0.05,cex=.6)
mtext("2",line=-4.3,adj=0.05,cex=.35)
mtext(scp.values,line=-5.1,adj=0.05,cex=.6)
mtext(rate.values,line=-6,adj=0.05,cex=.6)
mtext(L50.values,line=-6.9,adj=0.04,cex=.6)
mtext("50",line=-7,adj=0.05,cex=.35)
par(mfrow=c(2,2),oma = c(0, 0, 1.5, 0))
mtext(graph.name, outer = TRUE, cex = 1.5)
dev.off()
scp.L50.paste<-cbind(species,gender,round(coeff.poly[6,1],digits=1),round(L50,digits=1),round(AIC(unifitted),digits=2),round(AIC(polyfitted.total.rate.scp),digits=2))
scp.L50<-rbind(scp.L50,scp.L50.paste)
}}
names(scp.L50)<-c("Species","Gender","SCP","L50","AIC Huxley","AIC Polyphasic")
write.csv(scp.L50,file="Summary.csv")
I went through your code and there is indeed a missing bracket. Assuming that the indentation of your code is correct and that you only want to write the summary one time, this is where I think the problem is. Note that I already added the closing bracket on the line where I put my comment:
for (i in 1:length(files)){ # !! I believe you forgot to close this bracket !!
# Some code here..
species<-gsub(".txt", "", files[i]);print(paste("Species: ",gsub("_", " ", species)))
if (ncol(dados)==3){
# Some more code here..
}
if(ncol(dados)==3){
for (gender in sex){
# Wall of code
scp.L50.paste<-cbind(species,gender,round(coeff.poly[6,1],digits=1),round(L50,digits=1),round(AIC(unifitted),digits=2),round(AIC(polyfitted.total.rate.scp),digits=2))
scp.L50<-rbind(scp.L50,scp.L50.paste)
}}
} # !! I believe your closing bracket should be added here !!
names(scp.L50)<-c("Species","Gender","SCP","L50","AIC Huxley","AIC Polyphasic")
write.csv(scp.L50,file="Summary.csv")
EDIT: In other words, it is most likely the most outer for loop that is not closed properly.
The code has 43 opening brackets and 42 closing brackets, so if you're executing it as a script, you can't expect that to run correctly. I don't know for sure where your missing bracket should be, because it depends on what you want to do, but you should take a closer look and attempt to find it. Take a look at your recent changes, have you forgotten to close a loop or something similar?

Are my equations correct? Rotate on sphere from lat/long points A to B, where will point C be?

I’ve written the below python script. The idea is to calculate the new location of point C after you rotate the globe from point A to point B. I first calculate point P, which is the rotation pole. With calculating point P already something goes wrong. With the following input f.e. I would assume point P to be having latitude 90 or –90.
I asked this question before here: Rotate a sphere from coord1 to coord2, where will coord3 be?
But I figured it's better to ask again with the script included ;)
# GreatCircle can be downloaded from: http://www.koders.com/python/fid0A930D7924AE856342437CA1F5A9A3EC0CAEACE2.aspx?s=coastline
from GreatCircle import *
from math import *
# Points A and B defining the rotation:
LonA = radians(0)
LatA = radians(1)
LonB = radians(45)
LatB = radians(1)
# Point C which will be translated:
LonC = radians(90)
LatC = radians(1)
# The following equation is described here: http://articles.adsabs.harvard.edu//full/1953Metic...1...39L/0000040.000.html
# It calculates the rotation pole at point P of the Great Circle defined by point A and B.
# According to http://www.tutorialspoint.com/python/number_atan2.htm
# atan2(x, y) = atan(y / x)
LonP = atan2(((sin(LonB) * tan(LatA)) - (sin(LonA) * tan(LatB))), ((cos(LonA) * tan(LatB)) - (cos(LonB) * tan(LatA))))
LatP = atan2(-tan(LatA),(cos(LonP - LonA)))
print degrees(LonP), degrees(LatP)
# The equations to calculate the translated point C location were found here: http://www.uwgb.edu/dutchs/mathalgo/sphere0.htm
# The Rotation Angle in radians:
gcAP = GreatCircle(1,1,degrees(LonA),degrees(LatA),degrees(LonP),degrees(LatP))
gcBP = GreatCircle(1,1,degrees(LonB),degrees(LatB),degrees(LonP),degrees(LatP))
RotAngle = abs(gcAP.azimuth12 - gcBP.azimuth12)
# The rotation pole P in Cartesian coordinates:
Px = cos(LatP) * cos(LonP)
Py = cos(LatP) * sin(LonP)
Pz = sin(LatP)
# Point C in Cartesian coordinates:
Cx = cos(radians(LatC)) * cos(radians(LonC))
Cy = cos(radians(LatC)) * sin(radians(LonC))
Cz = sin(radians(LatC))
# The translated point P in Cartesian coordinates:
NewCx = (Cx * cos(RotAngle)) + (1 - cos(RotAngle)) * (Px * Px * Cx + Px * Py * Cy + Px * Pz * Cz) + (Py * Cz - Pz * Cy) * sin(RotAngle)
NewCy = (Cy * cos(RotAngle)) + (1 - cos(RotAngle)) * (Py * Px * Cx + Py * Py * Cy + Py * Pz * Cz) + (Pz * Cx - Px * Cz) * sin(RotAngle)
NewCz = (Cz * cos(RotAngle)) + (1 - cos(RotAngle)) * (Pz * Px * Cx + Pz * Py * Cy + Pz * Pz * Cz) + (Px * Cy - Py * Cx) * sin(RotAngle)
# The following equation I got from http://rbrundritt.wordpress.com/2008/10/14/conversion-between-spherical-and-cartesian-coordinates-systems/
# The translated point P in lat/long:
Cr = sqrt((NewCx*NewCx) + (NewCy*NewCy) + (NewCz*NewCz))
NewCLat = degrees(asin(NewCz/Cr))
NewCLon = degrees(atan2(NewCy, NewCx))
# Output:
print str(NewCLon) + "," + str(NewCLat)

Resources