Re: [R] Matrix multiplications

2016-05-21 Thread george brida
Thank you very much Peter.

On Sat, May 21, 2016 at 9:18 PM, peter dalgaard  wrote:

>
> > On 21 May 2016, at 21:00 , george brida  wrote:
> >
> > Dear R users:
> >
> > I have written the following lines :
> >
> >> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
> >> x=matrix(x,ncol=2)
> >> a=matrix(1,nrow(x),1)
> >> X=cbind(a,x)
> >> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75,
> 15.00)
> >
> >> b=solve(t(X)%*% X)%*% t(X)%*% y
> >
> > when I wrote the following line
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> > I have obtained an error message, I don't know why namely (t(y-X %*%
> > b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> > [,1]
> > [1,] 3.620354
> >
> >
> > Can you please help me.
>
> Mistake is that (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) is NOT a
> scalar, but a 1 x 1 matrix.
>
> This works:
>
> as.vector((t(y-X %*% b)%*%(y-X %*% b))/(length(y)-ncol(X)))*solve(t(X)%*%
> X)
>
> as does recognizing the first term as a sum of squares:
>
> sum((y-X %*% b)^2)/(length(y)-ncol(X))*solve(t(X)%*% X)
>
> (And, for the Illuminati, a Kronecker product works too:
>
> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) %x% solve(t(X)%*% X)
>
> This could be useful for multivariate y.)
>
> >
> > Thank you
> >
> > George
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd@cbs.dk  Priv: pda...@gmail.com
>
>
>
>
>
>
>
>
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix multiplications

2016-05-21 Thread peter dalgaard
I don't know if there is some sort of propagation delay, but the reason has 
already been given and multiple fixes suggested.

-pd


> On 21 May 2016, at 21:26 , george brida  wrote:
> 
> Roy,
> 
> Yes, t(y-X %*% b) is the transpose of y-X %*% b. In principle the product
> of  t(y-X %*% b) *(y-X %*% b) is a scalar, I don't know why I have an error
> message after the following line:
> 
> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> 
> Thanks
> 
> 
> On Sat, May 21, 2016 at 9:10 PM, Roy Mendelssohn - NOAA Federal <
> roy.mendelss...@noaa.gov> wrote:
> 
>>> str(t(y-X %*% b))
>> num [1, 1:10] 0.595 -1.7538 -0.0498 -1.651 -0.6328 ...
>>> str((y-X %*% b))
>> num [1:10, 1] 0.595 -1.7538 -0.0498 -1.651 -0.6328 …
>> 
>> -Roy
>> 
>> 
>>> On May 21, 2016, at 12:00 PM, george brida 
>> wrote:
>>> 
>>> Dear R users:
>>> 
>>> I have written the following lines :
>>> 
 x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
 x=matrix(x,ncol=2)
 a=matrix(1,nrow(x),1)
 X=cbind(a,x)
 y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75,
>> 15.00)
>>> 
 b=solve(t(X)%*% X)%*% t(X)%*% y
