Question

In: Computer Science

R PROGRAMMING QUESTION - Below I have code. For each double hashtag (##) can you comment...

R PROGRAMMING QUESTION

- Below I have code. For each double hashtag (##) can you comment on the code below it (Describe what is happening in the code below it next to each ##)

- Run the code and compare the confidence intervals

- I have to submit the confidence intervals, comments, and the code with the ## filled out

Leaps.then.press.plot.2<-function(xmat0,yvec,xpred,ncheck=20)

{

#

#input quadratic matrix with less than 30 columns eg. the result of x.auto2a<-matrix.2ndorder.make(xmat[,-7],F)

#also, no need for plotting, just pull out best, xpred is one of the row vectors from x.auto2a, but all terms with weight are divided by 2

#

   ##

   leaps.str<-leaps(xmat,yvec)

   ##

   z1<-leaps.str$Cp-leaps.str$size

   ##

   o1<-order(z1)

   matwhich<-(leaps.str$which[o1,])[1:ncheck,]

   MPSEvec<-NULL

   ##

   for(i in 1:ncheck){

       ls.str0<-regpluspress(xmat[,matwhich[i,]],yvec)

       ##

       parvec<-matwhich[i,]

       npar<-sum(parvec)

       ## (WHY npar+1)

       MPSE<-ls.str0$press/(length(yvec)-(npar+1))

       MPSEvec<-c(MPSEvec,MPSE)

   }

   ##

   I1<-(MPSEvec==min(MPSEvec))

   ##

   i<-c(1:ncheck)[I1]

   ##

   xmat.out<-xmat[,matwhich[i,]]

   ##

   xpred.out<-xpred[matwhich[i,]]

   ##

   list(xmatout=xmat.out,yvec=yvec,xpredout=xpred.out)

  

  

}

Bootreg<-function(xmat,yvec,xpred,nboot=10000,alpha=0.05)

{

   ##

   lstr0<-leaps.then.press.plot2(xmat,yvec,xpred)

   xmat0<-lstr0$xmat.out

   yvec0<-lstr0$yvec

   xpred0<-lsstr0$xpredout

   ##

   rprd.list<-regpred(xpred0,xmat0,yvec0)

   ypred0<-rprd.list$pred

   sdpred0<-rprd.list$sd

   df<-rprd.list$df

   ##

   bootvec<-NULL

   nobs<-length(yvec0)

   for(i in 1:nboot){

       ##

       vboot<-sample(c(1:nobs),replace=T)

       xmatb<-xmat0[vboot,]

       yvecb<-yvec0[vboot]

       ##

       lstrb<-leaps.then.press.plot2(xmatb,yvecb,xpred)

       ##

       xmatb0<-lstrb$xmat.out

       yvecb0<-lstrb$yvec

       xpredb0<-lsstrb$xpredout

       ##

       rprd.list<-regpred(xpred0,xmat0,yvec0)

       ypredb<-rprd.list$pred

       sdpredb<-rprd.list$sd

       dfb<-rprd.list$df

       ##

       bootvec<-c(bootvec,(ypredb-ypred0)/sdpredb)

}

##

lq<-quantile(bootvec,alpha/2)

uq<-quantile(bootvec,1-alpha/2)

##

LB<-ypred0-(sdpred0)*uq

UB<-ypred0-(sdpred0)*lq

##

NLB<-ypred0-(sdpred0)*qt(1-alpha/2,df0)

NUB<-ypred0+(sdpred0)*qt(1-alpha/2,df0)

list(bootstrap.confidence.interval=c(LB,UB),normal.confidence.interval=c(NLB,NUB))

}

> regpred<-

function(xpred,xmat,y){

##

ls.str<-lsfit(xmat,y)

#calculate prediction

ypred<-ls.str$coef%*%c(1,xpred)

#use ls.diag to extract covariance matrix

ycov<-ls.diag(ls.str)$cov.unscaled

#use ls.diag to extract std deviation

std.dev<-ls.diag(ls.str)$std.dev

#variance of data around line

v1<-std.dev^2

#variance of prediction

vpred<-v1*c(1,xpred)%*%ycov%*%c(1,xpred)

df=length(y)-length(diag(ycov))

list(pred=ypred,sd=sqrt(vpred),df=df)

}

Solutions

Expert Solution


leaps.str<-leaps(xmat,yvec
z1<-leaps.str$Cp-leaps.str$size
o1<-order(z1)
matwhich<-(leaps.str$which[o1,])[1:ncheck,]
MPSEvec<-NULL

for(i in 1:ncheck){
ls.str0<-regpluspress(xmat[,matwhich[i,]],yvec)
  
parvec<-matwhich[i,]
npar<-sum(parvec)
(WHY npar+1)
MPSE<-ls.str0$press/(length(yvec)-(npar+1))
MPSEvec<-c(MPSEvec,MPSE)
}

I1<-(MPSEvec==min(MPSEvec))

i<-c(1:ncheck)[I1]
xmat.out<-xmat[,matwhich[i,]]

xpred.out<-xpred[matwhich[i,]]
  
list(xmatout=xmat.out,yvec=yvec,xpredout=xpred.out)
}
Bootreg<-function(xmat,yvec,xpred,nboot=10000,alpha=0.05)
{
  
lstr0<-leaps.then.press.plot2(xmat,yvec,xpred)
xmat0<-lstr0$xmat.out
yvec0<-lstr0$yvec
xpred0<-lsstr0$xpredout

rprd.list<-regpred(xpred0,xmat0,yvec0)
ypred0<-rprd.list$pred
sdpred0<-rprd.list$sd
df<-rprd.list$df

bootvec<-NULL
nobs<-length(yvec0)
for(i in 1:nboot){
  
vboot<-sample(c(1:nobs),replace=T)
xmatb<-xmat0[vboot,]
yvecb<-yvec0[vboot]

lstrb<-leaps.then.press.plot2(xmatb,yvecb,xpred)

xmatb0<-lstrb$xmat.out
yvecb0<-lstrb$yvec
xpredb0<-lsstrb$xpredout

rprd.list<-regpred(xpred0,xmat0,yvec0)
ypredb<-rprd.list$pred
sdpredb<-rprd.list$sd
dfb<-rprd.list$df

bootvec<-c(bootvec,(ypredb-ypred0)/sdpredb)

}

lq<-quantile(bootvec,alpha/2)
uq<-quantile(bootvec,1-alpha/2)

LB<-ypred0-(sdpred0)*uq
UB<-ypred0-(sdpred0)*lq

NLB<-ypred0-(sdpred0)*qt(1-alpha/2,df0)
NUB<-ypred0+(sdpred0)*qt(1-alpha/2,df0)
list(bootstrap.confidence.interval=c(LB,UB),normal.confidence.interval=c(NLB,NUB))

}

> regpred<-
function(xpred,xmat,y){

ls.str<-lsfit(xmat,y)
calculate prediction
ypred<-ls.str$coef%*%c(1,xpred)
use ls.diag to extract covariance matrix
ycov<-ls.diag(ls.str)$cov.unscaled
use ls.diag to extract std deviation
std.dev<-ls.diag(ls.str)$std.dev
variance of data around line
v1<-std.dev^2
variance of prediction
vpred<-v1*c(1,xpred)%*%ycov%*%c(1,xpred)
df=length(y)-length(diag(ycov))
list(pred=ypred,sd=sqrt(vpred),df=df)

}


Related Solutions

C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Use R programming to resolve this; can you please provide details on the code? A) Create...
Use R programming to resolve this; can you please provide details on the code? A) Create a dataframe – comparativeGenomeSize with the following vectors: > organism<-c("Human","Mouse","Fruit Fly", "Roundworm","Yeast") > genomeSizeBP<-c(3000000000,3000000000,135600000,97000000,12100000) > estGeneCount<-c(30000,30000,13061,19099,6034) B) Print the organism and estGeneCount for Human and fruitfly.(1) C) Add a column to this data frame calculating base pairs per gene. To do this, write a function “genedensity” that takes as arguments the genomesizeBP and estimatedGeneCount information, and calculates from this the estimated bp per gene....
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
I AM USING R PROGRAMMING LANGUAGE / R- CODE **Problem 3 (5 pts) - similar to...
I AM USING R PROGRAMMING LANGUAGE / R- CODE **Problem 3 (5 pts) - similar to Pr. 44 page 243 in text** Consider tossing **four** fair coins. There are 16 possible outcomes, e.g `HHHH,HHHT,HHTH,HTHH,THHH, ...` possible outcomes. Define `X` as the random variable “number of heads showing when four coins are tossed.” Obtain the mean and the variance of X. Simulate tossing three fair coins 10,000 times. Compute the simulated mean and variance of X. Are the simulated values within...
This the question about the java and please show the detail comment and code of each...
This the question about the java and please show the detail comment and code of each class.Thank you. And the "+" means "public" the "-" means "private" and testBankAccount(): void testMobilePhone(): void testChocolate(): void all are static Create four classes with the following UML diagrams: +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ |...
Project on a double pendulum using MATLAB. I have simulated double pendulum chaos using the code...
Project on a double pendulum using MATLAB. I have simulated double pendulum chaos using the code I found in Chegg. Could you please guide in adding a code to determine the time it takes for the second arm of the pendulum to "flip" based on initial starting conditions?
I entered in this code for my programming fundamentals class for this question To encourage good...
I entered in this code for my programming fundamentals class for this question To encourage good grades, Hermosa High School has decided to award each student a bookstore credit that is 10 times the student’s grade point average. In other words, a student with a 3.2 grade point average receives a $32.0 credit. Create an application that prompts a student for a name and grade point average, and then passes the values to a method (computeDiscount) that displays a descriptive...
Write code in SAS to do each of the following I have posted the data below...
Write code in SAS to do each of the following I have posted the data below from a pace delimited data set consisting of 66 randomly selected cars Upload the data set to SAS and store it as a SAS data set called cars. Make sure the full values are stored for all character variables. Create a comparative bar chart (with appropriate title and labels) displaying the brands of each car by fuel type (so that fuel type is on...
These code are correct, and I want some explanation or comment for them(purpose for each function)...
These code are correct, and I want some explanation or comment for them(purpose for each function) def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2: return 2 if n == 3: return 6 if n == 4: return 4 * annoying_factorial(3) if n == 5: return 5 * annoying_factorial(4) if n == 6: return 6 * annoying_factorial(5) else: return n * annoying_factorial(n-1) def annoying_fibonacci(n): if n==0: return 0 if n==1: return 1 if...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT