4 Finding the Variance and Weights

 mobility <- read.csv("http://www.stat.cmu.edu/~cshalizi/mreg/15/lectures/24--25/mobility2.csv")
      plot(Mobility ~ Population, data=mobility, log="x", ylim=c(0,0.5))
      
      mobility$MobSE <- with(mobility, sqrt(Mobility*(1-Mobility)/Population))
      plot(Mobility ~ Commute, data=mobility,
           xlab="Fraction of workers with short commutes",
           ylab="Rate of economic mobility", pch=19, cex=0.2)
      with(mobility, segments(x0=Commute, y0=Mobility+2*MobSE,
                              x1=Commute, y1=Mobility-2*MobSE, col="blue"))
      mob.lm <- lm(Mobility ~ Commute, data=mobility)
      mob.wlm <- lm(Mobility ~ Commute, data=mobility, weight=1/MobSE^2)
      abline(mob.lm)
      abline(mob.wlm, col="blue")
      

      





Figure 10: Mobility versus the fraction of workers with short commute, with ±2standard deviation error bars (vertical blue bars), and the OLS linear fit (black line) andweighted least squares (blue line). Note that the error bars for some larger communitiesare smaller than the diameter of the dots.







 plot(x,residuals(fit.ols)^2,ylab="squared residuals")
      curve((1+x^2/2)^2,col="grey",add=TRUE)
      var1 <- smooth.spline(x=x, y=log(residuals(fit.ols)^2), cv=TRUE)
      grid.x <- seq(from=min(x),to=max(x),length.out=300)
      lines(grid.x, exp(predict(var1,x=grid.x)$y))
      
      
      fit.wls1 <- lm(y~x,weights=1/exp(var1$y))
      coefficients(fit.wls1)
      var2 <- smooth.spline(x=x, y=log(residuals(fit.wls1)^2), cv=TRUE)
      
      fit.wls2 <- lm(y~x,weights=1/exp(var2$y))
      coefficients(fit.wls2)
      var3 <- smooth.spline(x=x, y=log(residuals(fit.wls2)^2), cv=TRUE)
      
      fit.wls3 <- lm(y~x,weights=1/exp(var3$y))
      coefficients(fit.wls3)
      
      var4 <- smooth.spline(x=x, y=log(residuals(fit.wls3)^2), cv=TRUE)
      fit.wls4 <- lm(y~x,weights=1/exp(var4$y))
      coefficients(fit.wls4)
      
      
      
      fit.wls1 <- lm(y~x,weights=1/exp(var1$y))
      par(mfrow=c(1,2))
      plot(x,y)
      abline(a=3,b=-2,col="grey")
      abline(fit.ols,lty="dashed")
      abline(fit.wls1,lty="dotted")
      plot(x,(residuals(fit.ols))^2,ylab="squared residuals")
      points(x,residuals(fit.wls1)^2,pch=15)
      lines(grid.x, exp(predict(var1,x=grid.x)$y))
      var2 <- smooth.spline(x=x, y=log(residuals(fit.wls1)^2), cv=TRUE)
      curve((1+x^2/2)^2,col="grey",add=TRUE)
      lines(grid.x, exp(predict(var2,x=grid.x)$y),lty="dotted")
      par(mfrow=c(1,1))
      
      
   




iterative.wls <- function(x,y,tol=0.01,max.iter=100) {
iteration <- 1
old.coefs <- NA
regression <- lm(y~x)
coefs <- coefficients(regression)
while (is.na(old.coefs) ||
((max(abs(coefs - old.coefs)) > tol) && (iteration < max.iter))) {
variance <- smooth.spline(x=x, y=log(residuals(regression)^2), cv=TRUE)
old.coefs <- coefs
iteration <- iteration+1
regression <- lm(y~x,weights=1/exp(variance$y))
coefs <- coefficients(regression)
}
return(list(regression=regression,variance=variance,iterations=iteration))

}


### 非零返回值的顺序逻辑 在编程中,非零返回值通常用于指示函数执行的结果状态。如果返回值为非零,则表示操作成功或特定条件满足;而零可能代表失败或其他特殊情况。以下是关于如何实现和理解这种逻辑的一些关键点。 #### 使用断言验证非零返回值 在硬件描述语言(HDL)中,例如Verilog中的测试平台代码片段[^1]展示了通过`assert`语句来验证某些条件下变量是否具有预期的非零值。此方法可以扩展到其他场景: ```verilog initial begin input1 = 1; @(posedge clk); @(negedge clk); assert(output1 != 0) else $error("Output is zero when expected non-zero"); end ``` 上述代码确保 `output1` 的值不等于零,否则会触发错误消息并停止仿真过程。 #### JavaScript 中处理数组过滤后的非零结果 对于高级脚本语言如JavaScript,在数据流管道中也可以应用类似的思路。下面的例子说明了如何筛选偶数并将它们乘以十之后求和得到最终结果[^2]: ```javascript const result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .filter(n => n % 2 === 0) .map(a => a * 10) .reduce((a, b) => a + b); if(result !== 0){ console.log(`Non-Zero Result: ${result}`); } else { throw new Error('Unexpected Zero Value'); } ``` 这里我们不仅实现了功能需求还加入了简单的异常捕获机制以防万一计算过程中出现了意外情况下的零值输出。 #### 处理未定义与非零判断的关系 当涉及到动态类型的环境比如JavaScript时,“undefined”的概念变得尤为重要因为它直接影响着程序的行为模式以及潜在的风险控制措施[^3]。考虑如下情形: ```javascript let myVar; function checkValue(val){ if(typeof val === 'undefined' || val === null ){ return false; } return !!val; // Convert any truthy value into true and falsy ones to false. } console.log(checkValue(myVar)); // Outputs False because it's undefined. myVar = 0; console.log(checkValue(myVar)); // Still outputs False since we treat both as invalid states here. myVar = -5; console.log(checkValue(myVar)); // Now returns True indicating valid non-zero condition met. ``` 在这个例子当中,即使输入的是负数(-5),只要它不是null或者undefined就被认为是一个有效的“non-zero”状态被接受下来继续后续流程。 ### 结论 综上所述,无论是低级硬件模拟还是高层软件开发领域里都存在着针对不同上下文中定义好的规则去判定某个表达式的取值范围从而决定下一步动作走向这样的通用原则。具体实现方式取决于所使用的工具集及其特性支持程度等因素影响较大。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值