df=data.frame(x=1:10,y=c(1,3,2,5,6,8,5,11,13,14)) df plot(y~x,data=df) plot(x,y) plot(df$x,df$y) lm(y~x,data=df) plot(y~x,data=df) df=data.frame(x=1:10,y=c(1,3,2,5,6,8,5,11,13,14)) abline(lm(y~x,data=df)) fit=lm(y~x,data=df) summary(fit) coef(fit) fit=lm(y~x,data=df) fit$coef summary(fit)$r.sq str(summary(fit)) fit$coef summary(fit)$coef df=data.frame(x=1:10,y=c(1,3,2,5,6,8,5,11,13,14)) str(df) str(summary(fit)) df=data.frame(x=1:10,y=c(1,3,2,5,6,8,5,11,13,14)) df$f=c('a','b','a','b','b','a') df$f=c('a','b','a','b','b','a','b','a','a','b') df lm(y~f,data=df) df$f=as.factor(c('a','b','a','b','b','a','b','a','a','b')) str(df) tapply(df$y,df$f,mean) mean(df$y[df$f=='a']) mean(df$y[df$f=='b']) df lm(y~f,data=df) tapply(df$y,df$f,mean) lm(y~x+f,data=df) ptA=df$f=='a' ptA points(y~x,data=df[ptA,],col='red') points(y~x,data=df[ptA,],col='red',pch=16) points(y~x,data=subset(df,ptA),col='red',pch=16) points(y~x,data=subset(df,!ptA),col='blue',pch=16) abline(lm(y~x,data=subset(df,ptA)),col='red') abline(lm(y~x,data=subset(df,!ptA)),col='blue') points(y~x,data=subset(df,!ptA),col='blue',pch=3) fit=lm(y~x+f,data=df) fit$coef plot(y~x,data=df) points(y~x,data=subset(df,ptA),col='red',pch=16) abline(fit$coef[1:2],col='red') points(y~x,data=subset(df,!ptA),col='blue',pch=16) cfA=fit$coef[1:2] cfA abline(cfA,col='blue') cfB=cfA cfB[1]+fit$coef[3] cfB[1]=fit$coef[1]+fit$coef[3] cfB fit$coef abline(cfB,col='red') fit=lm(y~x+f,data=df) attach('~/Desktop/treemass.rdata') ls(2) search() ls(2) ls(2) head(treemass) str(treemass) colnames(treemass) options(width=80) colnames(treemass) options(width=50) colnames(treemass) options(width=60) colnames(treemass) table(treemass$ForestType) plot(y~x,data=df) abline(cfB,col='red') cf=c(6,0) abline(cf,col='blue') df yhat=cf[1]+df$x*cf[2] yhat cfB yhat=cfB[1]+df$x*cfB[2] points(df$x,yhat) points(df$x,yhat,pch=3) yhat=cf[1]+df$x*cf[2] points(df$x,yhat,pch=3) yhat-df$y (yhat-df$y)^2 sum((yhat-df$y)^2) yhatB=cfB[1]+df$x*cfB[2] sum((yhatB-df$y)^2) sumsq=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum(yhat-y)^2)) } sumsq=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum(yhat-y)^2) } sumsq sumsq(param=cfB,x=df$x,y=df$y) sumsq(param=c(0,0),x=df$x,y=df$y) yhatB sumsq=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum((yhat-y)^2)) } sumsq(param=cfB,x=df$x,y=df$y) sumsq=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum((yhat-y)^2)) } sumsq(param=cfA,x=df$x,y=df$y) coef(lm(y~x,data=df)) cf=coef(lm(y~x,data=df)) sumsq(param=cf,x=df$x,y=df$y) sumsq(param=c(-1,1.4),x=df$x,y=df$y) sumsq(param=c(-1,1.42),x=df$x,y=df$y) ?optim ?optim ?optim optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y) optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y,control=list(trace=1)) fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y,control=list(trace=1)) fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y) fit$val fit$par coef(lm(y~x,data=df)) fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y) ?optim fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y,control=list(reltol=1e-9)) fit$par fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y,control=list(reltol=1e-10)) fit$par summary(lm(y~x,data=df))$resid summary(lm(y~x,data=df))$resid^2 sum(summary(lm(y~x,data=df))$resid^2) fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y) fit$val sumsq=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum((yhat-y)^2)) } source('~/Desktop/teaching.functions.r') args(sumsq.linearmodel) sumsq.linearmodel=function(beta,x,y,graphit=FALSE) { yhat=beta[1]+x*beta[2] dev.squared=(yhat-y)^2 sumsq=sum(dev.squared,na.rm=TRUE) cat(beta,sumsq,"\n") if(graphit) graph.linearmodel(beta,x,y,addpts=FALSE) # browser() return(sumsq) } source('~/Desktop/teaching.functions.r') sumsq.linearmodel(beta=c(0,0),x=df$x,y=df$y) sumsq(param=c(0,0),x=df$x,y=df$y) fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y) fit$par fit=optim(par=c(0,0),fn=sumsq.linearmodel,x=df$x,y=df$y) fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y) save.history('~/meetings_workshops/Rmodeling/Cenpat/historyTuesAM9Oct.r') savehistory('~/meetings_workshops/Rmodeling/Cenpat/historyTuesAM9Oct.r') plot(y~x,data=df) abline(cf,col='blue') dnorm(3,mean=0,sd=1) textx=seq(-10,10,by=1) testx=seq(-10,10,by=1) testx dnorm(testx,mean=0,sd=1) plot(testx,dnorm(testx,mean=0,sd=1)) cumprod(dnorm(testx,mean=0,sd=1)) plot(y~x,data=df) dnorm(3,mean=0,sd=1) abline(cf,col='blue') dnorm(.3,mean=0,sd=1) dnorm(3,mean=0,sd=1,log=TRUE) dnorm(.3,mean=0,sd=1,log=TRUE) plot(testx,dnorm(testx,mean=0,sd=1,log=TRUE)) sumsq llike=function(param,x,y) { yhat=x*param[2]+param[1]; return() } plot(y~x,data=df) llike=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum(dnorm(yhat-y,mean=0,sd=2,log=TRUE))) } llike llike(param=c(0,0),x=df$x,y=df$y) llike(param=c(-1,1.4),x=df$x,y=df$y) cf llike(param=cf,x=df$x,y=df$y) sumsq(param=c(-1,1.4),x=df$x,y=df$y) sumsq(param=c(0,0),x=df$x,y=df$y) fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y) ?optim fit=optim(par=c(0,0),fn=sumsq,x=df$x,y=df$y,control=list(fnscale=(-1)) ) q,x=df$x,y=df$y,control=list(fnscale=(-1)) fit=optim(par=c(0,0),fn=llike,x=df$x,y=df$y,control=list(fnscale=(-1))) fit$par llike=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum(dnorm(yhat-y,mean=0,sd=2,log=TRUE))) } llike=function(param,x,y) { yhat=x*param[2]+param[1]; return(sum(dnorm(yhat-y,mean=0,sd=param[3],log=TRUE))) } fit=optim(par=c(0,0),fn=llike,x=df$x,y=df$y,control=list(fnscale=(-1))) fit=optim(par=c(0,0,10),fn=llike,x=df$x,y=df$y,control=list(fnscale=(-1))) fit$par abline(fit$par[1:2]) savehistory('~/meetings_workshops/Rmodeling/Cenpat/historyTuesPM9Oct.r') savehistory('~/meetings_workshops/Rmodeling/Cenpat/historyTuesPM9Oct.txt')