Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Given a table I need to use apply() to find t the correlation between each one of the 8 variables in the state.x77 matrix and the Population variable. state.x77 is a built in matrix with 8 columns.
I had to first create a function called cor_var due to the instructions and then have to use apply(). So here is my input:
cor_var=function(v1,v2=state.x77[,"Income"]){cor(v1,v2)}
apply(mat,2,cor_var,v2=state.x77[,"Population"])
the v2 is the extra optional argument for apply() ... argument, so this should work but it is returning Error in cor(v1, v2) : incompatible dimensions. Any help on where I am wrong would be appreciated. I have to use cor_var and apply two functions btw, can't use lappy or mapply.
You can use :
apply(state.x77,2,function(x) cor(x, state.x77[,"Income"]))
#Population Income Illiteracy Life Exp Murder HS Grad Frost Area
# 0.2082276 1.0000000 -0.4370752 0.3402553 -0.2300776 0.6199323 0.2262822 0.3633154
We can use
apply(mat,2,cor_var,v2=state.x77[,"Population"])
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have a question regarding the correlation coefficient.
Why, if both variables are numeric, does the coefficient give me N/A? Thanks
When I test different variables in a dependent, on several occasions I get N/A as a result. This happens when I do it between a numeric dependent and independent variable.
There is likely two possible reasons
One of the variables is constant
There are NA in your data, if so:
In R there are two functions that compute the pearson correlation, let's see an example.
Data
x <- rnorm(10)
y <- x;y[1] <- NA
There is the cor function
cor(x,y)
that will result in a NA by default. But if you change the argument use
cor(x,y,use = "na.or.complete")
It will result in 1. Another way is to use the function cor.test, that by default ignores missing values.
cor.test(x,y)
But since is a test function, the output is a list object. If you only want the coefficient. you can get the value, by:
cor.test(x,y)$estimate
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to do element wise multiplication of two matrix a and b I'm getting the error saying
Error in a + b : non-conformable arrays
data <- matrix(151:162, nrow=4)
data2 <- matrix(221:235, nrow=3)
Error in a * b : non-conformable arrays
However when I'm doing actual matrix multiplication I'm getting the desired output. Can anyone suggest me how to fix that.
150:162 are 13 elements and 220:235 are 16. length(150:162) will show. These aren't 4x3 or 3x5 matrices.
Update: For elementwise operation the dimensions of the matrices need to be identical. See size(data). So these operations are not possible with 3x4 and 4x3.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to know if there is library like lpSolve for solving that kind of problems, I do not know if this library allows to include in the constraints the product of the two variables or how I can specify "the product is a maximum".
Defining a projection onto the feasible region allows us to use unconstrained optimization to optimize this:
tot <- 10
proj <- function(x) tot * x / sum(x)
res <- optim(1:2, function(x) -prod(proj(x)))
res$convergence
## [1] 0
-res$value
## [1] 25
proj(res$par)
## [1] 4.999799 5.000201
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
does anyone of you knows a way to use R so to calculate and display the characteristic polynomial of the eigenvalues of a generic matrix composed by chr elements?
say i.e.
m <- matrix(c('a','b','c','d','e','f','g','h','i','l','m','n'),4,4)
Please consider that I have to apply this method to a very large matrix
Thank you in advance
You can do this with the Ryacas package, but you'll have to jump through the necessary hoops to install Yacas on your system first.
library("Ryacas")
m <- matrix(letters[1:16],4,byrow=TRUE)
yrow <- function(x) paste0("{",paste(x,collapse=","),"}")
yrow(m[1,]) ## "{a,b,c,d}"
ymat <- function(x) yrow(apply(x,1,yrow))
cheqstr <- function(x) {
paste0("Expand(CharacteristicEquation(",
ymat(x),",x),x)")
}
yacas(cheqstr(m))
## (a-x)*(f-x)*(k-x)*(p-x)-(a-x)*(f-x)*l*o+(a-x)*h*j*o-d*e*j*o-
## (a-x)*g*j*(p-x)+(a-x)*g*l*n-(a-x)*h*(k-x)*n+d*e*(k-x)*n+c*e*j*(p-x)-
## c*e*l*n+c*h*i*n-d*g*i*n-b*e*(k-x)*(p-x)+b*e*l*o-b*h*i*o+d*(f-x)*i*o+
## b*g*i*(p-x)-b*g*l*m+b*h*(k-x)*m-d*(f-x)*(k-x)*m-c*(f-x)*i*(p-x)+
## c*(f-x)*l*m-c*h*j*m+d*g*j*m;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Does anybody know the examples on how to run paired ttest in Matlab/R/SAS or Python/Java on many columns (I have 1139 variables) in all combinations or selected respective columns in a loop.
thank you
MATLAB Solution:
If I understand correctly, you're just looking for a way to feed ttest with two different columns from your input matrix everytime. You can get all possible combinations of column pairs using nchoosek:
pairs = nchoosek(1:size(X, 2), 2);
Now you can iterate over these indices, each time invoking ttest with a different pair:
for idx = transpose(pairs)
h = ttest(X(:, idx(1)), X(:, idx(2)));
%// Do something with the result...
end