>>> 
>>> when I wrote the following line
 (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
>>> I have obtained an error message, I don't know why namely (t(y-X %*%
>>> b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
 (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
>>>[,1]
>>> [1,] 3.620354
>>> 
>>> 
>>> Can you please help me.
>>> 
>>> Thank you
>>> 
>>> George
>>> 
>>>  [[alternative HTML version deleted]]
>>> 
>>> __
>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> 
>> **
>> "The contents of this message do not reflect any position of the U.S.
>> Government or NOAA."
>> **
>> Roy Mendelssohn
>> Supervisory Operations Research Analyst
>> NOAA/NMFS
>> Environmental Research Division
>> Southwest Fisheries Science Center
>> ***Note new address and phone***
>> 110 Shaffer Road
>> Santa Cruz, CA 95060
>> Phone: (831)-420-3666
>> Fax: (831) 420-3980
>> e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/
>> 
>> "Old age and treachery will overcome youth and skill."
>> "From those who have been given much, much will be expected"
>> "the arc of the moral universe is long, but it bends toward justice" -MLK
>> Jr.
>> 
>> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] Matrix multiplications

2016-05-21 Thread george brida
Roy,

Yes, t(y-X %*% b) is the transpose of y-X %*% b. In principle the product
of  t(y-X %*% b) *(y-X %*% b) is a scalar, I don't know why I have an error
message after the following line:

(t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)

Thanks


On Sat, May 21, 2016 at 9:10 PM, Roy Mendelssohn - NOAA Federal <
roy.mendelss...@noaa.gov> wrote:

> >  str(t(y-X %*% b))
>  num [1, 1:10] 0.595 -1.7538 -0.0498 -1.651 -0.6328 ...
> > str((y-X %*% b))
>  num [1:10, 1] 0.595 -1.7538 -0.0498 -1.651 -0.6328 …
>
> -Roy
>
>
> > On May 21, 2016, at 12:00 PM, george brida 
> wrote:
> >
> > Dear R users:
> >
> > I have written the following lines :
> >
> >> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
> >> x=matrix(x,ncol=2)
> >> a=matrix(1,nrow(x),1)
> >> X=cbind(a,x)
> >> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75,
> 15.00)
> >
> >> b=solve(t(X)%*% X)%*% t(X)%*% y
> >
> > when I wrote the following line
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> > I have obtained an error message, I don't know why namely (t(y-X %*%
> > b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
> >> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> > [,1]
> > [1,] 3.620354
> >
> >
> > Can you please help me.
> >
> > Thank you
> >
> > George
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> **
> "The contents of this message do not reflect any position of the U.S.
> Government or NOAA."
> **
> Roy Mendelssohn
> Supervisory Operations Research Analyst
> NOAA/NMFS
> Environmental Research Division
> Southwest Fisheries Science Center
> ***Note new address and phone***
> 110 Shaffer Road
> Santa Cruz, CA 95060
> Phone: (831)-420-3666
> Fax: (831) 420-3980
> e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/
>
> "Old age and treachery will overcome youth and skill."
> "From those who have been given much, much will be expected"
> "the arc of the moral universe is long, but it bends toward justice" -MLK
> Jr.
>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] Matrix multiplications

2016-05-21 Thread peter dalgaard

> On 21 May 2016, at 21:00 , george brida  wrote:
> 
> Dear R users:
> 
> I have written the following lines :
> 
>> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
>> x=matrix(x,ncol=2)
>> a=matrix(1,nrow(x),1)
>> X=cbind(a,x)
>> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75, 15.00)
> 
>> b=solve(t(X)%*% X)%*% t(X)%*% y
> 
> when I wrote the following line
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> I have obtained an error message, I don't know why namely (t(y-X %*%
> b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> [,1]
> [1,] 3.620354
> 
> 
> Can you please help me.

Mistake is that (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) is NOT a 
scalar, but a 1 x 1 matrix. 

This works:

as.vector((t(y-X %*% b)%*%(y-X %*% b))/(length(y)-ncol(X)))*solve(t(X)%*% X)

as does recognizing the first term as a sum of squares:

sum((y-X %*% b)^2)/(length(y)-ncol(X))*solve(t(X)%*% X)

(And, for the Illuminati, a Kronecker product works too:

(t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X))) %x% solve(t(X)%*% X)

This could be useful for multivariate y.)

> 
> Thank you
> 
> George
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Matrix multiplications

2016-05-21 Thread Roy Mendelssohn - NOAA Federal
>  str(t(y-X %*% b))
 num [1, 1:10] 0.595 -1.7538 -0.0498 -1.651 -0.6328 ...
> str((y-X %*% b))
 num [1:10, 1] 0.595 -1.7538 -0.0498 -1.651 -0.6328 …

-Roy


> On May 21, 2016, at 12:00 PM, george brida  wrote:
> 
> Dear R users:
> 
> I have written the following lines :
> 
>> x=c(10,11,12,13,14,17,15,16,10,11,41,25,26,14,12,14,15,20,14,22)
>> x=matrix(x,ncol=2)
>> a=matrix(1,nrow(x),1)
>> X=cbind(a,x)
>> y=c(12.00, 11.00, 13.00, 12.50, 14.00, 18.50, 15.00, 12.50, 13.75, 15.00)
> 
>> b=solve(t(X)%*% X)%*% t(X)%*% y
> 
> when I wrote the following line
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))*solve(t(X)%*% X)
> I have obtained an error message, I don't know why namely (t(y-X %*%
> b)%*%(y-X %*% b)/(length(y)-ncol(X))) is a scalar:
>> (t(y-X %*% b)%*%(y-X %*% b)/(length(y)-ncol(X)))
> [,1]
> [1,] 3.620354
> 
> 
> Can you please help me.
> 
> Thank you
> 
> George
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

**
"The contents of this message do not reflect any position of the U.S. 
Government or NOAA."
**
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new address and phone***
110 Shaffer Road
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: roy.mendelss...@noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.